DrawBitmapMesh 如何在 Android Canvas 中工作
我想在矩形上绘制位图。我使用以下值:
this.meshWidth = 1;
this.meshHeight = 1;
this.verts = new float[8];
this.points[0].x = (float)(this.getWidth()/4);
this.points[0].y = (float)(this.getHeight()/4);
this.points[1].x = (float)(this.points[0].x+this.getWidth()/2);
this.points[1].y = (float)(this.points[0].y);
this.points[2].x = (float)(this.points[0].x);
this.points[2].y = (float)(this.points[0].y+this.getHeight()/2);
this.points[3].x = (float)(this.points[1].x);
this.points[3].y = (float)(this.points[2].y);
点数组是我的顶点数组。
我的 onDraw 方法的
public void onDraw(Canvas canvas){
//canvas.drawBitmap(bitmap, 20,20, null);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawLine(this.points[0].x, this.points[0].y, this.points[1].x, this.points[1].y, paint);
canvas.drawLine(this.points[1].x, this.points[1].y, this.points[3].x, this.points[3].y, paint);
canvas.drawLine(this.points[3].x, this.points[3].y, this.points[2].x, this.points[2].y, paint);
canvas.drawLine(this.points[2].x, this.points[2].y, this.points[0].x, this.points[0].y, paint);
canvas.drawBitmapMesh(this.bitmap, meshWidth, meshHeight, verts, 0, null, 0, null);
}
输出是这样的
我想在用蓝线包围的矩形上绘制位图..
I want to draw a bitmap on a rectangle. I use the values below:
this.meshWidth = 1;
this.meshHeight = 1;
this.verts = new float[8];
this.points[0].x = (float)(this.getWidth()/4);
this.points[0].y = (float)(this.getHeight()/4);
this.points[1].x = (float)(this.points[0].x+this.getWidth()/2);
this.points[1].y = (float)(this.points[0].y);
this.points[2].x = (float)(this.points[0].x);
this.points[2].y = (float)(this.points[0].y+this.getHeight()/2);
this.points[3].x = (float)(this.points[1].x);
this.points[3].y = (float)(this.points[2].y);
points array is my vertex array.
my onDraw method
public void onDraw(Canvas canvas){
//canvas.drawBitmap(bitmap, 20,20, null);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawLine(this.points[0].x, this.points[0].y, this.points[1].x, this.points[1].y, paint);
canvas.drawLine(this.points[1].x, this.points[1].y, this.points[3].x, this.points[3].y, paint);
canvas.drawLine(this.points[3].x, this.points[3].y, this.points[2].x, this.points[2].y, paint);
canvas.drawLine(this.points[2].x, this.points[2].y, this.points[0].x, this.points[0].y, paint);
canvas.drawBitmapMesh(this.bitmap, meshWidth, meshHeight, verts, 0, null, 0, null);
}
The output is this
I want to draw bitmap on rectangle surrounded with blue lines..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
了解 DrawBitmapMesh 函数所需的所有内容都可以在 Android 开发人员文档中找到。请参阅Android 开发者文档。
不过,我在这里用我自己的话来解释一下。该函数的语法是:
位图显然是您要使用的位图。现在想象一下位图上的网格,其中 meshWidth+1 点沿着图像的行,meshHeight+1 点沿着位图的列。您可以在 verts 变量中指定这些点或顶点。这些是以行优先格式输入的,这意味着您在 vert 中从左到右输入第 1 行的顶点,然后从左到右输入第 2 行的顶点,依此类推,即如果我们有 4 x 4 个点,那么我们就有一些东西像这样:
*01 *02 *03 *04
*05 *06 *07 *08
*09 *10 *11 *12
*13 *14 *15 *16
其中 *n 是坐标适当放置的顶点(x,y)位图图像上。您可以将 vert 数组定义为:
如果要将这些点均匀分布在位图上,那么理论上,drawBitmapMesh 会提供与 drawBitmap 函数相同的输出。但是,如果您将这些顶点移离其“自然”位置,则 drawBitmapMesh 将开始按照规范拉伸位图。
您可以将剩余的函数参数分别设置为 0、null、0、null,就像您在示例中所做的那样。
Everything you need to understand the DrawBitmapMesh function can be found in the Android developer documentation. See Android developer documentation.
However, I will explain it here in my own words. The syntax for the function is:
The bitmap is clearly the bitmap you want to use. Now imagine a grid over the bitmap with meshWidth+1 points along the rows of the image and meshHeight+1 points down the columns of the bitmap. You specify these points, or vertices, in the verts variable. These are entered in row-major format, meaning that you enter the vertices in vert from left to right for row 1, then left to right for row 2, and so on, i.e. if we have 4 x 4 points, then we have something like this:
*01 *02 *03 *04
*05 *06 *07 *08
*09 *10 *11 *12
*13 *14 *15 *16
where *n is a vertex (x, y) with coordinates laid appropriately over the bitmap image. You would define your vert array as:
If you were to distribute these points uniformly across the bitmap then the drawBitmapMesh would in theory give the same output as the drawBitmap function. However, if you displace these vertices away from their "natural" position, then the drawBitmapMesh will begin to stretch the bitmap as per specification.
You can set the remaining function arguments to 0, null, 0, null respectively as you have done in your example.
如果您所做的只是绘制一个矩形,则根本不需要使用网格。使用网格会更昂贵。
If all you are doing is draw a rectangle, you don't need to use a mesh at all. It will be more expensive to use the mesh.
您没有在上面的代码中初始化顶点。如果您实际为 verts 元素分配值,它应该执行您想要的操作(尽管,正如 Romain 所说,这不是完成任务的最佳方法)。
You're not initializing verts in the code above. It should do what you want if you actually assign values to the verts elements (though, as Romain said, it's not the best way to accomplish the task).