动画进程的不均衡增长率



查看流动代码:

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

它的程序,使按钮的动画很容易。动画由几个部分组成(很重要(。有一个callback on_progress,用于收集时间数据和进度。该数据在程序结束时保存。我正在使用另一个脚本来按时间和进度构建情节,并得到这样的东西:在此处输入图像描述

正如您在动画进程的不同部分中看到的那样,增长不均衡。为什么会发生这种情况?如何修复?

这是一种奇怪的行为,但我认为原因是在动画的不同部分中使用了不同的时间间隔。我相信,这些间隔在不同相位变化的同时,也在kivy时钟的分辨率范围内。

我不知道为什么这些间隔会改变。我相信这是在kivy Clock下面的C代码中完成的。

Animation对象的+运算符生成AnimationSequence子类,这似乎触发了差异。一个变通方法是构建自己的序列,类似于这样:

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)

还有一个可能的解决方案-使用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.06351017951965331.06777167320251461.0720353722572327

最新更新