【问题标题】:Round corners in SeekBar for progress drawableSeekBar 中的圆角用于绘制进度
【发布时间】:2016-09-14 10:57:42
【问题描述】:

如何为包含位图的 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>

想要的外观:

【问题讨论】:

  • 发布一张想要的图片
  • 我添加了图片
  • 而不是&lt;bitmap android:src="@drawable/progress" android:tileMode="repeat"/&gt; 使用“圆形矩形”ShapeDrawable 并设置它的ShapeDrawable.ShaderFactory 以便它返回BitmapShader,我知道这不是简单的解决方案,但我担心唯一简单的解决方案是自定义 Drawable 类,例如 this
  • 谢谢@pskink,我去看看。
  • here 你有如何在 xml 和 java 代码中做到这一点(你可以删除 sd.getPaint().setColor(Color.RED); 行,因为它用于测试)

标签: android android-bitmap android-seekbar


【解决方案1】:

试试这个测试代码(添加到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/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>        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-19
    • 2015-10-26
    • 2017-08-16
    • 2015-12-01
    • 1970-01-01
    • 2014-12-10
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多