Android对大图片进行裁剪避免内存溢出

3 篇文章 0 订阅
2 篇文章 0 订阅

//获取Bitmap有关属性public void getBitmapInfo(Bitmap bitmap) { int height = bitmap.getHeight(); int width = bitmap.getWidth(); int density = bitmap.getDensity(); int memory = bitmap.getAllocationByteCount(); Log.d("Bitmap","height:"+ height + ",width" + width +",density" + density + ",memory:" + memory);}

 

//计算图片采样率,需要从Option获取宽高属性,以及需要的宽高
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int inSampleSize = 1;

    int width = options.outWidth;
    int height = options.outHeight;

    if (width > reqWidth || height > reqHeight) {
        int hafWidth = width / 2;
        int hafHeight = height / 2;

        while (hafWidth > reqWidth && hafHeight > reqHeight) {
            inSampleSize *= 2;
            hafWidth /= inSampleSize;
            hafHeight /= inSampleSize;
        }
    }

    return inSampleSize;
}
//对外提供的获取所需要的裁剪后的图片
public Bitmap getReqBitmap(Resources resources, int id, int reqWidth, int reqHeight) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(resources, id, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(resources, id, options);

}

public Bitmap optimizeBitmap(Resources resources, int id) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inSampleSize = 2;
    return BitmapFactory.decodeResource(resources, id, options);
}

public void getBitmapInfo(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();
    int density = bitmap.getDensity();
    int memory = bitmap.getAllocationByteCount();
    Log.d("Bitmap", "height:" + height + ",width" + width + ",density" + density + ",memory:" + memory);
}

public void clear() {
    for (ImageView iv : imageViewList) {
        Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
            iv.setImageBitmap(null);
        }
    }
    imageViewList.clear();
}

//图片区域显示
public void showPart() {
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream inputStream = getAssets().open("long1.jpg");
        BitmapFactory.decodeStream(inputStream, null, options);
        Rect rect = new Rect(options.outWidth / 2 - 100,
                options.outHeight / 2 - 100,
                options.outWidth / 2 + 100,
                options.outHeight / 2 + 100);
        options.inJustDecodeBounds = false;
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(inputStream, false);

        Bitmap bitmap = decoder.decodeRegion(rect, options);
        imageView.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值