【问题标题】:Out-of-memory error on ImageSwitcher with small images带有小图像的 ImageSwitcher 内存不足错误
【发布时间】:2011-07-11 14:53:16
【问题描述】:

我最近一直在使用 2.1 SDK(版本 7)并创建了一些图像,这些图像将通过 ImageSwitcher 显示并通过画廊进行更改,就像在其中一个示例应用程序中一样。我的图像都在 200Kb 左右,我注意到当我切换它们时,Android 会因内存不足错误而崩溃。我知道堆大小通常为 16Mb,所以我不明白为什么 200Kb 的图像会使应用程序崩溃。我在 drawables 目录中有 21 张图片,但一次只显示一张。

这是我的标记:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF">


         <ScrollView android:id="@+id/scroll"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginBottom="17dp">
             <ImageSwitcher android:id="@+id/switcher"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/adView"
            android:background="#FFFFFF"/>
        </ScrollView>

    <Gallery android:id="@+id/gallery"
        android:background="#66000000"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"        
        android:gravity="center_vertical"
        android:spacing="13dp"/>
</RelativeLayout>

我的代码主要来自 ImageSwitcher/Gallery 的 Google 示例:

public class HomeActivity extends Activity implements 
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{

    private Integer[] mThumbIds = {
            R.drawable.a3035, R.drawable.a40,
            R.drawable.a45, R.drawable.a50,
            R.drawable.a55, R.drawable.a60,
            R.drawable.a70, R.drawable.a80,
            R.drawable.a90,R.drawable.a100,
            R.drawable.a110,R.drawable.a120,
            R.drawable.a130,R.drawable.a140,
            R.drawable.a150,R.drawable.a160,
            R.drawable.a170,R.drawable.a180,
            R.drawable.a190,R.drawable.a200210,
            R.drawable.a220300};

    private Integer[] mImageIds = {
            R.drawable.aird3035, R.drawable.aird40, R.drawable.aird45,
            R.drawable.aird50, R.drawable.aird55, R.drawable.aird60,
            R.drawable.aird70, R.drawable.aird80,R.drawable.aird90,
            R.drawable.aird100, R.drawable.aird110, R.drawable.aird120,
            R.drawable.aird130, R.drawable.aird140,R.drawable.aird150,
            R.drawable.aird160, R.drawable.aird170,R.drawable.aird180,
            R.drawable.aird190, R.drawable.aird200210,R.drawable.aird220300};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

        scroll = (ScrollView)findViewById(R.id.scroll);

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(this);    
    }

    @Override
    public void onPause()
    {
        super.onPause();
        System.gc();
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        mSwitcher.setImageResource(mImageIds[position]);
        scroll.scrollTo(0,0);
            System.gc();
    }

    public void onNothingSelected(AdapterView parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFFFFFF);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        return i;
    }

    private ImageSwitcher mSwitcher;
    private ScrollView scroll;

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null) { // create new view
                convertView = new ImageView(mContext);

                convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            }
            ImageView iv = (ImageView) convertView;
            iv.setImageResource(mThumbIds[position]);
            return convertView;
        }

        private Context mContext;

    }     

}

如果您注意到 OnItemSelected 方法,我什至会强制调用垃圾收集器,但我仍然崩溃。如果我将图像缩小到每张 93Kb 左右(这意味着我在更大的屏幕尺寸下会失去分辨率),我的应用不会崩溃。

谁能提供一些关于为什么会发生这种情况的见解以及更好的解决方法?

【问题讨论】:

    标签: android gallery out-of-memory imageswitcher


    【解决方案1】:

    试试这个代码:

      public View getView(int position, View convertView, ViewGroup parent)
      {
          if(convertView == null)
          { // create new view
              convertView = new ImageView(mContext);
              convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          }
          ImageView iv = (ImageView) convertView;
          BitmapFactory.Options options=new BitmapFactory.Options();
          options.inPurgeable=true; //bitmap can be purged to disk
          Bitmap bm=BitmapFactory.decodeResource(mContext.getResources(), mThumbIds[position], options);
          iv.setImageBitmap(bm);
          return iv;
      }
    

    这里的关键点是BitmapFactory.Options.inPurgeable = true;(这是不言自明的)-您也可以尝试其他选项-有时它会有所帮助

    【讨论】:

    • 到目前为止,这看起来像是帮了大忙。但是,我很好奇,如果我需要对 public void onItemSelected(AdapterView parent, View v, int position, long id) { mSwitcher.setImageResource(mImageIds[position]); 做任何不同的事情scroll.scrollTo(0,0); } 方法,因为这是实际发生切换的地方?
    • @Shawn:如果有帮助 - 我的 +1 呢?
    • 我给了 +1,但它仍然崩溃。你能回答关于 OnItemSelected 的问题吗?
    • @Shawn:看起来真正的开关在 onItemSelected() 中。虽然我不太确定垃圾收集是不是一个不错的选择。我也不明白 scrollTo() 方法的目的。您是否使用示例 developer.android.com/resources/samples/ApiDemos/src/com/… 调查过 ImageS?
    • 好吧,我的图像大于可用的屏幕尺寸,所以我将 ImageSwitcher 包裹在 ScrollView 中。该 scrollTo 确保图像从顶部开始,以防上一个图像被滚动。
    【解决方案2】:

    不要调用 System.gc()。显着降低性能。不要在 UI 线程中执行此操作。绝不!

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多