kivy倒计时动画流畅度和同步度

kivy Countdown Animation smoothness and synchronization

我用kivy编写了这个倒计时动画。它有两个问题:

如果有任何修复建议,我将不胜感激。谢谢

这是代码 (py)

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.properties import NumericProperty


class CountDownLbl(Label):
    angle = NumericProperty(0)
    startCount = NumericProperty(20)
    Count = NumericProperty()
    def __init__(self, **kwargs):
        super(CountDownLbl, self).__init__(**kwargs)
        Clock.schedule_once(self.set_Circle, 0.1)
        self.Count = self.startCount
        Clock.schedule_interval(lambda x: self.set_Count(), 1)

    def set_Circle(self, dt):
        self.angle = self.angle + dt*360
        if self.angle >= 360:
            self.angle = 0
        Clock.schedule_once(self.set_Circle, 0.1)

    def set_Count(self):
        self.Count = self.Count - 1


class PhotoBoothApp(App):
    pass

if __name__ == '__main__':
    try:
        app = PhotoBoothApp()
        app.run()
    except KeyboardInterrupt:
        app.stop()

(kv)

CountDownLbl:
    text: str(self.Count)
    font_size: 30
    canvas:
        Color:
            rgb: 1,0,1
        Line:
            circle:self.center_x, self.center_y, 90, 0, self.angle
            width: 5

将间隔更改为 1.0/360

Clock.schedule_once(self.set_Circle, 1.0/360)

你也可以这样写:

class CountDownLbl(Label):

    angle = NumericProperty(0)
    startCount = NumericProperty(20)
    Count = NumericProperty()

    def __init__(self, **kwargs):
        super(CountDownLbl, self).__init__(**kwargs)
        Clock.schedule_once(self.set_Circle, 0.1)
        self.Count = self.startCount

    def set_Circle(self, dt):
        self.angle = self.angle + dt*360
        if self.angle >= 360:
            self.angle = 0
            self.Count = self.Count - 1
        if self.Count > 0:
            Clock.schedule_once(self.set_Circle, 1.0/360)

您可以使用 Animation 然后玩数学:

main.py:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import NumericProperty
from kivy.animation import Animation

class RootWidget(BoxLayout):
    pass

class CountDownLbl(Label):
    startCount = NumericProperty(10)
    angle = NumericProperty(0)

    def start(self):
        Animation.cancel_all(self)
        self.anim = Animation(angle=360 * self.startCount - 1,  duration=self.startCount)
        self.anim.bind(on_complete=self.finish)
        self.anim.start(self)

    def finish(self, animation, incr_crude_clock):
        incr_crude_clock.text = "FINISHED"


class TestApp(App):
    def build(self):
        return RootWidget()

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

test.kv:

<RootWidget>:
    orientation: "vertical"
    CountDownLbl:
        id: anim_label
        text: str(int(self.startCount - self.angle // 360))
        font_size: 30
        canvas:
            Color:
                rgb: 1,0,1
            Line:
                circle:self.center_x, self.center_y, 90, 0, self.angle % 360
                width: 5
    Button:
        size_hint_y: 0.1
        text: "Start"
        on_press: anim_label.start()

输出: