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

本文介绍了一种通过BitmapRegionDecoder实现的图片动态展示方法,能够指定显示图片的特定区域,并提供了详细的代码实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) {
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值