【问题标题】:How to make a repetitive rotating animation in Kivy?如何在 Kivy 中制作重复的旋转动画?
【发布时间】:2017-05-10 08:42:31
【问题描述】:

我想制作一个可以旋转加载微调器图像的动画小部件。我查看了Animation 课程,似乎它可以完成这项工作。但是我找不到一种方法来不断地在一个方向上旋转小部件

这是我的代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.graphics import Rotate
from kivy.animation import Animation
from kivy.properties import NumericProperty

Builder.load_string('''                                                                                                                                        
<Loading>:                                                                                                                                                 
    canvas.before:                                                                                                                                             
        PushMatrix                                                                                                                                             
        Rotate:                                                                                                                                                
            angle: self.angle                                                                                                                                  
            axis: (0, 0, 1)                                                                                                                                    
            origin: self.center                                                                                                                                
    canvas.after:                                                                                                                                              
        PopMatrix                                                                                                                                              
''')

class Loading(Image):
    angle = NumericProperty(0)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        anim = Animation(angle = 360)
        anim += Animation(angle = -360)
        anim.repeat = True
        anim.start(self)


class TestApp(App):
    def build(self):
        return Loading()

TestApp().run()

当您启动它时,您会看到该小部件向一个方向旋转 360 度,然后旋转旋转。如何构建动画序列,使角度不断增加或每 360 次旋转下降到 0?

【问题讨论】:

    标签: python animation rotation kivy


    【解决方案1】:

    您可以在 on_angle 方法中将角度设置为 0。这是一个稍作修改的版本:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.floatlayout import FloatLayout
    from kivy.animation import Animation
    from kivy.properties import NumericProperty
    
    Builder.load_string('''                               
    <Loading>:
        canvas.before:
            PushMatrix
            Rotate:
                angle: root.angle
                axis: 0, 0, 1
                origin: root.center
        canvas.after:
            PopMatrix
    
    
        Image:
            size_hint: None, None
            size: 100, 100
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    ''')
    
    class Loading(FloatLayout):
        angle = NumericProperty(0)
        def __init__(self, **kwargs):
            super(Loading, self).__init__(**kwargs)
            anim = Animation(angle = 360, duration=2) 
            anim += Animation(angle = 360, duration=2)
            anim.repeat = True
            anim.start(self)
    
        def on_angle(self, item, angle):
            if angle == 360:
                item.angle = 0
    
    
    class TestApp(App):
        def build(self):
            return Loading()
    
    TestApp().run()
    

    【讨论】:

    • 何时调用on_angle()
    • 每次angle属性改变。
    猜你喜欢
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多