1

我已经使用 Listview/customcursoradapter 创建了一个食谱列表。自定义布局包括食谱的照片,现在我在查看和滚动的性能方面遇到了一些问题,ListView尽管它只有 10 条记录(目标是 150 条)。有时我会收到此错误java.lang.OutOfMemoryError: bitmap size exceeds VM budget,我尝试实现AsyncTask但未能做到。有什么办法可以克服这个问题吗?

非常感谢您的帮助!

这是我的getView方法

public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);
    Cursor cursbbn  = getCursor();
    if (row == null)
    {
        LayoutInflater inflater = (LayoutInflater) localContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.listtype, null);
    }

    String Title = cursbbn.getString(2);
    String SandID=cursbbn.getString(1);
    String Readyin = cursbbn.getString(4);
    String Faovoites=cursbbn.getString(8);

    TextView titler=(TextView)row.findViewById(R.id.listmaintitle);
    TextView readyinr=(TextView)row.findViewById(R.id.listreadyin);

    int colorPos = position % colors.length;
    row.setBackgroundColor(colors[colorPos]);

    titler.setText(Title);
    readyinr.setText(Readyin);

    ImageView picture = (ImageView) row.findViewById(R.id.imageView1);

    Bitmap bitImg1 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0001);
    Bitmap bitImg2 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0002);
    Bitmap bitImg3 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0003);
    Bitmap bitImg4 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0004);
    Bitmap bitImg5 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0005);
    Bitmap bitImg6 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0006);
    Bitmap bitImg7 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0007);
    Bitmap bitImg8 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0008);
    Bitmap bitImg9 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0009);
    Bitmap bitImg10 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0010);

    if(SandID.contentEquals("0001"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg1));

    if(SandID.contentEquals("0002"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg2));

    if(SandID.contentEquals("0003"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg3));

    if(SandID.contentEquals("0004"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg4));

    if(SandID.contentEquals("0005"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg5));

    if(SandID.contentEquals("0006"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg6));
    if(SandID.contentEquals("0007"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg7));
    if(SandID.contentEquals("0008"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg8));
    if(SandID.contentEquals("0009"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg9));

    if(SandID.contentEquals("0010"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg10));

    return row;
}

这是错误:

05-02 03:11:55.898: E/AndroidRuntime(376): FATAL EXCEPTION: main
05-02 03:11:55.898: E/AndroidRuntime(376): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
05-02 03:11:55.898: E/AndroidRuntime(376):  at master.chef.mediamaster.AlternateRowCursorAdapter.getView(AlternateRowCursorAdapter.java:83)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.widget.AbsListView.obtainView(AbsListView.java:1409)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.widget.ListView.fillUp(ListView.java:700)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.widget.ListView.fillGap(ListView.java:646)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3399)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:2233)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.widget.ListView.onTouchEvent(ListView.java:3446)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.View.dispatchTouchEvent(View.java:3885)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-02 03:11:55.898: E/AndroidRuntime(376):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
05-02 03:11:55.898: E/AndroidRuntime(376):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
05-02 03:11:55.898: E/AndroidRuntime(376):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.os.Looper.loop(Looper.java:123)
05-02 03:11:55.898: E/AndroidRuntime(376):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-02 03:11:55.898: E/AndroidRuntime(376):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 03:11:55.898: E/AndroidRuntime(376):  at java.lang.reflect.Method.invoke(Method.java:507)
05-02 03:11:55.898: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-02 03:11:55.898: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-02 03:11:55.898: E/AndroidRuntime(376):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

3

首先,你没有使用 convertView,你应该开始,

View view = convertView;
if (view == null) {
  // inflate
}

这是一篇关于使用 convertView 的博客文章,

http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html

其次,您应该只解码您知道将使用的位图。所以改变你的方法,

if(SandID.contentEquals("0001"))
    Bitmap bitImg1 = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.rec0001);
    picture.setImageBitmap(getRoundedCornerImage(bitImg1));
...

第三,您可以将它们解码一次,并在此方法之外使用它们吗?例如,在构造函数中对它们进行一次解码并将它们作为实例成员存储在适配器类中,然后在getView().

第四,你在回收你的位图吗?位图的分配方式与其他对象不同,并且需要recycle()在使用完它们后调用它们的方法。如果您可以实现上面的第三项,则可以在很大程度上避免这种复杂性,尽管您仍然应该在活动停止时释放它们,在活动开始时重新分配它们。

关于 SO 有很多很好的信息Bitmap.recycle(),我建议您花一些时间研究它。解释其中的复杂性超出了此处的帖子所能完成的范围。这是一篇涵盖该主题的好帖子,

Bitmap、Bitmap.recycle()、WeakReferences 和垃圾回收

于 2012-05-02T01:06:47.503 回答