kivy倒计时动画平滑和同步

时间:2017-12-01 09:14:10

标签: kivy kivy-language

enter image description here

我已经将这个倒计时动画编程为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

2 个答案:

答案 0 :(得分:1)

将间隔更改为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)

enter image description here

答案 1 :(得分:1)

您可以使用Animation,然后使用Math:

<强> 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()

<强>输出:

enter image description here