Java 在Android中使用ValueAnimator重置Seekbar

Java 在Android中使用ValueAnimator重置Seekbar,java,android,animation,Java,Android,Animation,我想在按下按钮的同时,让一个按钮将搜索杆的进度缓慢地恢复到一个特定的值。比如说,seekbar的当前进度是150,它的标准值是100,我想在按下按钮时将进度减少到100,在seekbar上移动1个单位需要0.1秒。 我正试图使用ValueAnimator来实现这一点 main_seekbar_speed_reset.setOnTouchListener(new View.OnTouchListener() { @Override public boolea

我想在按下按钮的同时,让一个按钮将搜索杆的进度缓慢地恢复到一个特定的值。比如说,seekbar的当前进度是150,它的标准值是100,我想在按下按钮时将进度减少到100,在seekbar上移动1个单位需要0.1秒。 我正试图使用ValueAnimator来实现这一点

    main_seekbar_speed_reset.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

             ValueAnimator animator = ValueAnimator.ofInt(main_seekbar_speed.getProgress(), 100);;


            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    animator = ValueAnimator.ofInt(main_seekbar_speed.getProgress(), 100);
                    animator.setDuration(Math.abs(main_seekbar_speed.getProgress() - 100)*100);
                    animator.start();
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator valueAnimator) {
                            main_seekbar_speed.setProgress((int)valueAnimator.getAnimatedValue());
                        }
                    });
                    case MotionEvent.ACTION_UP:
                        animator.end();
            }

            return false;
        }
    });
但是这个代码会立即重置它

编辑

我忘了添加
break到每个案例的结尾。
这意味着
switch
已经完成了每一个案例,因此始终为last调用
animator.end()
,每次都将
seekbar
的进度设置为最终动画值(100)。 另外,
animator.end()
意味着动画师立即结束;它会跳到动画结束时应为的最后一个值

MotionEvent.ACTION\u DOWN
MotionEvent.ACTION\u UP
都创建了一个新的ValueAnimator,因此释放按钮不会影响触摸按钮时创建的
ValueAnimator
。它应该在侦听器外部声明

因此,工作守则:

`

    final ValueAnimator animator = ValueAnimator.ofInt(main_seekbar_speed.getProgress(), 100);
    main_seekbar_speed_reset.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    animator.setIntValues(main_seekbar_speed.getProgress(), 100);
                    animator.setDuration(Math.abs(main_seekbar_speed.getProgress() - 100)*100);
                    animator.start();
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator valueAnimator) {
                            main_seekbar_speed.setProgress((int)valueAnimator.getAnimatedValue());
                        }
                    });
                    break;
                case MotionEvent.ACTION_UP:
                    animator.pause();
                    break;
            }

            return false;
        }
    });`