答案 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);