带圆角和多种颜色的进度条

Progress bar with round corners and multiple colors

我已经读过类似的问题 here and 如果你只想显示一个带圆角的进度条,这是很好的解决方案,但在我的例子中,我想显示由多个 "sub-progress" 颜色如下图所示:

到目前为止,我所做的是像这样编写一个 LinearGradient 实现 answer,但它的角落看起来是方形的。

如何获得这个进度?

经过一些研究我发现这个 class PaintDrawable which I can set as background of any View, and surprisingly I can set a Shape to this Drawable, so if I use the 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);
        }