我正在尝试让一个 ValueAnimator
在结束后重复执行。我在 ListView
中使用它与一个 SeekBar
配合使用。奇怪的是,ValueAnimator
会完成,触发 onAnimationEnd()
然后再次开始,但当它到达结尾时,onAnimationEnd()
不会第二次被调用。
@Override
public View getContentView(int position, View convertView, ViewGroup parent) {
...
setupTimerBar(t);
...
}
private AnimatorListenerAdapter generateNewAnimatorListenerAdapter(final TylersContainer t) {
return new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
setupTimerBar(t);
}
};
}
private void setupTimerBar(TylersContainer t)
{
View view = t.getView();
BusTime arrivalTime = t.getBusTime();
int minutes = BusTime.differenceInMiuntes(arrivalTime, BusTime.now());
long milliseconds = minutes * 60 * 1000;
final TimerBar seekBar = (TimerBar) view.findViewById(R.id.SeekBar);
int progress = Utility.setProgress(arrivalTime, seekBar.getMax());
long duration = Utility.setAnimationDuration(progress);
seekBar.setProgress(progress);
seekBar.setAnimationDuration(duration);
seekBar.setAnimationStartDelay(milliseconds);
seekBar.setAnimatorListenerAdapter(generateNewAnimatorListenerAdapter(t));
}
seekBar
对象实际上是一个自定义对象,其中包含一个SeekBar
和一个ValueAnimator
,以下是相关部分:
//Constructor
public TimerBar(Context context) {
super(context);
startTime = Calendar.getInstance();
valueAnimator = ValueAnimator.ofInt(0, getMax());
//Override the update to set this object progress to the animation's value
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animProgress = (Integer) animation.getAnimatedValue();
setProgress(animProgress);
}
});
}
//Specify the start time by passing a long, representing the delay in milliseconds
public void setAnimationStartDelay(long milliseconds){
//Set the delay (if need be) and start the counter
if(milliseconds > 0)
valueAnimator.setStartDelay(milliseconds);
valueAnimator.setIntValues(this.getProgress(), this.getMax());
valueAnimator.start();
}
//Set the duration of the animation
public void setAnimationDuration(long duration){
valueAnimator.setDuration(duration);
}
public void setAnimatorListenerAdapter(AnimatorListenerAdapter ala){
valueAnimator.addListener(ala);
}
我不明白为什么它不能重复超过两次。
我尝试使用 Repeat
属性并将其设置为 INFINITE
,但这也没有帮助。
编辑:明确一下,我想要的是一个无限重复的动画,每次持续时间都不同。
TylersContainer
是什么?为什么每次执行setupTimerBar
时都要添加新的监听器,却从未删除任何监听器?为什么在setAnimationStartDelay
之后调用setAnimatorListenerAdapter
?你如何验证onAnimationEnd
只被调用了一次? - dstTylersContainer
是一个对象,它包含一个视图和下一班公交车到达时间(显示在该视图中)。我每次都添加了一个新的监听器,因为这是我找到的示例中的做法,而且我也不知道你需要删除监听器,我以为它们只会在完成后被删除。setAnimationListenerAdapter
和setAnimationStartDelay
的顺序是微不足道的。我通过一些打印语句来记录onAnimationEnd
只被调用了一次。 - TyleronAnimationEnd
。通常,您可以尝试使用Android的调试功能逐步执行代码的执行。也许这会产生结果? - dstsetAnimationStartDelay()
和setAnimatorListenerAdapter()
的顺序,如果持续时间和启动延迟为0,您将会遇到麻烦。 - M-Wajeeh