【问题标题】:uneven growth rate of progression in animation动画进展的不均匀增长率
【发布时间】:2020-08-24 16:35:27
【问题描述】:

看流动的代码:

from kivy.app           import App;
from kivy.uix.widget    import Widget;
from kivy.animation     import Animation;
from kivy.uix.button    import Button;
from time               import time;
import json;

but = Button();
anim = Animation(size_hint = (.75 , .75), duration = 1);
anim += Animation(size_hint = (.5 , .5), duration = 1);
anim += Animation(size_hint = (.25 , .25), duration = 1);
anim += Animation(size_hint = (.0 , .0), duration = 1);

progress_array = [];
time_array = [];

start_time = time();

def progr_fun(*args):
    global time_array, progress_array;
    time_array.append((time() - start_time));
    print((time() - start_time));
    progress_array.append(args[2]);
    print(args[2]);
    

anim.bind(on_progress = progr_fun);

anim.start(but);

class testApp(App):
    def build(self):
        return but;

if __name__ == '__main__':
    testApp().run();

f_obj = open('hello', 'w');
json.dump([progress_array, time_array], f_obj);
f_obj.close();

它的程序,为按钮制作简单的动画。动画由几个部分组成(很重要)。有回调 on_progress 收集时间数据和进度。此数据保存在程序结束时。 我正在使用另一个脚本按时间和进程构建情节并得到类似的东西: enter image description here

正如您所见,动画进度的不同部分增长不均衡。 为什么会这样?如何解决?

【问题讨论】:

  • 你能解释一下“不均匀”是什么意思吗?
  • 对不起我的英语。我在动画的不同部分讨论了不同的增长速度

标签: python animation kivy


【解决方案1】:

这是奇怪的行为,但我认为原因是用于步进动画不同部分的不同时间间隔。我相信这些间隔虽然会随着不同阶段的变化而变化,但在 kivy 时钟的分辨率范围内。

我不确定为什么这些间隔会发生变化。我相信这是在 kivy Clock 下的 C 代码中完成的。

Animation 对象的+ 运算符生成SequenceAnimation 子类,这似乎引发了差异。一种解决方法是构建自己的序列,如下所示:

    self.anim1 = Animation(size_hint=(.75, .75))
    self.anim2 = Animation(size_hint=(.5, .5))
    self.anim3 = Animation(size_hint=(.25, .25))
    self.anim4 = Animation(size_hint=(.0, .0))

    self.anim1.bind(on_progress=self.progr_fun, on_complete=self.start2)
    self.anim2.bind(on_progress=self.progr_fun, on_complete=self.start3)
    self.anim3.bind(on_progress=self.progr_fun, on_complete=self.start4)
    self.anim4.bind(on_progress=self.progr_fun)
    self.anim1.start(self.but)

def start2(self, *args):
    self.anim2.start(self.but)

def start3(self, *args):
    self.anim3.start(self.but)

def start4(self, *args):
    self.anim4.start(self.but)

【讨论】:

    【解决方案2】:

    另一种可能的解决方案 - 使用 time.time() 设置您自己的进度。它的罐子看起来像这样:

    but = Button();
    anim = Animation(size_hint = (.75 , .75), duration = 1);
    anim += Animation(size_hint = (.5 , .5), duration = 1);
    anim += Animation(size_hint = (.25 , .25), duration = 1);
    anim += Animation(size_hint = (.0 , .0), duration = 1);
    
    
    def start_fun(*args):
        global start_time;
        start_time = time();
    
    def progr_fun(*args):
        global time_array, progress_array, start_time , anim;
        my_progress = (time() - start_time) / anim.duration;
        print(my_progress);
        
    anim.bind(  on_progress = progr_fun,
                on_start = start_fun);
    
    anim.start(but);
    
    

    但是这个变体不是那么准确。你可以在动画结束时得到类似的东西:

    1.0635101795196533 1.0677716732025146 1.0720353722572327

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多