Search code examples
androidseekbar

Animation in seekbar to go to initial point once thumb is dragged to its end


how to animate the seekbar so that it one drags the thumb to seekbar max value the thumb automatically goes back to initial point.I am a begginer so plz if possible give a elaborated answer

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();

                        }
                    });

Solution

  • I have got the answer to my question. If anyone one need it, then it is....

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