图片缩放,解决oom

<span style="font-family: Arial, Helvetica, sans-serif;">转载:郭大神大作:http://blog.youkuaiyun.com/guolin_blog/article/details/9316683</span>

---------------------------------------------------------------------------------------------------------------

/**
 * http://blog.youkuaiyun.com/guolin_blog/article/details/9316683博文参考
 * 
 * 完成图片质量、尺寸的缩放
 */

public class MainActivity extends Activity {
	private ImageView imageView;
	private ImageView imageView2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		imageView = (ImageView) findViewById(R.id.imageview);
		imageView2 = (ImageView) findViewById(R.id.imageview2);
		imageView2.setImageResource(R.drawable.demo);
		
		/*资源文件压缩,自定义高和宽的压缩*/
		//Bitmap bitmap = ImageScaleUtil.newInstance().decodeBitmapFromResource(getResources(), R.drawable.demo, 200, 100);
		//imageView.setImageBitmap(bitmap);
		/*本地文件压缩*/
		Bitmap bitmap = ImageScaleUtil.newInstance().decodeBitmapFromFile(getResources(), "/mnt/sdcard/demo.jpg", 100, 100);
		
		/*获取当前设备内存*/
		int maxMemory = (int)(Runtime.getRuntime().maxMemory() / 1024);
		Log.d("fuck","Device maxMemory is :" + maxMemory + "KB");
		Log.d("fuck","Device maxMemory is :" + maxMemory / 1024 + "MB");
		
		/*获取图片属性,但是不加载进入内存*/
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;//禁止为bitmap分配内存,返回值也不再是一个Bitmap对象,而是null
		BitmapFactory.decodeResource(getResources(), R.drawable.demo, options);
		int imgWidth = options.outWidth;
		int imgHeight = options.outHeight;
		String imgType = options.outMimeType;
		
//		BitmapFactory.decodeFile(pathName, opts)
	}

}

工具类

package com.example.image;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageScaleUtil {

	public static ImageScaleUtil newInstance(){
		return new ImageScaleUtil();
	}
	
	/**
	 * 完成压缩:自定义压缩后的宽和高,宽和高的值是分辨率
	 * @param resources
	 * @param resId 对应图片的资源ID
	 * @param reqWidth 缩放后的宽
	 * @param reqHeight 缩放后的高
	 * @return Bitmap
	 */
	public static Bitmap decodeBitmapFromResource(Resources resources, int resId, int reqWidth, int reqHeight){
		// 第一次解析将inJustDecodeBounds设置为true,来获取图片大小,不占用内存
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(resources, resId, options);
		//调用方法计算inSampleSize值
		options.inSampleSize =  calculateInSampleSize(options, reqWidth, reqHeight);
		//使用获取到的inSampleSize值再次解析图片 
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(resources, resId, options);
	}
	
	// 本地文件压缩
	public static Bitmap decodeBitmapFromFile(Resources resources, String pathName, int reqWidth, int reqHeight){
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(pathName, options);
		//调用方法计算inSampleSize值
		options.inSampleSize =  calculateInSampleSize(options, reqWidth, reqHeight);
		//使用获取到的inSampleSize值再次解析图片 
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(pathName, options);
	}
	
	/**
	 * 图片压缩比:设置BitmapFactory.Options中inSampleSize的值达到图片缩放
	 * @param options
	 * @param reqWidth reqWidth 缩放后的宽
	 * @param reqHeight reqHeight 缩放后的高
	 * @return BitmapFactory.Options中inSampleSize的值(压缩值)
	 */
	public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
		//原图片高和宽
		int oriWidth = options.outWidth;
		int oriHeight = options.outHeight;
		int inSampleSize = 1;
		if(oriHeight > reqHeight || oriWidth > reqWidth){
			//计算出当前缩放的比例
			int heightRatio = Math.round((float)oriWidth / (float)reqWidth);
			int widthRatio = Math.round((float)oriHeight / (float)reqHeight);
			//选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
			inSampleSize = heightRatio > widthRatio ? widthRatio : heightRatio;
		}
		return inSampleSize;
	}
	
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值