得到一个BitMap对象


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()->得到Bitmap
		mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.pic))
				.getBitmap();



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值