Android進階:ListView性能優化異步加載圖片 使滑動效果流暢
ListView 是一種可以顯示一係列項目並能進行滾動顯示的 View,每一行的Item可能包含複雜的結構,可能會從網絡上獲取icon等的一些圖標信息,就現在的網絡速度要想保持ListView運行的很好滾動流暢是做不到的
所以這裏就需要把這些信息利用多線程實現異步加載
實現這樣功能的類
- public class AsyncImageLoader {
- private HashMap<String, SoftReference<Drawable>> imageCache;
- public AsyncImageLoader() {
- imageCache = new HashMap<String, SoftReference<Drawable>>();
- }
- public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
- if (imageCache.containsKey(imageUrl)) {
- SoftReference<Drawable> softReference = imageCache.get(imageUrl);
- Drawable drawable = softReference.get();
- if (drawable != null) {
- return drawable;
- }
- }
- final Handler handler = new Handler() {
- @Override
- public void handleMessage(Message message) {
- imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
- }
- };
- new Thread() {
- @Override
- public void run() {
- Drawable drawable = loadImageFromUrl(imageUrl);
- imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
- Message message = handler.obtainMessage(0, drawable);
- handler.sendMessage(message);
- }
- }.start();
- return null;
- }
- public static Drawable loadImageFromUrl(String url) {
- // ...
- }
- public interface ImageCallback {
- public void imageLoaded(Drawable imageDrawable, String imageUrl);
- }
- }
public class AsyncImageLoader { private HashMap<String, SoftReference<Drawable>> imageCache; public AsyncImageLoader() { imageCache = new HashMap<String, SoftReference<Drawable>>(); } public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { if (imageCache.containsKey(imageUrl)) { SoftReference<Drawable> softReference = imageCache.get(imageUrl); Drawable drawable = softReference.get(); if (drawable != null) { return drawable; } } final Handler handler = new Handler() { @Override public void handleMessage(Message message) { imageCallback.imageLoaded((Drawable) message.obj, imageUrl); } }; new Thread() { @Override public void run() { Drawable drawable = loadImageFromUrl(imageUrl); imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); Message message = handler.obtainMessage(0, drawable); handler.sendMessage(message); } }.start(); return null; } public static Drawable loadImageFromUrl(String url) { // ... } public interface ImageCallback { public void imageLoaded(Drawable imageDrawable, String imageUrl); }}
注意這裏使用了 SoftReference來緩存圖片,允許 GC在需要的時候可以對緩存中的圖片進行清理。它這樣工作:
· 調用 loadDrawable(ImageUrl, imageCallback),傳入一個匿名實現的 ImageCallback接口
· 如果圖片在緩存中不存在的話,圖片將從單一的線程中下載並在下載結束時通過 ImageCallback回調
· 如果圖片確實存在於緩存中,就會馬上返回,不會回調 ImageCallback
然後我們還可以根據09google I/0開發者大會提到的方式來繼續優化Adapter 使用ViewHolder來減少一些比較費時的操作,譬如inflate XML 和 findViewById()等操作
- public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
- private ListView listView;
- private AsyncImageLoader asyncImageLoader;
- public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, ListView listView) {
- super(activity, 0, imageAndTexts);
- this.listView = listView;
- asyncImageLoader = new AsyncImageLoader();
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Activity activity = (Activity) getContext();
- // Inflate the views from XML
- View rowView = convertView;
- ViewCache viewCache;
- if (rowView == null) {
- LayoutInflater inflater = activity.getLayoutInflater();
- rowView = inflater.inflate(R.layout.image_and_text_row, null);
- viewCache = new ViewCache(rowView);
- rowView.setTag(viewCache);
- } else {
- viewCache = (ViewCache) rowView.getTag();
- }
- ImageAndText imageAndText = getItem(position);
- // Load the image and set it on the ImageView
- String imageUrl = imageAndText.getImageUrl();
- ImageView imageView = viewCache.getImageView();
- imageView.setTag(imageUrl);
- Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
- public void imageLoaded(Drawable imageDrawable, String imageUrl) {
- ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
- if (imageViewByTag != null) {
- imageViewByTag.setImageDrawable(imageDrawable);
- }
- }
- });
- imageView.setImageDrawable(cachedImage);
- // Set the text on the TextView
- TextView textView = viewCache.getTextView();
- textView.setText(imageAndText.getText());
- return rowView;
- }
- }
public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> { private ListView listView; private AsyncImageLoader asyncImageLoader; public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, ListView listView) { super(activity, 0, imageAndTexts); this.listView = listView; asyncImageLoader = new AsyncImageLoader(); } @Override public View getView(int position, View convertView, ViewGroup parent) { Activity activity = (Activity) getContext(); // Inflate the views from XML View rowView = convertView; ViewCache viewCache; if (rowView == null) { LayoutInflater inflater = activity.getLayoutInflater(); rowView = inflater.inflate(R.layout.image_and_text_row, null); viewCache = new ViewCache(rowView); rowView.setTag(viewCache); } else { viewCache = (ViewCache) rowView.getTag(); } ImageAndText imageAndText = getItem(position); // Load the image and set it on the ImageView String imageUrl = imageAndText.getImageUrl(); ImageView imageView = viewCache.getImageView(); imageView.setTag(imageUrl); Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() { public void imageLoaded(Drawable imageDrawable, String imageUrl) { ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl); if (imageViewByTag != null) { imageViewByTag.setImageDrawable(imageDrawable); } } }); imageView.setImageDrawable(cachedImage); // Set the text on the TextView TextView textView = viewCache.getTextView(); textView.setText(imageAndText.getText()); return rowView; }}
這裏我們沒有加載完iamge之後直接設定到相應的ImageView上 ,而是通過Tag查找,這裏我們重用的View 這裏有個listView的引用來通過Tag查找 可見 CallBack的實現
- ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
- if (imageViewByTag != null) {
- imageViewByTag.setImageDrawable(imageDrawable);
- }
ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl); if (imageViewByTag != null) { imageViewByTag.setImageDrawable(imageDrawable); }
這裏通過ViewCatch來減少了 findViewById的使用
- public class ViewCache {
- private View baseView;
- private TextView textView;
- private ImageView imageView;
- public ViewCache(View baseView) {
- this.baseView = baseView;
- }
- public TextView getTextView() {
- if (textView == null) {
- textView = (TextView) baseView.findViewById(R.id.text);
- }
- return titleView;
- }
- public ImageView getImageView() {
- if (imageView == null) {
- imageView = (ImageView) baseView.findViewById(R.id.image);
- }
- return imageView;
- }
- }
public class ViewCache { private View baseView; private TextView textView; private ImageView imageView; public ViewCache(View baseView) { this.baseView = baseView; } public TextView getTextView() { if (textView == null) { textView = (TextView) baseView.findViewById(R.id.text); } return titleView; } public ImageView getImageView() { if (imageView == null) { imageView = (ImageView) baseView.findViewById(R.id.image); } return imageView; }}
總結 :這裏主要做了三點優化
- 在單一線程裏加載圖片
- 重用列表中行
- 緩存行中的 View
最後更新:2017-04-02 06:51:45
上一篇:
7.4 AbsoluteLayout布局詳解
下一篇:
AsyncTask實現原理和內部代碼
阿裏雲牽手印度電信巨頭 為全球市場提供快速網絡連接
PostgreSQL 10.0 preview 功能增強 - 邏輯複製支持並行COPY初始化數據
用Python開源機器人和5美元,我在Instagram上搞到了2500個真粉兒
中化集團牽手阿裏雲擁抱互聯網+ 打造領先的化工行業B2B垂直電商
Large-Scale Instant Messaging Hosting on Alibaba Cloud
協同OA——讓管理變透明
Automatic logon configuration on Linux OS
Java類加載器學習2——自定義類加載器和父類委托機製帶來的問題
Servlet中的監聽器及監聽在線人數實例
國航OA項目技術總結(一)關於catch的小技巧