在 Android 中使用 canvas 和位图,如何获取此图像?

Using canvas and bitmap in Android , how to get this image?

我是 android 的新人。我正在尝试绘制此图像(匹配统计) and fill the image with color with 10% to 100% . I tried this much and this is image

这是代码

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();

使用两张图片,轮廓和填充,例如下面。

上面的代码执行以下操作:

  1. 勾画轮廓。
  2. 应用剪辑(裁剪)区域。
  3. 绘制已应用裁剪的填充形状。
  4. 根据需要删除剪辑、图像。

应用不同的剪辑大小,您可以获得您需要的填充百分比。例如