您的位置:首页 > 移动开发 > Android开发

android 加载超大图片内存溢出问题

2016-05-06 12:10 471 查看
1.先把图片资源转换成输入流
LargeImageView largeImageView = (LargeImageView) findViewById(R.id.liv);
InputStream inputStream = getResources().openRawResource(R.raw.register_background);
largeImageView.setInputStream(inputStream);
2.使用
private static final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
/**
* 绘制的区域
*/
private volatile Rect mRect = new Rect();
//默认直接显示图片的中心区域,可以自己去调节mRect.left = 0;mRect.top = imageHeight / 2 - height / 2;mRect.right = width;mRect.bottom = mRect.top + height;
public void setInputStream(InputStream is) {try {
	BitmapRegionDecoder mDecoder = BitmapRegionDecoder.newInstance(is, false);BitmapFactory.Options tmpOptions = new BitmapFactory.Options();// Grab the bounds for the scene dimensionstmpOptions.inJustDecodeBounds = true;BitmapFactory.decodeStream(is, null, tmpOptions);mImageWidth = tmpOptions.outWidth;mImageHeight = tmpOptions.outHeight;
Bitmap bm = mDecoder.decodeRegion(mRect, options);//修改mRect的边界值即可动态的展示图片区域
    } catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) is.close();} catch (Exception e) {}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: