我对Universal Image loader有另一个问题。尝试在listview和gridview中使用它来显示大量图像。传递给UIL的网址包含高分辨率图像。我已经尝试使用几乎所有的scaletypes,它仍然在移动网络上加载比原始LazyList慢得多的图像。据我所知,它首先加载精确的图像然后调整大小。有没有办法下载较少量的数据来显示这个非常大比例的图像。此外,它有时会在加载图像时出现两种不同的异常情况。 EOFException和SocketTimeoutException。我在这里做错了什么想法?
更新 适配器类:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private List<String[]> data;
private static LayoutInflater inflater=null;
public ImageLoade1r imageLoader;
public boolean show;
public final String webs = "http://www";
public DisplayImageOptions options;
public ImageLoader nostra;
public LazyAdapter(Activity a, List<String[]> d, boolean flag) {
activity = a;
data=d;
show=flag;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a.getApplicationContext())
.enableLogging()
.build();
nostra = ImageLoader.getInstance();
nostra.init(config);
options = new DisplayImageOptions.Builder()
.cacheOnDisc()
.showImageOnFail(R.drawable.failed)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoade1r(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
String[] url = data.get(position);
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);;
ImageView image=(ImageView)vi.findViewById(R.id.image);
ProgressBar spinner = (ProgressBar) vi.findViewById(R.id.prgrsItem);
RelativeLayout layo = (RelativeLayout) vi.findViewById(R.id.thelayout);
if(url[1].trim().equals("folder"))
{
String img = webs+url[3].trim() + url[2] +"title"+".jpg";
display(image, img, spinner);
text.setText(url[2].replaceAll("_", " "));
}
else
{
String img;
if(show==true)
img = webs+url[3] + url[2]+"title"+".jpg";
else
img = webs+url[0];
display(image, img, spinner);
text.setText(url[2].replaceAll("_", " "));
}
if(show==true)
{
text.setVisibility(View.VISIBLE);
}
else
{
text.setVisibility(View.GONE);
}
return vi;
}
public void display(ImageView img, String url, final ProgressBar spinner)
{
nostra.displayImage(url, img, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
spinner.setVisibility(View.GONE);
EasyTracker.getTracker().sendException(failReason.toString(), false);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
}
更新 EOFException:
03-21 13:51:55.800: E/ImageLoader(31605): null
03-21 13:51:55.800: E/ImageLoader(31605): java.io.EOFException
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:113)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:84)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:299)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:237)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:149)
03-21 13:51:55.800: E/ImageLoader(31605): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-21 13:51:55.800: E/ImageLoader(31605): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-21 13:51:55.800: E/ImageLoader(31605): at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:2)
使用时应使用视图持有者来提高性能。
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
在滚动ListView期间,您的代码可能经常调用findViewById(),这会降低性能。即使适配器返回一个膨胀的视图以进行回收,您仍然需要查找元素并更新它们。重复使用findViewById()的方法是使用“视图持有者”设计模式。
http://www.youtube.com/watch?v=wDBM6wVEO70。主题是abot listview和view holder。
当套接字读取或接受操作超时时,抛出SokectTimeOut异常。
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a.getApplicationContext())
.enableLogging()
.build();
.imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000)) // connectTimeout (5 s), readTimeout (20 s)
对于EOFException,请检查链接。
http://code.google.com/p/libs-for-android/issues/detail?id=14
System.setProperty("http.keepAlive", "false").
提出了类似的问题,有些人建议在打开HTTP网址连接之前尝试上述操作。
可能与您的代码无关。