Kivy Fade-In Animation

岁酱吖の 提交于 2019-12-25 09:34:33

问题


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:


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)


来源:https://stackoverflow.com/questions/27446980/kivy-fade-in-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!