How to cut an image into multiple images in Android

Source: Internet
Author: User

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.

Related Article

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.