Dav*_*ave 9 java android listview image
问题描述: 我正在创建一个可滚动的文章列表,其中包含由我的SQLite数据库填充的缩略图.一般来说,除了缓慢之外,它"工作":
图像加载非常缓慢...我想使用" 通用图像加载器 "缓存了设备上的图像,如果你已经查看它们(或者至少接近那个图像),它们会使它们看起来只是滚动到视图中).但是 - 当您向上/向下拖动时,没有任何图像存在,然后3-5秒后,图像开始弹出(好像它正在重新下载它们)
我正在改变动态缩略图框的可见性,但这样做完美无瑕 - 它们似乎没有改变 - 它们只是滚动到视图中,没有闪烁或任何东西.(但随后图像不再出现几秒钟).
我已经通过在滚动后删除我的PHP脚本进行测试...当我滚动回到prev spot时,图像没有显示 - 让我认为它是从我的PHP脚本每次加载.
但根据文档: "UsingFreqLimitedMemoryCache(超出缓存大小限制时删除最不常用的位图) - 默认情况下使用"
细节:
在我的ArticleEntryAdapter.js身上:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
// We need to get the best view (re-used if possible) and then
// retrieve its corresponding ViewHolder, which optimizes lookup efficiency
final View view = getWorkingView(convertView);
final ViewHolder viewHolder = getViewHolder(view);
final Article article = getItem(position);
// Set the title
viewHolder.titleView.setText(article.title);
//Set the subtitle (subhead) or description
if(article.subtitle != null)
{
viewHolder.subTitleView.setText(article.subtitle);
}
else if(article.description != null)
{
viewHolder.subTitleView.setText(article.description);
}
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("", viewHolder.thumbView); //clears previous one
if(article.filepath != null && article.filepath.length() != 0) {
imageLoader.displayImage(
"http://img.sltdb.com/processes/resize.php?image=" + article.filepath + "&size=100&quality=70",
viewHolder.thumbView
);
viewHolder.thumbView.setVisibility(View.VISIBLE);
} else {
viewHolder.thumbView.setVisibility(View.GONE);
}
return view;
}
Run Code Online (Sandbox Code Playgroud)
至于图像不正确 - 它不常见,但有时在滚动时,我会看到2张相同的图像,当我看到这些文章时,它们根本没有相关性(即没有机会实际拥有相同的图像)所以 - 我滚动它,然后回来,它不再是不正确的图像.
注意:我是Java/Android的新手 - 你可能已经注意到了.
每条评论请求的代码更多:
private View getWorkingView(final View convertView) {
// The workingView is basically just the convertView re-used if possible
// or inflated new if not possible
View workingView = null;
if(null == convertView) {
final Context context = getContext();
final LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
workingView = inflater.inflate(articleItemLayoutResource, null);
} else {
workingView = convertView;
}
return workingView;
}
Run Code Online (Sandbox Code Playgroud)
更新: 我的清单文件有:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
但我找到的缓存文件夹是完全空的:
mnt
-sdcard
-Android
-data
-com.mysite.news
-cache
-uil-images
Run Code Online (Sandbox Code Playgroud)
Cam*_*mer 19
我在列表视图中遇到了与图像类似的问题.可能这个答案将纠正你错误的图像问题.
我刚刚使用UniversalImageLoader下载了示例项目,它表现出与您描述的行为相同的行为.
到目前为止,通过源代码查看的一些注释.
public static final int DEFAULT_THREAD_POOL_SIZE = 3;
public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 1;
public static final int DEFAULT_MEMORY_CACHE_SIZE = 2 * 1024 * 1024; // bytes
Run Code Online (Sandbox Code Playgroud)
这表示在任何时候都会有三个线程下载,最多2MB的图像.你下载的图片有多大?你还缓存到磁盘?如果是这样,那将是缓慢的.
要配置ImageLoader中的一些基本选项,您需要传入displayImage:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.cacheInMemory()
.cacheOnDisc()
.build();
Run Code Online (Sandbox Code Playgroud)
我也希望你尝试这些选择:
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
.enableLogging()
.memoryCacheSize(41943040)
.discCacheSize(104857600)
.threadPoolSize(10)
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(imageLoaderConfiguration);
Run Code Online (Sandbox Code Playgroud)
通过我的测试,图像在磁盘上,但加载速度仍然很慢.
经过大量测试后,我确定主要问题是UniversalImageLoader只是很慢.具体来说,ImageLoader和LoadAndDisplayImageTask正在阻碍工作.我(很快)将LoadAndDisplayImageTask重写为AsyncTask,它立即表现得更好.您可以在GitHub上下载分叉版本的代码.