Kivy角度示例

时间:2017-05-03 15:55:29

标签: touch kivy angle

角度旋转仅适用于第一个示例

我想明白。为什么角度动画调用的一个示例有效,另一个失败。

从我可以收集的内容来看,这与Builder.load_string有关。

工作示例

#working example
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.image import Image

from kivy.graphics import Rotate
from kivy.properties import NumericProperty

from math import atan2, degrees

from kivy.animation import Animation

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

class PlayerImage(Image):
    angle = NumericProperty(0)

    def on_touch_down(self, touch):
        Animation.cancel_all(self)
        angle = degrees(atan2(touch.y - self.center_y, 
                              touch.x - self.center_x))

        Animation(center=touch.pos, angle=angle).start(self)

root = Builder.load_string('''                                                                                                                                 
Widget:                                                                                                                                                        
    PlayerImage:                                                                                                                                               
        source: 'images/example.png'                                                                                                                                 
        allow_stretch: True                                                                                                                                    
        keep_ratio: False                                                                                                                                    
''')

runTouchApp(root)

非工作示例

from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.image import Image

from kivy.graphics import Rotate
from kivy.properties import NumericProperty

from math import atan2, degrees

from kivy.animation import Animation

class PlayerImage(Image):
    angle = NumericProperty(0)

    def on_touch_down(self, touch):
        Animation.cancel_all(self)
        angle = degrees(atan2(touch.y - self.center_y, 
                              touch.x - self.center_x))

        Animation(center=touch.pos, angle=angle).start(self)


root = Builder.load_string('''                                                                                                                                 
Widget:                                                                                                                                                        
    PlayerImage:                                                                                                                                                
        source: 'images/example.png'                                                                                                                                 
        allow_stretch: True                                                                                                                                    
        keep_ratio: False
        canvas.before:                                                                                                                                             
            PushMatrix                                                                                                                                             
                Rotate:                                                                                                                                                
                    angle: self.angle                                                                                                                                  
                    axis: (0, 0, 1)                                                                                                                                    
                    origin: self.center                                                                                                                                
        canvas.after:                                                                                                                                              
            PopMatrix                                                                                                                                  
''')

runTouchApp(root)

1 个答案:

答案 0 :(得分:1)

第二个KVlang块不正确,Rotate上有多余的缩进(为了样式目的,在PushMatrix之后缺少“:”)。