【发布时间】:2017-11-21 20:07:18
【问题描述】:
我已经阅读了类似的问题here 和here,如果您只想显示一个带圆角的进度条,这是很好的解决方案,但在我的情况下,我想显示多个“子进度”颜色如下图所示:
到目前为止,我所做的是编写一个类似 answer 的 LinearGradient 实现,但它看起来是方形的。
我怎样才能获得这个进度?
【问题讨论】:
标签: android android-progressbar
我已经阅读了类似的问题here 和here,如果您只想显示一个带圆角的进度条,这是很好的解决方案,但在我的情况下,我想显示多个“子进度”颜色如下图所示:
到目前为止,我所做的是编写一个类似 answer 的 LinearGradient 实现,但它看起来是方形的。
我怎样才能获得这个进度?
【问题讨论】:
标签: android android-progressbar
经过一番研究,我发现这个类 PaintDrawable 可以设置为任何视图的背景,令人惊讶的是,我可以为这个 Drawable 设置一个 Shape,所以如果我使用 RoudRectShape,它看起来像我想要的那样圆润是的,我的最终代码如下:
// mRatios is a View
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int i, int i1) {
LinearGradient lg = new LinearGradient(0, 0, mRatios.getWidth(), 0,
barColorsArray,
barRatiosArray,
Shader.TileMode.REPEAT);
return lg;
}
};
PaintDrawable paintDrawable = new PaintDrawable();
// not sure how to calculate the values here, but it works with these constants in my case
paintDrawable.setShape(new RoundRectShape(new float[]{100,100,100,100,100,100,100,100}, null, null));
paintDrawable.setShaderFactory(sf);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mRatios.setBackgroundDrawable(paintDrawable);
} else {
mRatios.setBackground(paintDrawable);
}
【讨论】: