Android對大圖片進行裁剪避免內存溢出

//獲取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();
    }
}

 

發佈了44 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章