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

来源:互联网 发布:潘婷三分钟奇迹知乎 编辑:程序博客网 时间:2024/06/05 16:50

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 dimensions        tmpOptions.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) {        }    }}

0 0
原创粉丝点击