Search code examples
androidseekbar

Seekbar's thumb animation while dragging


I am creating a seekbar with custom thumb. So my thumb it layeredDrawable. So I want to resize my thumb image while dragging thumb.

I mean when I will drag my thumb... thumb size will increase and seekbar position should not be change.

Please help me and thanks in advance.


Solution

  • Changing the seek bars thumb size as you slide requires some code

    android:thumb="@drawable/round"
    

    Code

    private SeekBar seekBar;
    

    in OnCreate add

    seekBar = (SeekBar) findViewById(R.id.mySeekBarID);
    
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
                int progress = 0;
    
                @Override
    
                public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
    
                    progress = progresValue;
    
                    Resources res = getResources();
                    Drawable thumb = res.getDrawable(R.drawable.round);
                    int h = progress / 10;
                    int w = h;
                    Bitmap bmpOrg = ((BitmapDrawable) thumb).getBitmap();
                    Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOrg, w, h, true);
                    Drawable newThumb = new BitmapDrawable(res, bmpScaled);
                    newThumb.setBounds(0, 0, newThumb.getIntrinsicWidth(), newThumb.getIntrinsicHeight());
                    seekBar.setThumb(newThumb);
    
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
            });
    

    Tested and works as expected. ie as you move the thumb it gets smaller to the left and larger to the right

    This line here int h = progress / 10;` is what calculates the new thumb size as you slide so you will need to play around with some math to get the exact result you require