Cut a picture into multiple images in Android [go]

Source: Internet
Author: User

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
    1. package arui319.blog.csdn.net;     
    2.     
    3. import  android.graphics.bitmap;    
    4.     
    5. public CLASS IMAGEPIECE {    
    6.     
    7.     public int index = 0;    
    8.         
    9.      public bitmap bitmap = null;     
    10. }    


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
  1. Package arui319.blog.csdn.net;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import Android.graphics.Bitmap;
  5. Public class Imagesplitter {
  6. public static list<imagepiece> split (Bitmap Bitmap, int xpiece, int ypiece) {
  7. list<imagepiece> pieces = new Arraylist<imagepiece> (xpiece * ypiece);
  8. int width = bitmap.getwidth ();
  9. int height = bitmap.getheight ();
  10. int piecewidth = width/ 3;
  11. int pieceheight = height/ 3;
  12. For (int i = 0; i < ypiece; i++) {
  13. For (int j = 0; j < Xpiece; J + +) {
  14. Imagepiece piece = new Imagepiece ();
  15. Piece.index = j + i * xpiece;
  16. int xValue = j * Piecewidth;
  17. int yvalue = i * pieceheight;
  18. Piece.bitmap = Bitmap.createbitmap (Bitmap, XValue, Yvalue,
  19. Piecewidth, Pieceheight);
  20. Pieces.add (piece);
  21. }
  22. }
  23. return pieces;
  24. }
  25. }


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]

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.