Kivy Fade-In Animation

2019-09-06 02:21发布

Is there a way to get a fade-in/-out animation on a canvas rectangle background in kivy? I tried it by using the Clock.schedule_interval() function. But several problems occured concerning concurrency and data raise.

One of my tries looks as follows:

 def calendarClicked(self, *args):
    self.alpha = 1.0
    self.alphaDelta = 0.01

    self.root.canvas.add(Color(1,0,0,self.alpha))
    self.root.canvas.add(Rectangle(size=self.root.size))

    def fadeIn(self, dt):

        self.alpha -= self.alphaDelta
        self.root.canvas.add(Color(1,0,0,self.alpha))
        self.root.canvas.add(Rectangle(size=self.root.size))

        return self.alpha >= 0.5

    Clock.schedule_interval(partial(fadeIn, self), 0.1)   

Another idea was to use the kivy.animation. But I coudn't find a way to edit the color instead of the position of an object/widget.

Thanks in advance!

1条回答
叛逆
2楼-- · 2019-09-06 02:34

You can use an Animation on canvas instructions just like you would a widget. The problem is that you're not modifying the canvas instructions at all, you just keep adding more and more canvas instructions. You need to add the instructions one time only, then update the values via Animation.

def calendarClicked(self, *args):
    if not hasattr(self, 'color'):
        with self.root.canvas:
            self.color = Color(1, 0, 0, 0)
            self.rect = Rectangle(size=self.root.size)

    self.color.a = 0
    anim = Animation(a=1.0)
    anim.start(self.color)
查看更多
登录 后发表回答