绝望的角度例子

Kivy Angle Examples

角度旋转只适用于第一个例子

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

据我所知,这与 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)

第二个 KVlang 块不正确,Rotate 上的缩进过多(出于文体目的,PushMatrix 后缺少“:”)。