好的,我一直在努力优化我的照片库几天(这是我的第一个Android项目)。所有照片都是通过网页加载的。我已经开始使用Universal Image Loader,但我仍然不满足于结果。
这是我的班级:
public class Galerija extends Activity {
ArrayList<RSSItem> lista = new ArrayList<RSSItem>();
ArrayList<String> lst_slika = new ArrayList<String>();
RSSItem tempItem = new RSSItem();
ImageAdapter adapter;
ImageLoader imageLoader;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_galerija);
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader myReader = sp.getXMLReader();
URL url = new URL("http://erdut.gausstesting.info/generateXML/galerija.php");
XMLHandler myXMLHandler = new XMLHandler();
myReader.setContentHandler(myXMLHandler);
myReader.parse(new InputSource(url.openStream()));
lista = myXMLHandler.getRss_lista();
lst_slika = lista.get(0).getImages();
} catch (Exception e) {
System.out.println(e);
}
adapter = new ImageAdapter(this, lst_slika);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> lista;
/*
final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.mContext)
.enableLogging()
.memoryCacheSize(41943040)
.discCacheSize(104857600)
.threadPoolSize(10)
.build();
*/
final DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory()
.cacheOnDisc()
.build();
public ImageAdapter(Context c, ArrayList<String> lista) {
this.mContext = c;
this.lista = lista;
imageLoader = ImageLoader.getInstance();
//imageLoader.init(config);
imageLoader.init(ImageLoaderConfiguration.createDefault(c));
}
public int getCount() {
return lista.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
//ova 3 polja označavaju veličinu, cropanje i padding prikazanih slika
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(lista.get(position), imageView, options);
//new ImageDownloadTask(imageView).execute(lista.get(position));
return imageView;
}
}
}
与我以前的解决方案相比,它确实加快了速度,但我并不满足。首先,我甚至无法在我的HTC Incredible S上启动它 - 只需启动黑屏活动,无需加载。那是为什么?
它仅适用于模拟器,但在滚动时会重新加载所有图像。因此,一旦您向下滚动然后再向上,所有图像似乎都会重新加载。它是如何工作的?还有什么方法可以改善这个吗?
提前致谢!
答案 0 :(得分:9)
您应该考虑使用asynctask从服务器获取URL。
您应该使用视图持有者来实现平滑滚动和性能。 http://www.youtube.com/watch?v=wDBM6wVEO70。谈话是关于观看者和列表视图的表现。
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
https://github.com/nostra13/Android-Universal-Image-Loader。查看配置和显示选项
下的主题不应再次下载,而应将图像缓存在手机记忆库或SD卡中。
它使用url作为密钥,在sdcard中缓存图像(如果已正确配置)。如果存在从缓存中显示,则下载,缓存和显示图像。
在自定义适配器构造函数
中 File cacheDir = StorageUtils.getOwnCacheDirectory(activity context, "your folder");//for caching
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.drawable.default_pic)//display stub image until image is loaded
.displayer(new RoundedBitmapDisplayer(20))
.build();
在你的getView()
中viewholder.image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, viewholder.image,options);//provide imageurl, imageview and options.