【问题标题】:Android png images big in memoryAndroid png图像在内存中很大
【发布时间】:2012-07-09 17:39:06
【问题描述】:

我在 android 中有一个具有imageflipper 的应用程序。问题是,在将大约 8 张图像加载到内存后,出现内存不足错误。

好吧,我尝试进行动态图像加载,这样如果用户翻转 2 个图像,我会将下 2 个图像加载到内存并删除 2 个第一个图像。它有点工作,但它很难看,当用户翻转图像时我遇到了麻烦(imageflipper.showprevious())。

我不能真正转移所有图像并将新图像放在开头。

我的问题是:
有没有更好的方法来做这种事情?调整图像大小并没有真正的帮助。

【问题讨论】:

    标签: android image png


    【解决方案1】:

    我用下面的代码sn-p解决了内存相关的问题,我们可以通过isRecycled()方法解决这个问题。将此代码添加到您的代码中,这里finalImage 是我的BitmapDrawableR.id.image_viewer 是我的图像视图,您可以更改它自己的

     @Override
            protected void onDestroy() {
    
                 finalImage.setCallback(null);
                 if (!((BitmapDrawable) finalImage).getBitmap().isRecycled()) {
                 ((BitmapDrawable) finalImage).getBitmap().recycle();
                 }
                 finalImage = null;
                unbindDrawables(findViewById(R.id.image_viewer));
                Runtime.getRuntime().gc();
                // scaledBitmap.recycle();
                System.gc();
                super.onDestroy();
            }
    
            private void unbindDrawables(View view) {
                if (view.getBackground() != null) {
                    view.getBackground().setCallback(null);
                }
                if (view instanceof ViewGroup) {
                    for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                        unbindDrawables(((ViewGroup) view).getChildAt(i));
                    }
                    ((ViewGroup) view).removeAllViews();
                }
            }
    

    【讨论】:

      【解决方案2】:

      使用BitmapFactory.Options

      BitmapFactory.Options opts = new BitmapFactory.Options();
      opt.inSampleSize = 2;
      //this will decrease bitmap size,
      // but also affect quality of the image, 
      //so just play with this value to spot the good one;
      Bitmap b = BitmapFactory.decodeFile(fileName, opts);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 2012-10-16
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        相关资源
        最近更新 更多