滚动浏览ListView时,一些图像非常滞后

mle*_*vit 6 performance android android-listview

我有下面的屏幕,其中包含一些图像(每个可见页面6个).向上和向下滚动对我来说似乎很迟钝.就像它再次渲染图像一样.向后滚动似乎比向下滚动更糟糕.

任何人都知道如何提高这样一个区域的性能,以创建一个漂亮的平滑滚动?

更新:图像和文本都是从我的SQLite数据库中检索的.该列表使用SimpleCursorAdapter创建.

画廊

private class HistoryViewBinder implements SimpleCursorAdapter.ViewBinder 
{
    //private int wallpaperNumberIndex;
    private int timeIndex;
    private int categoryIndex;
    private int imageIndex;

    private java.text.DateFormat dateFormat;
    private java.text.DateFormat timeFormat;
    private Date d = new Date();

    public HistoryViewBinder(Context context, Cursor cursor) 
    {
        dateFormat = android.text.format.DateFormat.getDateFormat(context);
        timeFormat = android.text.format.DateFormat.getTimeFormat(context);

        //wallpaperNumberIndex = cursor.getColumnIndexOrThrow(HistoryDatabase.KEY_WALLPAPER_NUMBER);
        timeIndex = cursor.getColumnIndexOrThrow(HistoryDatabase.KEY_TIME);
        categoryIndex = cursor.getColumnIndexOrThrow(HistoryDatabase.KEY_CATEGORY);
        imageIndex = cursor.getColumnIndexOrThrow(HistoryDatabase.KEY_IMAGE);
    }

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) 
    {
        Log.d(TAG, "setViewValue");

        if (view instanceof TextView)
        {
            Log.d(TAG, "TextView");
            TextView tv = (TextView) view;

            if (columnIndex == timeIndex)
            {
                Log.d(TAG, "timeIndex");
                d.setTime(cursor.getLong(columnIndex));
                tv.setText(timeFormat.format(d) + "  " + dateFormat.format(d));
                return true;
            }
            else if (columnIndex == categoryIndex)
            {
                Log.d(TAG, "categoryIndex");
                tv.setText(cursor.getString(columnIndex));
                return true;
            }
        }
        else if (view instanceof ImageView)
        {
            Log.d(TAG, "ImageView");
            ImageView iv = (ImageView) view;

            if (columnIndex == imageIndex)
            {
                Log.d(TAG, "imageIndex");
                byte[] image = cursor.getBlob(columnIndex);
                Bitmap bitmapImage = BitmapFactory.decodeByteArray(image, 0, image.length);
                iv.setImageBitmap(bitmapImage);
                return true;
            }
        }

        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

Jan*_*usz 6

问题是每个图像在视图准备好显示时被解码.ListView控件将回收您的意见,这意味着,在目前的视图离开它会被重用,因此图像将被覆盖,垃圾收集屏幕.如果项目重新进入屏幕,则必须再次从数据库中解码图像.

解码速度合理,但如果用户非常快速地更改列表中的位置,则所有解码调用都会使您的列表非常滞后.

我会像ImageCache这样的东西.一个包含带有WeakReferences的Map 到图像的类.每次要显示的图像,你看看如果图像是媒体链接的地图,如果仍的WeakReference指向对象,如果这不是你需要的图像进行解码,然后将其存储在地图的情况.

看看延迟加载的问题,这些将告诉你如何把解码在后台任务,然后更新列表中的图像加载的时刻.这需要更多的努力,但它可以使列表更快.如果您要使用延迟加载问题中的代码示例,请尝试使用AsyncTasks而不是Threads来进行解码.