簡體   English   中英

在Android中使用canvas和bitmap,如何獲取此圖像?

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

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

使用兩個圖像,概述和填充,例如下面。

在此輸入圖像描述 在此輸入圖像描述

上面的代碼執行以下操作:

  1. 畫輪廓。
  2. 應用剪輯(裁剪)區域。
  3. 繪制填充形狀與作物應用。
  4. 根據需要刪除剪輯,圖像。

在此輸入圖像描述

應用不同的剪輯大小,您可以獲得所需的填充百分比。 例如

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM