Android ApiDemos example (55): Graphics-& gt; BitmapMesh

Source: Internet
Author: User

The drawBitmapMesh of Canvas is defined as follows:

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

It means that the image is drawn on the grid, which is more common. You can think of the canvas as a lattice cloth and draw the image on it. For a grid with evenly distributed mesh endpoints, there are meshWidth + 1 vertex horizontally and meshHeight + 1 endpoint vertically. Verts is a row-first array (two-dimensional arrays are represented by one-dimensional arrays and followed by columns ). The grid can be unevenly distributed. The parameters are defined as follows:

Bitmap: The image to be drawn on the grid.
MeshWidth: Number (number of columns) of the width direction of the Grid. If it is 0, no image is drawn.
MeshHeight: Number (inclusive) of the grid height direction. If it is 0, no image is drawn.
Verts: an array of (x, y) pairs, indicating the coordinates of the mesh vertices. At least (meshWidth + 1) * (meshHeight + 1) * 2 + meshOffset (x, y) coordinates.
VertOffset: Number of (x, y) pairs that are skipped in the verts array.
Colors: It can be empty. If it is not empty, the corresponding color value is defined for no vertex. At least (meshWidth + 1) * (meshHeight + 1) * 2 + meshOffset (x, y) coordinates.
ColorOffset: Number of (x, y) pairs that are skipped from the colors array.
Paint: It can be empty.
To better illustrate the concept of "Mesh", we need to slightly modify the code and add a green Mesh to the image (if we know OpenGL, here, the Mesh concept is similar to that of 3D drawing ). DrawImageWithGrid draw a grid on the image.

[Java]
Bitmap mBitmap1 = BitmapFactory. decodeResource (getResources (),
R. drawable. beach );

MBitmap = Bitmap. createBitmap (mBitmap1.getWidth (),
MBitmap1.getHeight (), Bitmap. Config. ARGB_8888 );
DrawImageWithGrid (mBitmap1 );

...
Private void drawImageWithGrid (Bitmap image ){
Canvas canvas = new Canvas (mBitmap );
Float w = mBitmap. getWidth ();
Float h = mBitmap. getHeight ();
Int xCount = (int) w/WIDTH;
Int yCount = (int) h/HEIGHT;
Paint paint = new Paint ();
Canvas. drawBitmap (image, 0, 0, paint );
Paint. setStyle (Paint. Style. STROKE );
Paint. setStrokeWidth (1 );
Paint. setColor (0x8000FF00 );
For (int I = 0; I <xCount; I ++ ){
For (int j = 0; j <yCount; j ++ ){
Canvas. drawRect (I * WIDTH, j * HEIGHT,
I * WIDTH + WIDTH, j * HEIGHT + HEIGHT, paint );
}
}
}
Bitmap mBitmap1 = BitmapFactory. decodeResource (getResources (),
R. drawable. beach );
 
MBitmap = Bitmap. createBitmap (mBitmap1.getWidth (),
MBitmap1.getHeight (), Bitmap. Config. ARGB_8888 );
DrawImageWithGrid (mBitmap1); www.2cto.com
 
...
Private void drawImageWithGrid (Bitmap image ){
Canvas canvas = new Canvas (mBitmap );
Float w = mBitmap. getWidth ();
Float h = mBitmap. getHeight ();
Int xCount = (int) w/WIDTH;
Int yCount = (int) h/HEIGHT;
Paint paint = new Paint ();
Canvas. drawBitmap (image, 0, 0, paint );
Paint. setStyle (Paint. Style. STROKE );
Paint. setStrokeWidth (1 );
Paint. setColor (0x8000FF00 );
For (int I = 0; I <xCount; I ++ ){
For (int j = 0; j <yCount; j ++ ){
Canvas. drawRect (I * WIDTH, j * HEIGHT,
I * WIDTH + WIDTH, j * HEIGHT + HEIGHT, paint );
}
}
}
By adjusting the coordinates of the grid, you can change the image. In this example, the effect is achieved, but when you click on the image, just as if you press your finger on a canvas a little (the cloth is soft ). In this example, the warp method is used to implement the warp canvas algorithm (the vertex coordinates of the Mesh are transformed without further research ).

 

 

Author: mapdigit
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.