首先建立一个小块图片的类,保存切割后的图片
class="brush:java;collapse:true;;gutter:true;">public class ImagePiece { private int index; private Bitmap bitmap; public ImagePiece() {} public ImagePiece(int index,Bitmap bitmap) { this.index=index; this.bitmap=bitmap; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public Bitmap getBitmap() { return bitmap; } public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; } @Override public String toString() { return "ImagePiece [index=" + index + ", bitmap=" + bitmap + ", toString()=" + super.toString() + "]"; } }
切割图片
/** * 传入一个bitmap * @param bitmap * @param piece * @return List<ImagePiece> */ public static List<ImagePiece> splitImage(Bitmap bitmap,int piece) { List<ImagePiece> imagePieces=new ArrayList<ImagePiece>(); int width=bitmap.getWidth(); int height=bitmap.getHeight(); int pieceWidth=Math.min(width, height)/piece; for(int i=0;i<piece;i++) { for(int j=0;j<piece;j++) { ImagePiece imagePiece=new ImagePiece(); imagePiece.setIndex(j+i*piece); int x=j*pieceWidth; int y=i*pieceWidth; Log.i("x", x+""); Log.i("y", y+""); imagePiece.setBitmap(Bitmap.createBitmap(bitmap, x, y, pieceWidth, pieceWidth)); imagePieces.add(imagePiece); } } return imagePieces; }