Android實(shí)現(xiàn)ListView異步加載圖片的方法
本文實(shí)例講述了Android實(shí)現(xiàn)ListView異步加載圖片的方法。分享給大家供大家參考。具體如下:
ListView異步加載圖片是非常實(shí)用的方法,凡是是要通過網(wǎng)絡(luò)獲取圖片資源一般使用這種方法比較好,用戶體驗(yàn)好,不用讓用戶等待下去,下面就說實(shí)現(xiàn)方法,先貼上主方法的代碼:
package cn.wangmeng.test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
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() {
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) {
URL m;
InputStream i = null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(i, "src");
return d;
}
public interface ImageCallback {
public void imageLoaded(Drawable imageDrawable, String imageUrl);
}
}
以上代碼是實(shí)現(xiàn)異步獲取圖片的主方法,SoftReference是軟引用,是為了更好的為了系統(tǒng)回收變量,重復(fù)的URL直接返回已有的資源,實(shí)現(xiàn)回調(diào)函數(shù),讓數(shù)據(jù)成功后,更新到UI線程。
幾個(gè)輔助類文件:
package cn.wangmeng.test;
public class ImageAndText {
private String imageUrl;
private String text;
public ImageAndText(String imageUrl, String text) {
this.imageUrl = imageUrl;
this.text = text;
}
public String getImageUrl() {
return imageUrl;
}
public String getText() {
return text;
}
}
package cn.wangmeng.test;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
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 textView;
}
public ImageView getImageView() {
if (imageView == null) {
imageView = (ImageView) baseView.findViewById(R.id.image);
}
return imageView;
}
}
ViewCache是輔助獲取adapter的子元素布局:
package cn.wangmeng.test;
import java.util.List;
import cn.wangmeng.test.AsyncImageLoader.ImageCallback;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
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();
}
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);
}
}
});
if (cachedImage == null) {
imageView.setImageResource(R.drawable.default_image);
}else{
imageView.setImageDrawable(cachedImage);
}
// Set the text on the TextView
TextView textView = viewCache.getTextView();
textView.setText(imageAndText.getText());
return rowView;
}
}
ImageAndTextListAdapter是實(shí)現(xiàn)ListView的Adapter,里面有個(gè)技巧就是imageView.setTag(imageUrl),setTag是存儲(chǔ)數(shù)據(jù)的,這樣是為了保證在回調(diào)函數(shù)時(shí),listview去更新自己對應(yīng)item,大家仔細(xì)閱讀就知道了。
最后貼出布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
運(yùn)行效果截圖如下:

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
- Android中ListView異步加載圖片錯(cuò)位、重復(fù)、閃爍問題分析及解決方案
- Android實(shí)現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動(dòng)態(tài)更新的方法
- Android程序開發(fā)ListView+Json+異步網(wǎng)絡(luò)圖片加載+滾動(dòng)翻頁的例子(圖片能緩存,圖片不錯(cuò)亂)
- Android ListView異步加載圖片方法詳解
- Android實(shí)現(xiàn)ListView異步加載的方法(改進(jìn)版)
- Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能(ListView)
- Android之ListView分頁加載數(shù)據(jù)功能實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)ListView分頁自動(dòng)加載數(shù)據(jù)的方法
- Android ListView實(shí)現(xiàn)上拉加載更多和下拉刷新功能
- 基于Android ListView之加載使用技巧
- Android開發(fā)實(shí)現(xiàn)ListView異步加載數(shù)據(jù)的方法詳解
相關(guān)文章
Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android應(yīng)用中圖片瀏覽時(shí)實(shí)現(xiàn)自動(dòng)切換功能的方法詳解
這篇文章主要介紹了Android應(yīng)用中圖片瀏覽時(shí)實(shí)現(xiàn)自動(dòng)切換功能的方法,文中還講解了一個(gè)觸摸大圖進(jìn)行圖片切換的深入功能,需要的朋友可以參考下2016-04-04
Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能
這篇文章主要介紹了Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
這篇文章主要介紹了Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法,本文給出了效果圖和實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-01-01
Android音視頻開發(fā)之VideoView使用指南
VideoView組件內(nèi)部同樣是使用MediaPlayer+SurfaceView的形式控制MediaPlayer對視頻文件進(jìn)行播放,本文就來詳細(xì)講講它的使用方法,需要的可以參考一下2022-04-04
Android模擬實(shí)現(xiàn)華為系統(tǒng)升級進(jìn)度條
這篇文章主要介紹了如何通過Android模擬實(shí)現(xiàn)華為在系統(tǒng)升級時(shí)顯示的進(jìn)度條。文中的實(shí)現(xiàn)過程講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-01-01
Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效(原理解析)
這篇文章主要介紹了Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效,抖音傳送帶特效推出已經(jīng)很長一段時(shí)間了,前面也實(shí)現(xiàn)了下,最近把它整理出來了,如果你有仔細(xì)觀測傳送帶特效,就會(huì)發(fā)現(xiàn)它的實(shí)現(xiàn)原理其實(shí)很簡單,需要的朋友可以參考下2022-07-07
Jetpack Compose之選擇器使用實(shí)例講解
這篇文章主要介紹了Jetpack Compose之選擇器使用,選擇器主要是指Checkbox復(fù)選框,單選開關(guān)Switch,滑桿組件Slider等用于提供給用戶選擇一些值和程序交互的組件,比如像復(fù)選框Checkbox,可以讓用戶選擇一個(gè)或者多個(gè)選項(xiàng)2023-04-04

