如何在Kivy中制作重复的旋转动画? [英] How to make a repetitive rotating animation in Kivy?

查看:139
本文介绍了如何在Kivy中制作重复的旋转动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个动画小部件,该小部件将旋转加载微调器图像.我研究了Animation类,看来它可以完成这项工作.但是我找不到一种方法来保持小部件不断向单一方向旋转

I want to make an animated widget that would rotate the loading spinner image. I've looked into the Animation class and it seems like it can do the job. But I couldn't find a way to keep rotating the widget in a single direction constantly

这是我的代码:

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?

When you launch it, you'll see that the widget rotates 360 degrees in one direction and then turns the rotation around. How could I build the animation sequence so that the angle would constantly keep increasing or would be dropped to 0 every 360-rotation?

推荐答案

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

You can set your angle to 0 inside on_angle method. Here's a slightly modified version:

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()

这篇关于如何在Kivy中制作重复的旋转动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆