一旦拇指被拖到最后,搜索栏中的动画将进入初始点

时间:2014-08-18 06:26:23

标签: android seekbar

如何设置搜索栏的动画,以便将拇指拖动到搜索栏的最大值,拇指会自动返回初始点。我是一个初学者,所以如果可能的话,请给出详细的答案

sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onProgressChanged(SeekBar seekBar,
                                int progress, boolean fromUser) {

                            value = sb.getProgress();

                            if (value == 50) {
                                Drawable drawable = getResources().getDrawable(
                                        R.drawable.off);
                                sb.setThumb(drawable);

                            } 
                            ValueAnimator anim = ValueAnimator.ofInt(seekBar.getProgress(),0);
                            anim.setDuration(1000);
                            anim.addUpdateListener(new AnimatorUpdateListener() {
                                @Override
                                public void onAnimationUpdate(ValueAnimator animation) {
                                int animProgress = (Integer) animation.getAnimatedValue();
                                sb.setProgress(animProgress);
                                }
                            });
                            anim.start();

                        }
                    });

1 个答案:

答案 0 :(得分:2)

我已经得到了我的问题的答案。如果有人需要它,那就是....

SeekBar sb;
ValueAnimator anim;
int value;
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar,
            int progress, boolean fromUser) {
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        value = sb.getProgress();
        ValueAnimator anim = ValueAnimator.ofInt(value,
                sb.getMax());
        anim.setDuration(1000);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(
                    ValueAnimator animation) {
                value = (Integer) animation
                        .getAnimatedValue();
                sb.setProgress(value);
            }
        });
        anim.start();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ValueAnimator anim = ValueAnimator.ofInt(0,
                        sb.getMax());
                anim.setDuration(1000);
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(
                            ValueAnimator animation) {
                        value = (Integer) animation
                                .getAnimatedValue();
                        value = 100 - value;
                        sb.setProgress(value);
                    }
                });
                anim.start();
            }
        }, 1200);
    }
});