Android-GC在列表视图滚动时滞后于“更大"图片 [英] Android - GC lags listview scrolling with "bigger" images

查看:52
本文介绍了Android-GC在列表视图滚动时滞后于“更大"图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在列表视图中,我想在列表条目上绘制一张图像. 在垂直模式下,这20张图像必须缩放以填充宽度. 手机分辨率为480 x 800像素(SGS2). 图像分辨率为400x400,大小约为100KB. 我已将图像放置在drawable文件夹中.

In a listview I want to draw one image on a list entry. These 20 images have to scale to fill the width in vertical mode. The phone resolution is 480 x 800 pixels (SGS2). The images resolution is 400x400 and are about 100KB in size. I've placed the images in the drawable folder.

当我滚动浏览列表时,它运行得并不顺利.

When I scroll through the list it's not going smooth.

我了解一些事情: -应用程序堆大小=受限,第一次运行时分配= 13MB,剩下1 MB. -我有GC_FOR_ALLOC日志消息,该消息将系统暂停15毫秒. (我认为这是我的滚动迟滞). -更改代码后,我还看到了GC_CONCURRENT消息.

I understand a few things: - application Heap size = limited, Allocated when first run = 13MB, got 1 MB left. - I have GC_FOR_ALLOC Log messages, which halts the system for 15ms. (this is my lagg in scrolling, I think). - After changing the code I also see GC_CONCURRENT messages.

有了这些GC消息,我了解到每次滚动到新的listview条目中的另一张图像时,我的垃圾收集都会启动. 到目前为止,我可以对此进行分析,但我不知道永久修复和删除GC的确切方法. 我已将图像缩小到100x100,并将GC消息延迟了更长的时间. 但是最终,GC开始了. 我确实使用convertview回收了视图,并且已经在视图中使用了持有人.

With these GC messages, I understand my garbage-collection kicks in every time I scroll to another image in a new listview entry. This I can analyse so far, but I don't know exactly what to do to fix and remove the GC permanently. I have scaled down the images to 100x100 and it postpones the GC messages for a longer time. But eventually the GC kicks in. I do recycle the views with convertview, and use holders inside the views already.

我已经阅读了有关重复使用图像内存的信息,但不确定是否以及如何执行此操作. 或者,当在列表视图中使用这些较大的图像时,maby是正常"的,我需要重新考虑图像的绘制,并且仅在拖曳结束后才开始绘制?

I've read about re-using image memory, but not sure if and how I do this. Or, maby it's "normal" when using these larger images in a listview and I need to rethink the drawing of the images and only start drawing when the scolling ends?

我应该使用Listview滚动图片吗?

Should I be using Listview for scrolling through pictures?

2012-12-31_02:11我已经实现了setOnScrollListener,它可以使滚动平滑. 我认为这是我必须进一步研究的代码,以解决问题.

2012-12-31_02:11 I've implemented the setOnScrollListener, which makes scrolling smooth. I think this is the piece of code I have to investigate further to butter things up.

public class ListviewAdapter extends BaseAdapter {

    private static Activity activity;
    private int[] data;
    private static LayoutInflater mInflater = null;

    public ListviewAdapter(Activity a, int[] d) {
        activity = a;
        data = d;
        mInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

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

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listviewitem, parent,
                    false);
            holder = new ViewHolder();

            holder.picture = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!MainActivity.isScrolling) {
    holder.picture.setImageResource(data[position]);
    }

        return convertView;
    }

    static class ViewHolder {
        ImageView picture;
    }

}


listview XML

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <ImageView
      android:id="@+id/image"
      android:src="@drawable/stub"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="Animal images"
        android:scaleType="fitCenter"
        android:scrollingCache="false"
        android:animationCache="false" 
        />

</LinearLayout>

推荐答案

有很多技术可以使ListViews更快.请参见 Android中的有效ListView .您应该回收视图,这将减少垃圾收集.您可能还考虑在列表滚动停止之前不加载图像,请参见 Lazy .

There are lots of techniques to make ListViews faster. See Efficient ListView in android. You should recycle Views, which will decrease Garbage Collection. You might also consider not loading Images before the list scrolling has stopped, see Lazy Load images on Listview in android(Beginner Level)?.

这篇关于Android-GC在列表视图滚动时滞后于“更大"图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆