在屏幕上定位Kivy动画。

时间:2017-07-27 23:42:13

标签: kivy

我一直在玩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()   

2 个答案:

答案 0 :(得分:0)

谢谢你提出一个明确的问题。

问题在于您使用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()

结果如下:

End result

答案 1 :(得分:0)

为了进一步澄清这段代码(对于那些日后阅读的代码)并在Edvardas的回答评论中回答我的问题,我现在明白如何改变pos_hint属性来调整位置动画。

在Edvardas的回答中,他使用了pos_hint:{&#39; top&#39;:1,&#39; right&#39;:1}来声明45RPM记录动画的位置。它将动画置于屏幕上最右上方的位置,动画不会超出Kivy GUI的界限。这个pos_hint&#39; top&#39; 1,&#39; right&#39;:1将动画从左下角默认位置移动到GUI的右上角,而不超出窗口的限制

在下面的代码中,我使用pos_hint:{&#39; top&#39;:。7,&#39; right&#39;:1.1}。这些数字将动画顶部的70%放在屏幕上,右边的动画侧面放在屏幕上。所以对我来说,我认为顶部是&#34;屏幕从底部开始#34;在左边的屏幕上&#34;右边#34;

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

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

enter image description here

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()