ValueAnimator只重复一次

时间:2013-12-18 02:46:46

标签: java android animation seekbar

我试图让ValueAnimator在结束后重复。我在SeekBar中使用ListView。出于某种原因,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对象实际上是一个包含SeekBarValueAnimator的自定义对象,以下是相关位:

    //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,但这也无济于事。


编辑:要清楚,我想要的是一个无限期重复的动画,每次都有不同的持续时间。

2 个答案:

答案 0 :(得分:14)

我犯了将RepeatMode设置为无效而无效的错误,必须设置为RepeatCount

valueAnimator.setRepeatCount(ValueAnimator.INFINITE);

答案 1 :(得分:1)

如果您使用Animator,然后将其与Animator.AnimatorListener

绑定

函数AnimatorListener.onAnimationEnd()用于动画重复有限时间,仅调用一次

如果你的动画重复了多次,你应该使用函数AnimatorListener.onAnimationRepeat(),这将在每次重复结束后每次动画重复时使用

根据我的理解,您需要的是onAnimationRepeat(),因此,如果您只是将每次重复后要执行的代码从onAnimationEnd()移动到onAnimationRepeat(),则应该修复它

参考:http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html