Kivy自定义动画功能

2024-03-29 14:37:39 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以组合各种内置的动画功能,甚至创建自定义功能? 我喜欢以下功能:

输入输出三次方、输入输出四次方、输入输出正弦

但我想做一些像:

输入/输出正弦

看看会不会没事。你知道吗

用其他数学函数来创建各种效果也很有趣。你知道吗

如何在Kivy做到这一点?你知道吗


Tags: 方法函数功能动画数学内置输入输出效果
1条回答
网友
1楼 · 发布于 2024-03-29 14:37:39

你所指出的可能有几种解释,所以我将向你展示不同的可能性:

  • 使用从p1到p2的in_cubic动画和从p2到终点p3的out_sine动画。你知道吗

    from kivy.animation import Animation
    from kivy.app import App
    from kivy.uix.button import Button
    
    class TestApp(App):
        def animate(self, instance):
            animation = Animation(pos=(200, 200), t='in_cubic')
            animation += Animation(pos=(400, 400), t='out_sine')
            animation.start(instance)
    
        def build(self):
            button = Button(size_hint=(None, None), text='plop',
                            on_press=self.animate)
            return button
    
    if __name__ == '__main__':
        TestApp().run()
    
  • 应用50%的进位立方和进位立方,为此我们创建了一个新函数:

    from kivy.animation import Animation, AnimationTransition
    from kivy.app import App
    from kivy.uix.button import Button
    
    
    def in_cubic_out_sine(progress):
        return AnimationTransition.in_cubic(progress) if progress < 0.5 else AnimationTransition.out_sine(progress)
    
    class TestApp(App):
        def animate(self, instance):
            animation = Animation(pos=(200, 200), t=in_cubic_out_sine)
            animation.start(instance)
    
        def build(self):
            button = Button(size_hint=(None, None), text='plop',
                            on_press=self.animate)
            return button
    
    if __name__ == '__main__':
        TestApp().run()
    

一般来说,您可以实现自己的函数,唯一需要记住的是,progress取0到1之间的值:

from kivy.animation import Animation
from kivy.app import App
from kivy.uix.button import Button

from math import cos, sin, pi, exp

def custom_animation(progress):
    return 1 - exp(-progress**2)*cos(progress*pi)**3

class TestApp(App):
    def animate(self, instance):
        animation = Animation(pos=(200, 200), t=custom_animation)
        animation.start(instance)

    def build(self):
        button = Button(size_hint=(None, None), text='plop',
                        on_press=self.animate)
        return button

if __name__ == '__main__':
    TestApp().run()

相关问题 更多 >