SeekBar中的圆角可绘制进度

时间:2016-09-14 10:57:42

标签: android android-bitmap android-seekbar

如何为包含位图的SeekBar的进度状态添加圆角?

此进度位图是一个PNG文件,其中包含要重复的模式,这就是为什么我不能在此状态下使用九个补丁的原因。

<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>

期望的样子:

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试此测试代码(在Activity#onCreate中添加)并修改sb.xml以满足您的需求:

    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 /抽拉/ 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>