如何在Kivy中制作重复的旋转动画?
问题描述:
我想制作一个可以旋转加载微调器图像的动画小部件。我已经研究过Animation
班,看起来它可以完成这项工作。但我无法找到一个方法来保持在一个方向上旋转部件不断如何在Kivy中制作重复的旋转动画?
这是我的代码有:
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?
答
您可以在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()