問題描述
我想制作一個(gè)可以旋轉(zhuǎn)加載微調(diào)器圖像的動(dòng)畫小部件.我查看了 Animation
類,它似乎可以完成這項(xiàng)工作.但是我找不到一種方法來不斷地在一個(gè)方向上旋轉(zhuǎn)小部件
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()
當(dāng)您啟動(dòng)它時(shí),您會(huì)看到該小部件在一個(gè)方向上旋轉(zhuǎn) 360 度,然后旋轉(zhuǎn)旋轉(zhuǎn).如何構(gòu)建動(dòng)畫序列,使角度不斷增加或每 360 次旋轉(zhuǎn)下降到 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
方法中將角度設(shè)置為 0.這是一個(gè)稍作修改的版本:
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()
這篇關(guān)于如何在 Kivy 中制作重復(fù)的旋轉(zhuǎn)動(dòng)畫?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!