得到一个BitMap对象

来源:互联网 发布:热电材料 知乎 编辑:程序博客网 时间:2024/05/22 02:04


2.根据图片的路径的得到一个Bitmap对象(注意要在AndroidMainfest.xm中加入权限)

public static Bitmap optimizeBitmap(String pathName, int maxWidth,int maxHeight) {Bitmap result = null;// 图片配置对象,该对象可以配置图片加载的像素获取个数BitmapFactory.Options options = new BitmapFactory.Options();// 表示加载图像的原始宽高options.inJustDecodeBounds = true;result = BitmapFactory.decodeFile(pathName, options);// Math.ceil表示获取与它最近的整数(向上取值 如:4.1->5 4.9->5)int widthRatio = (int) Math.ceil(options.outWidth / maxWidth);int heightRatio = (int) Math.ceil(options.outHeight / maxHeight);// 设置最终加载的像素比例,表示最终显示的像素个数为总个数的if (widthRatio > 1 || heightRatio > 1) {if (widthRatio > heightRatio) {options.inSampleSize = widthRatio;} else {options.inSampleSize = heightRatio;}}// 解码像素的模式,在该模式下可以直接按照option的配置取出像素点options.inJustDecodeBounds = false;result = BitmapFactory.decodeFile(pathName, options);return result;}

3.从资源中得到一个Bitmap对象

public static Bitmap optimizeBitmap(Resources resources, int drawableID,int maxWidth, int maxHeight) {Bitmap result = null;// 图片配置对象,该对象可以配置图片加载的像素获取个数BitmapFactory.Options options = new BitmapFactory.Options();// 表示加载图像的原始宽高options.inJustDecodeBounds = true;result = BitmapFactory.decodeResource(resources, drawableID, options);// Math.ceil表示获取与它最近的整数(向上取值 如:4.1->5 4.9->5)int widthRatio = (int) Math.ceil(options.outWidth / maxWidth);int heightRatio = (int) Math.ceil(options.outHeight / maxHeight);// 设置最终加载的像素比例,表示最终显示的像素个数为总个数的if (widthRatio > 1 || heightRatio > 1) {if (widthRatio > heightRatio) {options.inSampleSize = widthRatio;} else {options.inSampleSize = heightRatio;}}// 解码像素的模式,在该模式下可以直接按照option的配置取出像素点options.inJustDecodeBounds = false;result = BitmapFactory.decodeResource(resources, drawableID, options);return result;}



4.简单的得到一个BitMap对象

/* 从资源文件中装载图片 */// getResources()->得到Resources// getDrawable()->得到资源中的Drawable对象,参数为资源索引ID// getBitmap()->得到BitmapmBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.pic)).getBitmap();
	
				
		
原创粉丝点击