【问题标题】:How to set the background of imageView avoid OutOfMemoryError in Android?如何在Android中设置imageView的背景避免OutOfMemoryError?
【发布时间】:2014-02-24 12:21:10
【问题描述】:

我尝试更改ImageView的背景,但由于OutOfMemoryError而出错。

我有搜索使用 Bitmap 更改背景,但我不知道如何使用它。

当我点击按钮时,背景会变成另一张图片。

代码如下:

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

我已经搜索了以下代码,但是我不知道用它来更改Imageviewbackground。 谁能教我怎么用?

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

有人可以教我如何使用它来改变ImageView 和没有OutOfMemoryError 的背景吗?

提前致谢。

【问题讨论】:

标签: android bitmap out-of-memory android-imageview


【解决方案1】:

试试下面的代码:

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

使用你的方法:

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

【讨论】:

    【解决方案2】:

    使用这两种方法来有效地加载位图..... 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多