There is a scene where we want to cut a picture into multiple images. For example, we are in the development of a puzzle game, we must first cut the image.
The following is a packaged two class, you can achieve the cutting of the picture. For reference and learning only.
One is the Imagepiece class, which holds a bitmap object and an int variable that identifies the sequential index of the picture.
Java code
- package arui319.blog.csdn.net;
-
- import android.graphics.bitmap;
-
- public CLASS IMAGEPIECE {    
-
- public int index = 0;    
-
- public bitmap bitmap = null;
- }
One is the Imagesplitter class, there is a static method split, the incoming parameter is the bitmap object to be cut, and the number of cut slices in both horizontal and vertical direction. For example, the incoming is 3, 3, the vertical to all cut into 3 pieces, will eventually cut the entire picture into 3x3=9 tablets.
Java code
- Package arui319.blog.csdn.net;
- Import java.util.ArrayList;
- Import java.util.List;
- Import Android.graphics.Bitmap;
- Public class Imagesplitter {
- public static list<imagepiece> split (Bitmap Bitmap, int xpiece, int ypiece) {
- list<imagepiece> pieces = new Arraylist<imagepiece> (xpiece * ypiece);
- int width = bitmap.getwidth ();
- int height = bitmap.getheight ();
- int piecewidth = width/ 3;
- int pieceheight = height/ 3;
- For (int i = 0; i < ypiece; i++) {
- For (int j = 0; j < Xpiece; J + +) {
- Imagepiece piece = new Imagepiece ();
- Piece.index = j + i * xpiece;
- int xValue = j * Piecewidth;
- int yvalue = i * pieceheight;
- Piece.bitmap = Bitmap.createbitmap (Bitmap, XValue, Yvalue,
- Piecewidth, Pieceheight);
- Pieces.add (piece);
- }
- }
- return pieces;
- }
- }
Here the cutting, mainly using the Bitmap object CreateBitmap method, no longer do a specific description.
Cut a picture into multiple images in Android [go]