Java ValueAnimator仅重复一次
我试图让Java ValueAnimator仅重复一次,java,android,animation,seekbar,Java,Android,Animation,Seekbar,我试图让ValueAnimator在结束后重复。我将它与列表视图中的SeekBar一起使用。由于某些原因,ValueAnimator将完成,再次触发onAnimationEnd()转到,但当它到达末尾时,将不会再次调用onAnimationEnd() @Override public View getContentView(int position, View convertView, ViewGroup parent) { ... setupTi
ValueAnimator
在结束后重复。我将它与列表视图中的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
属性并将其设置为INIFINITI
,但也没有任何帮助
编辑:要清楚,我想得到的是一个无限期重复的动画,每次都有不同的持续时间。如果您使用的是Animator
,然后将其与Animator.AnimatorListener
函数AnimatorListener.onAnimationEnd()
用于动画重复有限次的情况,并且只调用一次
如果动画重复多次,则应改用函数AnimatorListener.onAnimationRepeat()
,该函数将应用于每次重复结束后动画重复的时间
据我所知,您需要的是onAnimationRepeat()
,因此,如果您只是将每次重复后要执行的代码从onAnimationEnd()
移动到onAnimationRepeat()
,这应该可以解决问题
参考资料:我将RepeatMode
设置为无限时出错,但该错误不起作用,必须将其设置为RepeatCount
:
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
一些问题:什么是TylersContainer
?为什么每次执行setupTimerBar
时都要添加一个新的侦听器,而从不删除任何侦听器?为什么在setAnimationStartDelay
之后调用setAnimatorListenerAdapter
?您是如何验证只调用一次onAnimationEnd
的?TylersContainer
是一个对象,它包含一个视图和下一条总线到达的时间(显示在该视图中)。我每次都添加一个新的侦听器,因为在我发现的示例中就是这样做的,我也不知道你必须删除侦听器,我以为它们只是在完成后才被删除。setAnimationListenerAdapter
和setAnimationStartDelay
的顺序很简单。我验证了onAnimationEnd
仅被调用一次,并使用一些打印语句来记录cat。“setAnimationListenerAdapter和setAnimationStartDelay的顺序很简单。”我问,因为我不确定是否有可能在添加侦听器之前调用onAnimationEnd
。通常,您可以尝试使用android的调试功能逐步完成代码的执行。也许这会产生结果?多亏了一些线索,will doAs@dst指出,我想你需要重新安排setAnimationStartDelay()
和SetAnimatorListeneradAdapter()
,如果持续时间和开始延迟为0,你会遇到麻烦。