Android 将图片平均切割成多张小片

public class ImageSplitter
{
    /**
     * 将图片切成 , piece *piece
     * 
     * @param bitmap
     * @param piece
     * @return
     */
    public static List<ImagePiece> split(Bitmap bitmap, int piece)
    {

        List<ImagePiece> pieces = new ArrayList<ImagePiece>(piece * piece);

        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.index = j + i * piece;
                
                Log.e("TAG", "imagePiece.index" + (j + i * piece));
                
                int xValue = j * pieceWidth;
                int yValue = i * pieceWidth;
                
                imagePiece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,
                        pieceWidth, pieceWidth);
                pieces.add(imagePiece);
            }
        }
        return pieces;
    }
}

 

posted on 2015-08-05 17:04  2015xbx  阅读(596)  评论(0编辑  收藏  举报

导航