首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在屏幕上定位Kivy动画。

在屏幕上定位Kivy动画。
EN

Stack Overflow用户
提问于 2017-07-28 07:42:13
回答 2查看 746关注 0票数 1

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

我已经搜索过了,但没有找到任何关于如何将图像重新定位到右上角的信息,以及/或者关于如何确保图像在移动后保持正确旋转的信息。

如果能帮上忙我会很感激。

提前谢谢。

....brad...

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

代码语言:javascript
运行
复制
# 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()   
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-28 18:33:14

谢谢你的一个清晰的问题。

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

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

代码如下:

代码语言:javascript
运行
复制
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()

结果如下所示:

票数 0
EN

Stack Overflow用户

发布于 2017-07-28 23:33:55

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

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

在下面的代码中,我使用了pos_hint:{'top':.7,'right':1.1}。这些数字将动画的顶部置于屏幕上方70%,将动画的右侧置于屏幕之外。所以对我来说,top的意思是“从底部往上看屏幕”,而right的意思是“从左边穿过屏幕”。

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

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

代码语言:javascript
运行
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45362689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档