值动画器只重复一次

2022-09-02 12:59:22

我试图在结束后重复一次。我正在将其与.由于某种原因,将完成,再次触发go,但是当它到达终点时,永远不会再调用第二次。ValueAnimatorSeekBarListViewValueAnimatoronAnimationEnd()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));
    }

该对象实际上是一个包含 a 和 a 的自定义对象,以下是相关位:seekBarSeekBarValueAnimator

    //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);
    }

我不明白为什么它没有重复两次以上。

我尝试过使用该属性并将其设置为,但这也没有帮助。RepeatINIFINITI


编辑:需要明确的是,我试图得到的是一个动画,它无限期地重复,每次都有不同的持续时间。


答案 1

我犯了将设置为无限的错误,这不起作用,这必须设置为:RepeatModeRepeatCount

valueAnimator.setRepeatCount(ValueAnimator.INFINITE);

答案 2

如果您使用的是 ,然后将其绑定AnimatorAnimator.AnimatorListener

该函数用于动画重复有限时间的情况,并且仅调用一次AnimatorListener.onAnimationEnd()

如果您的动画重复,对于次数,您应该改用该函数,该函数将在每次重复结束后每次动画重复时应用AnimatorListener.onAnimationRepeat()

据我所知,你需要的是,所以如果你只是移动你想要在每次重复后执行的代码从 到 ,这应该可以解决它onAnimationRepeat()onAnimationEnd()onAnimationRepeat()

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


推荐