gpt4 book ai didi

android - 滚动 ListView 时有些图像非常滞后

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:52:44 24 4
gpt4 key购买 nike

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

有人知道如何提高此类区域的性能以创建漂亮的平滑滚动吗?

更新:图片和文字全部取 self 的SQLite数据库。该列表是使用 SimpleCursorAdapter 创建的。

Gallery

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;
}
}

最佳答案

问题是每张图片都是在 View 准备显示时解码的。 ListView 将回收您的 View ,这意味着在 View 离开屏幕的那一刻,它将被重新使用,因此图像将被覆盖并被垃圾收集。如果该项目重新进入屏幕,则必须再次从数据库中解码图像。

解码速度相当快,但如果用户很快改变列表中的位置,所有解码调用都会使您的列表非常滞后。

我会实现类似 ImageCache 的东西。一个包含 WeakReferences 的 map 的类到图像。每次你想显示一个图像时,你要看看图像是否已经在 map 中,如果 WeakReference 仍然指向一个对象,如果不是这种情况,你需要解码图像然后将它存储在 map 中。

看看延迟加载问题,这些将向您展示如何将解码放在后台任务中,然后在加载图像时更新列表。这需要更多的努力,但它可以使列表更快。如果您打算使用延迟加载问题中的代码示例,请尝试使用 AsyncTasks而不是线程来进行解码。

关于android - 滚动 ListView 时有些图像非常滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3651401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com