The following are two encapsulated classes for image cutting. For reference and learning only.
One is the ImagePiece class, which stores a Bitmap object and an int variable that identifies the ordered index of the image.
Copy codeThe Code is as follows: 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, which has a static split method. The input parameter is the Bitmap object to be cut, and the number of horizontal and vertical slices. For example, if the input value is 3 or 3, the image is cut to 3X3 = 9 in the horizontal and vertical directions.Copy codeThe Code is as follows: 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 cut mainly uses the createBitmap method of the Bitmap object, which is not described in detail.