简体   繁体   中英

How to set the background of imageView avoid OutOfMemoryError in Android?

I try change the background of ImageView , but it error due to OutOfMemoryError .

I have search for change the background by using Bitmap , but I don't know how to use it.

When I click the button , the background will change to the another picture.

The code is like the following:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.user_guide, container, false) ;
        last = (Button) view.findViewById(R.id.last);
        user = (ImageView) view.findViewById(R.id.image);
        last.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                                user.setBackgroundResource(R.drawable.test1);
            }
        });

I have search for the following code , but I don't know hot to use it to change the background of Imageview . Can someone teach me how to use it?

public static Bitmap readBitMap(Context context, int resId){
         BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
         opt.inPurgeable = true;
         opt.inInputShareable = true;
         InputStream is = context.getResources().openRawResource(resId);
         return BitmapFactory.decodeStream(is,null,opt);
         }

Can someone teach me how to use it to change the background of ImageView and without OutOfMemoryError ?

Thanks in advance.

Try below code:

last.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 Bitmap bitmap=readBitmap(this, R.drawable.test1);
                 image.setImageBitmap(bitmap);
                 bitmap.recycle();
                 bitmap=null;
            }
        });

Use of your method:

public static Bitmap readBitMap(Context context, int resId){
         BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
         opt.inPurgeable = true;
         opt.inInputShareable = true;
         InputStream is = context.getResources().openRawResource(resId);
         return BitmapFactory.decodeStream(is,null,opt);
         }

use these two methods for loading bitmap efficiently..... for efficient large bitmap loading

last.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {
            WeakReference bitmapWeakReference=new  WeakReference(decodeSampledBitmapFromResource(getApplicationContext().getRssource(),drawableId,reqWidth,reqHeight));
            user.setImageBitmap(bitmapWeakReference.get());
        }
    });

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM