Changing size of seekbar thumb

2019-01-11 01:12发布

I'm using a drawable for seekbar thumb with

android:thumb="@drawable/thumb"

How can i set the size of this thumb in dip unit? Because i use a style for seekbar like

<style name="CustomSeekBar">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:minHeight">8dip</item>
    <item name="android:maxHeight">8dip</item>
    <item name="android:thumbOffset">8dip</item>
</style> 

and i want to have the thumb with 12dip height and width.

6条回答
家丑人穷心不美
2楼-- · 2019-01-11 01:31

Create a shape of rectangle with height and width as you require. Use this shape as drawable for your thumb.

查看更多
在下西门庆
3楼-- · 2019-01-11 01:46

Another simple solution that worked for me was using scaleX and scaleY. The entire seekbar will be larger this way and not just the thumb.

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleX="2"
    android:scaleY="2" />
查看更多
趁早两清
4楼-- · 2019-01-11 01:47

From the sdk you can see this is the style for the basic SeekBar:

<style name="Widget.SeekBar">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
    <item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
    <item name="android:minHeight">20dip</item>
    <item name="android:maxHeight">20dip</item>
    <item name="android:thumb">@android:drawable/seek_thumb</item>
    <item name="android:thumbOffset">8dip</item>
    <item name="android:focusable">true</item>
</style>

With this as the thumb selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_pressed" />

    <item android:state_focused="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:state_selected="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:drawable="@drawable/seek_thumb_normal" />

</selector>

You should be able to use these as a base to replace the drawable which are currently being used (This looks like the stage you've got to).

These drawable can be different sizes on different screens based on drawable folder size bucket (drawable-hdpi, drawable-large-hdpi etc) or you can make different SeekBars look at different styles / drawable sizes like so:

<style name="SeekBar.Thumb.Smiley" parent="@android:style/Widget.SeekBar">
    <item name="android:thumb">@drawable/smiley</item>
<style>

<style name="SeekBar.Thumb.Smiley.Large" parent="@android:style/Widget.SeekBar">
    <item name="android:thumb">@drawable/smiley_large</item>
<style>

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    style="@style/SeekBar.Thumb.Smiley.Large"/>
查看更多
爷、活的狠高调
5楼-- · 2019-01-11 01:48

I was also looking for a way to do something similar and after looking at some other questions and putting all the answers together, I came up with a way to resize the Seekbar thumb in onCreate().

Here's my code from onCreate(), slightly adapted to your needs.

ViewTreeObserver vto = mySeekBar.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        Resources res = getResources();
        Drawable thumb = res.getDrawable(R.drawable.thumb);
        int h = mySeekBar.getMeasuredHeight() * 1.5; // 8 * 1.5 = 12
        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());
        mySeekBar.setThumb(newThumb);

        mySeekBar.getViewTreeObserver().removeOnPreDrawListener(this);

        return true;
        }
    });

Note that this works for resizing your thumb, but it might be clipped because it's bigger than the containing seekbar (not sure about that). If it does get clipped, you might need to create a taller seekbar and create your own seekbar background matching the size you want for the display.

Hope this helps!

查看更多
神经病院院长
6楼-- · 2019-01-11 01:48

I work like this:

    public class FlexibleThumbSeekbar extends SeekBar{

        public FlexibleThumbSeekbar (Context context) {
            super(context);
        }

        public FlexibleThumbSeekbar (Context context, AttributeSet attrs) {
            super(context, attrs);

            Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.thumb);
            Bitmap thumb=Bitmap.createBitmap(50,50, Bitmap.Config.ARGB_8888);
            Canvas canvas=new Canvas(thumb);
            canvas.drawBitmap(bitmap,new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),
                    new Rect(0,0,thumb.getWidth(),thumb.getHeight()),null);
            Drawable drawable = new BitmapDrawable(getResources(),thumb);
            setThumb(drawable);
        }
}

just a sample,in your real usage,you should define the width,height and picture of your thumb in a xml file.

查看更多
在下西门庆
7楼-- · 2019-01-11 01:54

The most flexible way to set thumb size for me is to create layered-drawable with shape as placeholder thumb_image.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape>
            <size
                android:height="40dp"
                android:width="40dp" />

            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:drawable="@drawable/scrubber_control_normal_holo"/>

</layer-list>

So basically changing the size of shape will stretch drawable, the shape is transparent so it won't be visible. Also the minimum size of such thumb is size of bitmap.

Here is example of layout:

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:thumb="@drawable/thumb_image" />

<SeekBar
    android:id="@+id/seekBar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Seek bar with different thumb size

查看更多
登录 后发表回答