我是android新手。我正在尝试绘制此图像(匹配统计信息)
并用10%到100%的颜色填充图像。我做了很多尝试,这是图片
这是代码
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 100, 100, paint);
paint.setStrokeWidth(0);
paint.setColor(Color.GRAY);
canvas.drawRect(33, 60, 97, 97, paint);
paint.setColor(Color.WHITE);
canvas.drawRect(33, 33, 97, 60, paint);
}
任何建议对我都会有很大的帮助。
提前致谢。
最佳答案
我会准备两个图像-完全填充和未填充(仅笔画)。有了它们,将它们作为两个Bitmap
对象加载,然后像这样绘制:
float fillProgress = 0.1f; // let's say image is 10% filled
canvas.drawBitmap(onlyStroke, 0f, 0f, null); // draw just stroke first
canvas.save();
canvas.clipRect(
0f, // left
getHeight() - fillProgress * getHeight(), // top
getWidth(), // right
getHeight() // bottom
);
canvas.drawBitmap(filled, 0f, 0f, null); // region of filled image specified by clipRect will now be drawn on top of onlyStroke image
canvas.restore();
使用两幅轮廓和填充图像,例如以下。
上面的代码执行以下操作:
应用不同的 fragment 大小,您可以获得所需的填充百分比。例如