在Android中使用canvas和bitmap,如何获取此图像? [英] Using canvas and bitmap in Android , how to get this image?
问题描述
我是android的新手.我正在尝试绘制此图像(匹配统计信息) ,然后用10%到100%的颜色填充图像.我做了很多尝试,这就是图片
I am new in android. I am trying to draw this image(match statistic) 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);
}
任何建议对我都会有很大帮助. 预先感谢.
Any Suggestion will be much helpful for me. Thanks in advance.
推荐答案
我将准备两个图像-完全填充和未填充(仅笔画).有了它们,将它们作为两个Bitmap
对象加载,然后像这样绘制:
I would prepare two images - fully filled and not filled (only stroke). Having that, load them as two Bitmap
objects and then draw like that:
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();
使用两幅概述和填充的图像,例如在下面.
Using two images, outlined and filled e.g. below.
上面的代码执行以下操作:
The code above does the following:
- 绘制轮廓.
- 应用剪辑(裁剪)区域.
- 在应用作物的情况下绘制填充形状.
- 根据需要删除剪辑,图像.
应用不同的片段大小,可以获得所需填充的百分比.例如
Applying different clip sizes, you can get the % of fill you require. e.g.
这篇关于在Android中使用canvas和bitmap,如何获取此图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!