/** * 圖片切割方法 * @param bitmap 圖片 * @param xPiece 行 * @param yPiece 列 * @return */ 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; }
ImagePiece代碼
/** * 此類保存了一個Bitmap對象和一個標識圖片的順序索引的int變量 * * @author lenovo * */ public class ImagePiece { public int index = 0; public Bitmap bitmap = null; }