Andriod加载大图

在很多APP中都需要从服务器去加载N多资源,比如图片、音乐等等。在加载服务器资源时,因为服务器上的资源是用于WEB的,所有资源可能比较大,那在移动端就不能这样加载了,因为移动端本身系统所持有的资源很紧张,所有下载服务器大资源时,就需要压缩

在Android Doc中关于图片的压缩,有很好的解释:

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class. Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

大题意思是:BitmapFactory类提供了几个解码方法(decodeByteArray(),decodeFile(),decodeResource()),创建位。选择最适当的解码方法基于图像数据来源。这些方法试图分配内存的构造图,因此可以很容易地导致OutOfMemory异常。每种类型的解码方法可以让你通过BitmapFactory解码。将inJustDecodeBounds属性设置为true,解码避免内存分配,位图对象返回null,但设置outWidth outHeight outMimeType。

图片布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/bigimg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
然后编写一个工具类:

public class BitmapCompressTools {
	public static Bitmap decodeSampledBitmapFromResource(Resources res,
			int resId, int reqWidth, int reqHeight) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);
		// 计算比例
		options.inSampleSize = calculateInSampleSize(options, reqHeight,
				reqHeight);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}

	// 指定输入图片的缩放比例
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		int imageHeight = options.outHeight;
		int imageWidth = options.outWidth;
		int inSampleSize = 1;
		if (imageHeight > reqHeight || imageWidth > reqWidth) {
			// 计算压缩的比例:分为宽高比例
			final int heightRatio = Math.round((float) imageHeight
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) imageWidth
					/ (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}

		return inSampleSize;
	}
}
然后在Activity中调用

public class LoadBigImgActivity extends Activity {
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loadimg);
		imageView = (ImageView) findViewById(R.id.bigimg);
		//获取系统屏幕宽高
		WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
		int width = manager.getDefaultDisplay().getHeight();
		int height = manager.getDefaultDisplay().getWidth();
		
		Bitmap bitmap = BitmapCompressTools.decodeSampledBitmapFromResource(
				getResources(), R.drawable.photo, width, height);
		imageView.setImageBitmap(bitmap);
		//释放图片资源
		if(!bitmap.isRecycled()){
			bitmap.recycle();
			System.gc();
		}
	}
}
这样就能解决因为图片过大而照成的OOM问题





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值