自定义进度条具有不同的高度

时间:2015-08-20 06:45:49

标签: android android-view android-progressbar

任何人都可以帮我创建一个像这样的样式的进度条:

enter image description here

我知道这是一个常见问题,可以找到解决方案,但我没有看到进度条比其他部分更进一步。

有人可以帮我创建吗?

1 个答案:

答案 0 :(得分:0)

所以在@pskink的代码的帮助下,我已经设法为这个问题创建了一个解决方案。所以这是代码,我希望将来可以帮助某人!

public class ProgressDrawable extends Drawable {
    private final int mForeground;
    private final int mBackground;
    private final Paint mPaint = new Paint();
    private final RectF mSegment = new RectF();

    public ProgressDrawable(int fgColor, int bgColor) {
        mForeground = fgColor;
        mBackground = bgColor;
    }

    @Override
    protected boolean onLevelChange(int level) {
        invalidateSelf();
        return true;
    }

    @Override
    public void draw(Canvas canvas) {
        float level = getLevel() / 10000f;
        Rect b = getBounds();

        float gapWidth = b.height();
        float segmentWidth = b.width();
        mSegment.set(0, 0, segmentWidth * level, b.height());
        mPaint.setColor(mForeground);

        mSegment.set(0, b.height() / 3, segmentWidth, b.height() - (b.height() / 3));
        mPaint.setColor(mBackground);
        canvas.drawRect(mSegment.left, mSegment.top, mSegment.right, mSegment.bottom, mPaint);
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public int getIntrinsicHeight() {
        return 3;
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

和用法:

Drawable d = new ProgressDrawable(getActivity().getResources().getColor(R.color.toolbar_text_blue), getActivity().getResources().getColor(R.color.toolbar_text_blue));
                    dataProgress.setProgressDrawable(d);
                    dataProgress.setProgress(percent);