【问题标题】:Positioning a Kivy Animation On Screen.在屏幕上定位 Kivy 动画。
【发布时间】:2017-07-27 23:42:13
【问题描述】:

我一直在玩How to make a repetitive rotating animation in Kivy? 的代码。在下面的代码中,我使用它来旋转 45RPM 记录的图像。我想将记录的位置从屏幕中心更改为在屏幕右上角旋转。

我已搜索但找不到任何有关如何将图像重新定位到右上角的信息和/或有关如何确保图像在移动后保持正确旋转的信息。

我将不胜感激。

提前致谢。

....布拉德....

代码图片位于:https://drive.google.com/open?id=0B-T2cvsAoZ2vQ2hmaHM0SnlQVlU

# Modified from https://stackoverflow.com/questions/41321832/how-to-make-a-repetitive-rotating-animation-in-kivy
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:
        source: '45rpm.png'
        size_hint: None, None
        size: 200, 200
        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 RotationAnimation(App):
    def build(self):
        return Loading()

RotationAnimation().run()   

【问题讨论】:

    标签: kivy


    【解决方案1】:

    感谢您提出明确的问题。

    问题是您使用Loading 类作为主要小部件,它应该被称为LoadingCircleLoadingRecord 或类似的名称,并与其他小部件一起使用。

    所以,首先,添加另一个主小部件(我将添加一个FloatLayout)。然后我会告诉Loading 去哪里使用pos_hint 并将其大小设置为我想要的任何值。

    代码如下:

    from kivy.animation import Animation
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.properties import NumericProperty
    from kivy.uix.floatlayout import FloatLayout
    
    Builder.load_string('''                               
    <Loading>:
        # Describe the record's position and size here
        size_hint: None, None
        size: 200, 200
        pos_hint: {'top': 1, 'right': 1}
        canvas.before:
            PushMatrix
            Rotate:
                angle: root.angle
                axis: 0, 0, 1
                origin: root.center
        canvas.after:
            PopMatrix
        Image:
            source: '45rpm.png'
            size_hint: None, None
            size: self.parent.size # So we don't have to change it every time, change the size above only
            pos_hint: {'center_x': .5, 'center_y': .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 RotationAnimation(App):
        def build(self):
            f = FloatLayout()  # The new main widget
            f.add_widget(Loading())  # Add the loading record to it
            return f
    
    
    RotationAnimation().run()
    

    结果如下:

    【讨论】:

    • 这真的很棒,它应该让我开始。它还可以帮助我处理多个动画小部件。您知道我在哪里可以找到有关如何使用 pos_hint 属性的更多信息吗?我真的很想改变和了解更多关于 pos_hint: {'top': 1, 'right': 1} 的信息,这样我就可以更精细地调整动画的位置。
    • 嗯,是的,也不是。不幸的是,kivy 没有明确的教程。但是,文档非常好。 kivy.org/docs/… 这是一个很好的起点。只需用谷歌搜索你感兴趣的东西,你应该会找到一些文档。除此之外,如果您有时间和动力,只需查看一些 kivy 示例或其他人的代码即可。顺便说一句,如果它回答了您的问题,请考虑接受答案。
    【解决方案2】:

    为了进一步澄清这段代码(对于那些稍后阅读它的人)并在 Edvardas 的答案中回答我的问题,我现在了解如何更改 pos_hint 属性以调整动画的位置。

    在 Edvardas 的回答中,他使用 pos_hint: {'top': 1, 'right': 1} 来声明 45RPM 记录动画的位置。它将动画放置在屏幕右上角最完整的位置,而动画不会超出 Kivy GUI 的边界。 this pos_hint of 'top': 1, 'right': 1 将动画从左下角默认位置移动到 GUI 的右上角,而不超出窗口的限制。

    在下面的代码中,我使用 pos_hint: {'top': .7, 'right': 1.1}。这些数字将动画的顶部放置在屏幕上方的 70% 处,而动画的右侧则远离屏幕。所以对我来说,我认为 top 是“从底部向上的屏幕”,而 right 是“从左侧穿过屏幕”。

    知道了这一点后,我现在可以准确地将动画放置到屏幕上的任何点。非常有用的信息。再次感谢。

    我的输出如下所示,我将工作代码放在下面;

    from kivy.animation import Animation
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.properties import NumericProperty
    from kivy.uix.floatlayout import FloatLayout
    
    Builder.load_string('''                               
    <Loading>:
        # Describe the record's position and size here
        size_hint: None, None
        size: 200, 200
        pos_hint: {'top': .7, 'right': 1.1}
        canvas.before:
            PushMatrix
            Rotate:
                angle: root.angle
                axis: 0, 0, 1
                origin: root.center
        canvas.after:
            PopMatrix
        Image:
            source: '45rpm.png'
            size_hint: None, None
            size: self.parent.size # So we don't have to change it every time, change the size above only
            pos_hint: {'center_x': .5, 'center_y': .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 RotationAnimation(App):
        def build(self):
            f = FloatLayout()  # The new main widget
            f.add_widget(Loading())  # Add the loading record to it
            return f
    
    RotationAnimation().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多