2016-12-25 1 views
1

로딩 스피너 이미지를 회전시키는 애니메이션 위젯을 만들고 싶습니다. 나는 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으로 떨어 뜨릴 수 있도록 애니메이션 시퀀스를 어떻게 만들 수 있습니까?

답변

2

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() 
관련 문제