位图的常用方法:
public static Bitmap createBitmap(Bitmap src) ——以src为原图生成不可变得新图像
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
int dstHeight, boolean filter)——以src为原图,创建新的图像,指定新图像的高宽以及是否可变
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。
注意:前三种方法的width不可以 >= source.getWidth();高类同
这些方法也可以称作为图像的切割,按照 x , y, width,height,的角标来切割,当然要切的尺寸就不能大于原图的宽高了。
public static Bitmap createBitmap(int width, int height, Config config)——创建指定格式、大小的位图
另外一种bitmap的静态方法如下:
//改变图片的尺寸
public static Bitmap changePicSize(Bitmap bitmap, float width, float height) {
Matrix matrix = new Matrix();
matrix.postScale(width / bitmap.getWidth(), height / bitmap.getHeight());
//对bitmap的尺寸横、纵 都扩大指定的倍数,倍数由matrix封装
//前四个参数决定着新图像由原图像那部分决定,第四个是matrix,第六个是新图像的宽高是否可变。
Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bm;
}
这些是拼图的部分代码:
/**
* 照片碎片加入集合
* @param bitmapList 小位图的集合
* @param itemList item的集合
* @param type x/y方向上切割的份数
*/
public static Bitmap add2List(Bitmap bitmap, int type, List<Bitmap> bitmapList, List<ItemBean> itemList, Context context) {
int smallBitmapWidth = bitmap.getWidth() / type;//每个小位图的尺寸
int smallBitmapHeight = bitmap.getHeight() / type;
Log.d("ImageUtil","smallBitmapWidth00"+ smallBitmapWidth + "smallBitmapHeight00"+smallBitmapHeight);
for (int i = 1; i <= type; i++) {
for (int j = 1; j <= type; j++) {
Bitmap smallBitmap = Bitmap.createBitmap(bitmap, (i - 1) * smallBitmapWidth, (j - 1) * smallBitmapHeight,
smallBitmapWidth, smallBitmapHeight);
bitmapList.add(smallBitmap);//将bitmap加入集合
itemList.add(new ItemBean((i - 1) * type + j,
(i - 1) * type + j, smallBitmap));//将item对象加入集合
}
}
//得到最后一个小位图
bitmap = bitmapList.get(type * type - 1);
bitmapList.remove(type * type - 1);
itemList.remove(type * type - 1);
//创建最后一个小位图
Bitmap lastbitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.lastsmallbit);
Bitmap changeLast = changePicSize(lastbitmap,smallBitmapWidth,smallBitmapHeight);//对最后一张小位图进行缩放处理
bitmapList.add(changeLast);
ItemBean itemBean = new ItemBean(type * type,type * type,changeLast);
itemList.add(itemBean);
mBlankItemBean = itemBean;
Log.d("ImageUtil"," "+ itemBean.getmBitmapId());
return bitmap;//切割掉的位图
}