我想制作带有分隔线的垂直进度条。但是,我无法这样做。 此代码适用于水平进度条,但是当我想使它像垂直进度条一样工作时,它不起作用。
任何帮助表示赞赏。
class ProgressDrawable extends Drawable {
private static final int NUM_RECTS = 10;
Paint mPaint = new Paint();
@Override
protected boolean onLevelChange(int level) {
invalidateSelf();
return true;
}
@Override
public void draw(Canvas canvas) {
int level = getLevel();
Rect b = getBounds();
float height = b.height();
for (int i = 0; i < NUM_RECTS; i++) {
float bottom = height * i / NUM_RECTS;
float top = bottom + 0.9f * height / NUM_RECTS;
mPaint.setColor((i + 1) * 10000 / NUM_RECTS <= level? 0xff888888 : 0xffbbbbbb);
// canvas.drawRect(left, b.top, right, b.bottom, mPaint);
//canvas.drawRect(left, b.top, right, b.bottom, mPaint);
canvas.drawRect(bottom, b.bottom, top, b.top, mPaint);
}
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
答案 0 :(得分:0)
我知道它已经很晚了,但如果有人想要找它,你可以在这里看到
private static final int NUM_RECTS = 10;
Paint mPaint = new Paint();
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
int level = getLevel();// It will give the level of progress(0 to 10,000)
Rect b = getBounds();
float height = b.height();
float width=b.width();
float x=40;
float y=0;
for (int i =0; i<NUM_RECTS; i++) {
if((i+1)*10000/NUM_RECTS>level)
{
mPaint.setColor(Color.GRAY);
}else{
mPaint.setColor(Color.GREEN);
}
canvas.drawRect(0,height-x,width,height-y, mPaint);
//canvas.drawCircle(width/2,height-y,30, mPaint);
x=x+50;
y=y+50;
}
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
protected boolean onLevelChange(int level) {
invalidateSelf();
return true;
}
}