How drawBitmapMesh works in android canvas

I want to draw a bitmap on a rectangle. I use the following values:

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

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

This conclusion alt text

I want to draw a bitmap on a rectangle surrounded by blue lines.

+4
source share
3 answers

All 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. Function Syntax:

 public void drawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint) 

A bitmap is the bitmap that you want to use. Now imagine a grid above a bitmap with meshWidth + 1 point along the lines of the image and meshHeight + 1 points to the columns of the bitmap. You specify these points or vertices in the verts variable. They are entered in a string format, which means that you enter vertices in vert from left to right for row 1, then from left to right for row 2, etc., 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 the vertex (x, y) with coordinates appropriately stacked over the bitmap. You define your vert array as:

 vert[0] = (*01).x; vert[1] = (*01).y; vert[2] = (*02).x; vert[3] = (*02).y; ... 

If you have to distribute these points evenly across the bitmap, then drawBitmapMesh will theoretically give the same result as the drawBitmap function. However, if you offset these vertices from their β€œnatural” position, then drawBitmapMesh will begin to stretch the bitmap according to the specification.

You can set the remaining arguments of the function to 0, null, 0, null, respectively, as it was in your example.

+7
source

If all you do is draw a rectangle, you do not need to use a grid at all. Using a grid will be more expensive.

+2
source

You do not initialize verts in the above code. It should do what you need if you are actually assigning values ​​to the verts elements (although, as Romain said, this is not the best way to complete the task).

+1
source

Source: https://habr.com/ru/post/1334434/


All Articles