Mariusz
Mariusz

Reputation: 1875

Round corners in SeekBar for progress drawable

How can I add rounded corners to progress state of SeekBar which contains bitmap?

This progress bitmap is a PNG file that contains a pattern to be repeated, that's why I can't use nine patch for this state.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <nine-patch android:src="@drawable/background"/>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <nine-patch android:src="@drawable/secondary_progress"/>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <bitmap android:src="@drawable/progress" android:tileMode="repeat"/>
        </clip>
    </item>
</layer-list>

Desired look:

enter image description here

Upvotes: 0

Views: 3007

Answers (1)

pskink
pskink

Reputation: 24740

try this test code (add it in Activity#onCreate) and modify sb.xml to fit your needs:

    SeekBar sb = new SeekBar(this);
    setContentView(sb, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 128));
    LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.sb);
    float[] radii = {
            32, 32, 32, 32, 32, 32, 32, 32,
    };
    ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(radii, null, null));
    sd.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.chrome);
            return new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        }
    });
    Drawable cd = new ClipDrawable(sd, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    ld.setDrawableByLayerId(android.R.id.progress, cd);
    sb.setProgressDrawable(ld);

res/drawable/sb.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android    ">
<item android:id="@android:id/background">
    <shape>
        <solid android:color="#0a0"/>
        <corners android:radius="32px"/>
    </shape>
</item>
<item android:id="@android:id/progress"  android:drawable="@android:color/transparent">
</item>
</layer-list>        

Upvotes: 1

Related Questions