如何drawBitmapMesh在Android画布中工作 [英] How drawBitmapMesh works in android canvas

查看:185
本文介绍了如何drawBitmapMesh在Android画布中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在矩形上绘制位图。我使用以下值:

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数组是我的顶点数组。

points array is my vertex array.

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

输出结果为

The output is this

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

I want to draw bitmap on rectangle surrounded with blue lines..

推荐答案

需要了解DrawBitmapMesh函数的所有内容都可以在Android开发者文档中找到。请参阅 Android开发人员文件

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:

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

位图显然是你想要的位图使用。现在想象一个网格的位图,meshWidth + 1点沿着图像的行和meshHeight + 1点向下的位图的列。在verts变量中指定这些点或顶点。这些以行主格式输入,意味着您输入顶点从左到右的行1,然后从左到右的行2,等等,如果我们有4×4点,那么我们有东西像这样:

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

*01 *02 *03 *04

* 05 * 06 * 07 * 08

*05 *06 *07 *08

* 09 * 10 * 11 * 12

*09 *10 *11 *12

* 13 * 14 * 15 * 16

*13 *14 *15 *16

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

where *n is a vertex (x, y) with coordinates laid appropriately over the bitmap image. You would define your vert array as:

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

如果您要将这些点均匀分布在位图中,drawBitmapMesh理论上将给出与drawBitmap函数相同的输出。但是,如果你将这些顶点从它们的自然位置移开,那么drawBitmapMesh将根据规范开始拉伸位图。

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.

你可以设置剩余的函数参数到0,null,0,null,就像你在你的例子中做的那样。

You can set the remaining function arguments to 0, null, 0, null respectively as you have done in your example.

这篇关于如何drawBitmapMesh在Android画布中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆