0

哪种风格消耗更少的内存?(避免OOM异常)..

    ImageView img = (ImageView)findViewById(R.id.test) 
 // <--android:src="@drawable/test.png" declared in layout.xml

或者

       res = getBaseContext().getResources();

    imgV = (ImageView)findViewById(R.id.imageView1);
    bm1 = BitmapManager.ShrinkBitmap(res , R.drawable.test, MainActivity.this);
    imgV.setImageBitmap(bm1);

    @Override
public void onDestroy() {

    if(bm1!=null)
        if(!bm1.isRecycled()){
            bm1.recycle();
            //bm1 = null;
        }
    .....
}

...


public static Bitmap ShrinkBitmap(Resources res , int id , Activity parent){
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        bmpFactoryOptions.inPurgeable = true;
        bmpFactoryOptions.inInputShareable = true;
        bmpFactoryOptions.inPreferredConfig= Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeResource(res, id, bmpFactoryOptions) ;          
        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeResource(res, id, bmpFactoryOptions);
        return bitmap;
    }
4

1 回答 1

1

我认为第一个比第二个花费更少的内存。对于第一种方式,解码后的位图大小(宽度,高度)与 ImageView 大小相同。对于第二种方式,您首先解码位图,但您没有在选项中指定宽度和高度。可能大小比 ImageView 大。因此,当您将位图设置为 ImageView 时,位图将重新缩放以适应。

于 2013-09-05T01:35:12.880 回答