码迷,mamicode.com
首页 > 其他好文 > 详细

图片分割

时间:2015-03-11 10:40:32      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

首先建立一个小块图片的类,保存切割后的图片

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;
	}

  

图片分割

标签:

原文地址:http://www.cnblogs.com/feng290802455/p/4329002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!