【问题标题】:Kivy Line(circle=(x, y, r)) - radius AnimationKivy Line(circle=(x, y, r)) - 半径动画
【发布时间】:2021-10-17 20:48:18
【问题描述】:

我的期望:

半径为 100px 的圆将动画为半径 150px。解决方案可能很简单,但这个问题超出了我的知识范围......

小例子

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Line, Ellipse
from kivy.clock import Clock
from kivy.animation import Animation


class AnimationExample(BoxLayout):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        Clock.schedule_interval(self.animate, 2)

        with self.canvas:
            self.pulsating_line = Line(
                        circle=(self.center_x, self.center_y, 100),
                        width=2)

        self.bind(size = self.update,
                    pos = self.update
                    )

    def update(self, *args):
        self.pulsating_line.circle = (self.center_x,
                                      self.center_y,
                                      100,
                                      )
    def animate(self, *args):
        print('Animate')

        circ = self.pulsating_line
        circ.circle = (self.center_x,
                       self.center_y,
                       100,
                       )
        circ.width = 2

        # This works for animated "width"
        Animation(width=8,
            t='out_bounce',
            d=1.1).start(circ)
   
        # In my opinion is this analogical approach, but of course, it not works :/
        # Animation(circle=(self.center_x,
        #                   self.center_y,
        #                   150,
        #                   ),
        #           t='out_bounce',
        #           d=0.8).start(circ)

class TutorialApp(App):
    def build(self):
        return AnimationExample()

TutorialApp().run()

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    使用Propertieskv 为画布指令设置动画是一种方便的方法。 kv 自动设置绑定Properties,所以动画只是动画那些Properties。这是使用该方法的代码的修改版本:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.properties import NumericProperty, ListProperty
    from kivy.uix.boxlayout import BoxLayout
    from kivy.clock import Clock
    from kivy.animation import Animation
    
    kv = '''
    <AnimationExample>:
        canvas:
            Color:
                rgba: self.circle_color
            Line:
                circle: self.center_x, self.center_y, self.radius  # this sets up bindings to the Properties defined in AnimationExample
                width: self.line_width  # this sets up a binding to the line_width Property
            
    '''
    
    
    class AnimationExample(BoxLayout):
        # define properties for the circle
        circle_color = ListProperty([1, 0, 0, 1])
        radius = NumericProperty(100)
        line_width = NumericProperty(2)
    
        def animate(self, *args):
            # since the kv sets up bindings to the properties of this class, we can just animate those properties
            print('Animate')
    
            # This works for animated "width"
            width_anim = Animation(line_width=8, t='out_bounce', d=1) + Animation(line_width=2, t='out_bounce', d=1)
            width_anim.start(self)
    
            # to animate the circle radius
            radius_anim = Animation(radius=150, d=1) + Animation(radius=100, d=1)
            radius_anim.start(self)
    
    
    class TutorialApp(App):
        def build(self):
            Builder.load_string(kv)
            r = AnimationExample()
            Clock.schedule_interval(r.animate, 2)
            return r
    
    
    TutorialApp().run()
    

    【讨论】:

    • 感谢您的及时答复。我还有一个额外的问题:当我有更多不同半径的圆时,我是否需要为每个圆使用特定的 ID(在 *.kv 中)?谢谢。
    • 您不能将ids 分配给画布指令。但是对于不同的半径,您可以只拥有几个Properties,或者您可以在ListProperty 中拥有一个半径列表。
    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2020-10-31
    相关资源
    最近更新 更多