【问题标题】:Kivy Animation start Animation when a other Animation is completeKivy 动画在其他动画完成时开始动画
【发布时间】:2017-08-11 08:52:31
【问题描述】:

当另一个动画完成时,我如何在 kivy 动画中开始?我想开始第一个动画,当这个动画完成时,我想开始下一个动画。

def on_button_clickedBIG(self):
        myLayout.CokkieAnzahl += 1

        print(myLayout.CokkieAnzahl)
        self.ids.CokkieZahl.text = str(myLayout.CokkieAnzahl)

        anim1 = Animation(size=(650,350), duration=0.3)
        anim1.start(self.ids.CookieImage)


    def on_button_clickedSMALL(self):

        anim2 = Animation(size=(600, 300))
        anim2.start(self.ids.CookieImage)

这里是我的 kv 语言代码:

 Image:
                size_hint_y: None
                size_hint_x: None
                keep_ratio: True
                allow_stretch: True
                id: CookieImage
                source: "cookie.png"
                on_touch_down: root.on_button_clickedBIG()
                #on_touch_down: root.on_button_clickedSMALL()
                size: 600, 300
                pos: -80, -30

EDIT1: 我尝试了但我不起作用,所以我问是否有人知道这里的方法是我的解决方案我如何尝试但它不起作用:

anim1.bind(on_complete=myLayout.on_button_clickedSMALL)

我把这个放在def on_button_clickedBIG(self): 但我得到一个错误:TypeError: on_button_clickedSMALL() takes 1 positional argument but 2 were given

【问题讨论】:

    标签: animation kivy


    【解决方案1】:

    您必须将以下内容添加到您的程序中。我还提供了一个示例供您参考。

    1. animation1.bind(on_complete=animation_complete)

    2. 函数动画_完成

    您的计划

    def on_button_clickedBIG(self):
        myLayout.CokkieAnzahl += 1
    
        print(myLayout.CokkieAnzahl)
        self.ids.CokkieZahl.text = str(myLayout.CokkieAnzahl)
    
        anim1 = Animation(size=(650,350), duration=0.3)
        anim1.bind(on_complete=self.animation_complete)
        anim1.start(self.ids.CookieImage)
    
    
    def animation_complete(self, widget, item):
        self.remove_widget(item)
        self.on_button_clickedSMALL()
    
    
    def on_button_clickedSMALL(self):
    
        anim2 = Animation(size=(600, 300))
        anim2.start(self.ids.CookieImage)
    

    程序示例

    动画.py

    ​​>
    import kivy
    kivy.require('1.10.0')
    
    from kivy.app import App
    from kivy.animation import Animation
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.button import Button
    from kivy.properties import NumericProperty
    
    
    class AnimateRecord(FloatLayout):
    
        angle = NumericProperty(0)
    
        def __init__(self, **kwargs):
            super(AnimateRecord, self).__init__(**kwargs)
            animation_record = Animation(angle=360, duration=2)
            animation_record += Animation(angle=360, duration=2)
            animation_record.repeat = True
            animation_record.start(self)
    
        def on_angle(self, item, angle):
            if angle == 360:
                item.angle = 0
    
    
    class AnimateButton(FloatLayout):
    
        def animate_button(self, instance):
            # create an animation object. This object could be stored
            # and reused each call or reused across different widgets.
            # += is a sequential step, while &= is in parallel
            animation_btn = Animation(pos=(100, 100), t='out_bounce')
            animation_btn += Animation(pos=(200, 100), t='out_bounce')
            animation_btn &= Animation(size=(500, 500))
            animation_btn += Animation(size=(100, 50))
    
            animation_btn.bind(on_complete=self.animation_complete)
    
            # apply the animation on the button, passed in the "instance" argument
            # Notice that default 'click' animation (changing the button
            # color while the mouse is down) is unchanged.
            animation_btn.start(instance)
    
        def animation_complete(self, widget, item):
            self.remove_widget(item)
            self.add_widget(AnimateRecord())
    
    
    class AnimationsApp(App):
    
        def build(self):
            layout = AnimateButton()
            # create a button, and  attach animate() method as a on_press handler
            button = Button(size_hint=(None, None), text='plop',
                            on_press=layout.animate_button)
            layout.add_widget(button)
            return layout
    
    
    if __name__ == '__main__':
        AnimationsApp().run()
    

    动画.kv

    #:kivy 1.10.0
    
    <AnimateRecord>:
        # Describe the record's position and size here
        size_hint: None, None
        size: 200, 200
        pos_hint: {'top': 0.7, 'right': 1.1}
    
        canvas.before:
            PushMatrix
            Rotate:
                angle: root.angle
                axis: (0, 0, 1)
                origin: root.center
        canvas.after:
            PopMatrix
    
        Image:
            source: '/home/iam/Pictures/Images/45rpm.png'
            size_hint: None, None
            # size: 200, 200
            size: self.parent.size  # So we don't have to change it every time, change the size above only
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    

    【讨论】:

      【解决方案2】:

      答案在 Kivy 文档中:https://kivy.org/docs/api-kivy.animation.html 下次在此处询问之前尝试在那里搜索。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-29
        • 2016-03-06
        • 1970-01-01
        • 2017-04-17
        • 2018-03-19
        相关资源
        最近更新 更多