drawBitmapMesh如何在android画布中工作

kml*_*ckr 6 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);
Run Code Online (Sandbox Code Playgroud)

points数组是我的顶点数组.

我的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);
}
Run Code Online (Sandbox Code Playgroud)

输出就是这个 替代文字

我想在用蓝线包围的矩形上绘制位图.

Pix*_*xel 10

您可以在Android开发人员文档中找到理解DrawBitmapMesh函数所需的一切.请参阅Android开发者文档.

但是,我会用自己的话来解释.该函数的语法是:

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

位图显然是您要使用的位图.现在想象一下位图上的网格,沿着图像行的meshWidth + 1点,meshHeight + 1指向位图的列.您可以在verts变量中指定这些点或顶点.这些是以行主格式输入的,这意味着您在行中从左到右输入顶点,然后从左到右输入第2行,依此类推,即如果我们有4 x 4点,那么我们就有了一些东西像这样:

*01*02*03*04

*05*06*07*08

*09*10*11*12

*13*14*15*16

其中*n是顶点(x,y),坐标适当地放置在位图图像上.您可以将vert数组定义为:

vert[0] = (*01).x;
vert[1] = (*01).y;
vert[2] = (*02).x;
vert[3] = (*02).y;
...
Run Code Online (Sandbox Code Playgroud)

如果要在位图上均匀分布这些点,那么drawBitmapMesh理论上会提供与drawBitmap函数相同的输出.但是,如果将这些顶点移离其"自然"位置,则drawBitmapMesh将开始按照规范拉伸位图.

您可以将剩余的函数参数分别设置为0,null,0,null,就像在示例中所做的那样.


Rom*_*Guy 1

如果您所做的只是绘制一个矩形,则根本不需要使用网格。使用网格会更昂贵。