ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,不用让用户等待下去,下面就说实现方法,先贴上主方法的代码: ? package? cn.wangmeng.test; import? java.io.IOException; import? android.graphics.drawable.Drawable; public?? class? AsyncImageLoader { ????? private? HashMap < String, SoftReference < Drawable >>? imageCache; } 以上代码是实现异步获取图片的主方法,SoftReference是软引用,是为了更好的为了系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。 ViewCache是辅助获取adapter的子元素布局 package? cn.wangmeng.test; import? java.util.List; import? cn.wangmeng.test.AsyncImageLoader.ImageCallback; import? android.app.Activity; public?? class? ImageAndTextListAdapter? extends? ArrayAdapter < ImageAndText >? { ???????? private? ListView listView; ???????? public? ImageAndTextListAdapter(Activity activity, List < ImageAndText >? imageAndTexts, ListView listView) { ???????? public? View getView( int? position, View convertView, ViewGroup parent) { ???????????? //? Inflate the views from XML? ???????????? //? Load the image and set it on the ImageView? ???????????? return? rowView; } ImageAndTextListAdapter是实现ListView的Adapter,里面有个技巧就是imageView.setTag(imageUrl),setTag是存储数据的,这样是为了保证在回调函数时,listview去更新自己对应item,大家仔细阅读就知道了。 方法二: package com.android.dieke.util; import java.io.FilterInputStream; import org.apache.http.HttpEntity; import android.graphics.Bitmap; /** ??? public enum Mode { ??? private Mode mode = Mode.NO_ASYNC_TASK; ??? public Bitmap download(String url, ImageView imageview) { ??? private void forceDownload(String url, ImageView imageview) { ??? private static boolean cancelPotentialDownload(String url, ??? private static BitmapDownloaderTask getBitmapDownloaderTask( ??? Bitmap downloadBitmap(String url) { ??????????????? InputStream inputStream = null; ??? static class FlushedInputStream extends FilterInputStream { ??????? public long skip(long n) throws IOException { ??? class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> { ??????? public BitmapDownloaderTask(ImageView imageview) { ??????? } ??????? @Override ??????? protected void onPostExecute(Bitmap bitmap) { ??? static class DownloadedDrawable extends ColorDrawable { ??????? public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) { ??????? public BitmapDownloaderTask getBitmapDownloaderTask() { ??? public void setMode(Mode mode) { ??? private static final int HARD_CACHE_CAPACITY = 10; ??? private final static ConcurrentHashMap<String, SoftReference<Bitmap>> sSoftBitmapCache = new ConcurrentHashMap<String, SoftReference<Bitmap>>( ??? private void addBitmapToCache(String url, Bitmap bitmap) { ??? private Bitmap getBitmapFromCache(String url) { ??? public void clearCache() { ??? private void resetPurgeTimer() { 方法三: /*package com.android.dieke.util; import java.net.URL; import android.content.Context; import com.dieke._class.FoodInfo; public class ImageLoadTask extends AsyncTask<Void, Void, Void> { ??? private GridAllFoodAdapter adapter; ??? public ImageLoadTask(Context context, GridAllFoodAdapter adapter) { ??? @Override ??? } ??? @Override ??????? for (int i = 0; i < adapter.getCount(); i++) { ??????????? try { ??? @Override ??????? adapter.notifyDataSetChanged(); ??? @Override
import? java.io.InputStream;
import? java.lang.ref.SoftReference;
import? java.net.MalformedURLException;
import? java.net.URL;
import? java.util.HashMap;
import? android.os.Handler;
import? android.os.Message;
??????
????? 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);
???????? }
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
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;
???????? private? AsyncImageLoader asyncImageLoader;
???????????? super (activity,? 0 , imageAndTexts);
???????????? this .listView? =? listView;
??????????? asyncImageLoader? =?? new? AsyncImageLoader();
??????? }
??????????? Activity activity? =? (Activity) getContext();
??????????? 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);
??????????? 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());
??????? }
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
?* This helper class download images from the Internet and binds those with the
?* provided ImageView.
?* <p>
?* It requires the INTERNET permission, which should be added to your
?* application's manifest * file.
?* </p>
?* * * A local cache of downloaded images is maintained internally to improve
?* performance.
?*/
public class ImageDownloaderTask {
??? private static final String LOG_TAG = "ImageDowloader";
??????? NO_ASYNC_TASK, NO_DOWNLOADED_DRAWABLE, CORRECT
??? }
??????? resetPurgeTimer();
??????? Bitmap bitmap = getBitmapFromCache(url);
??????? if (bitmap == null) {
??????????? forceDownload(url, imageview);
??????? } else {
??????????? cancelPotentialDownload(url, imageview);
??????????? imageview.setImageBitmap(bitmap);
??????? }
??????? return bitmap;
??? }
??????? if (url == null) {
??????????? imageview.setImageDrawable(null);
??????????? return;
??????? }
??????? if (cancelPotentialDownload(url, imageview)) {
??????????? switch (mode) {
??????????? case NO_ASYNC_TASK:
??????????????? Bitmap bitmap = downloadBitmap(url);
??????????????? addBitmapToCache(url, bitmap);
??????????????? imageview.setImageBitmap(bitmap);
??????????????? break;
??????????? case NO_DOWNLOADED_DRAWABLE:
??????????????? imageview.setMinimumHeight(100);
??????????????? BitmapDownloaderTask task = new BitmapDownloaderTask(imageview);
??????????????? task.execute(url);
??????????????? break;
??????????? case CORRECT:
??????????????? task = new BitmapDownloaderTask(imageview);
??????????????? DownloadedDrawable downloadedDrawable = new DownloadedDrawable(
??????????????????????? task);
??????????????? imageview.setImageDrawable(downloadedDrawable);
??????????????? imageview.setMinimumHeight(156);
??????????????? task.execute(url);
??????????????? break;
??????????? }
??????? }
??? }
??????????? ImageView imageview) {
??????? BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageview);
??????? if (bitmapDownloaderTask != null) {
??????????? String bitmapUrl = bitmapDownloaderTask.url;
??????????? if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
??????????????? bitmapDownloaderTask.cancel(true);
??????????? } else {
??????????????? // The same URL is already being downloaded.
??????????????? return false;
??????????? }
??????? }
??????? return true;
??? }
??????????? ImageView imageview) {
??????? if (imageview != null) {
??????????? Drawable drawable = imageview.getDrawable();
??????????? if (drawable instanceof DownloadedDrawable) {
??????????????? DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
??????????????? return downloadedDrawable.getBitmapDownloaderTask();
??????????? }
??????? }
??????? return null;
??? }
??????? final int IO_BUFFER_SIZE = 4 * 1024;
??????? final HttpClient client = new DefaultHttpClient();
??????????????? //: AndroidHttpClient.newInstance("Android");
??????? final HttpGet getRequest = new HttpGet(url);
??????? try {
??????????? HttpResponse response = client.execute(getRequest);
??????????? final int statusCode = response.getStatusLine().getStatusCode();
??????????? if (statusCode != HttpStatus.SC_OK) {
??????????????? Log.v("ImageDownloader", "Error" + statusCode
??????????????????????? + "while restrieving bitmap from" + url);
??????????????? return null;
??????????? }
??????????? final HttpEntity entity = response.getEntity();
??????????? if (entity != null) {
??????????????? try {
??????????????????? inputStream = entity.getContent();
??????????????????? return BitmapFactory.decodeStream(new FlushedInputStream(
??????????????????????????? inputStream));
??????????????? } finally {
??????????????????? if (inputStream != null) {
??????????????????????? inputStream.close();
??????????????????? }
??????????????????? entity.consumeContent();
??????????????? }
??????????? }
??????? } catch (IOException e) {
??????????? getRequest.abort();
??????????? Log.v(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
??????? } catch (IllegalStateException e) {
??????????? getRequest.abort();
??????????? Log.w(LOG_TAG, "Incorrect URL: " + url);
??????? } catch (Exception e) {
??????????? getRequest.abort();
??????????? Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
??????? } finally {
??????????? if ((client instanceof AndroidHttpClient)) {
??????????????? ((AndroidHttpClient) client).close();
??????????? }
??????? }
??????? return null;
??? }
??????? public FlushedInputStream(InputStream inputStream) {
??????????? super(inputStream);
??????? }
??????????? long totalBytesSkipped = 0L;
??????????? while (totalBytesSkipped < n) {
??????????????? long bytesSkipped = in.skip(n - totalBytesSkipped);
??????????????? if (bytesSkipped == 0L)
??????????????????? break;
??????????????? totalBytesSkipped += bytesSkipped;
??????????? }
??????????? return totalBytesSkipped;
??????? }
??? }
??????? private String url;
??????? private final WeakReference<ImageView> imageViewReference;
??????????? imageViewReference = new WeakReference<ImageView>(imageview);
??????? protected Bitmap doInBackground(String... params) {
??????????? // TODO Auto-generated method stub
??????????? url = params[0];
??????????? return downloadBitmap(url);
??????? }
??????????? if (isCancelled()) {
??????????????? bitmap = null;
??????????? }
??????????? addBitmapToCache(url, bitmap);
??????????? if (imageViewReference != null) {
??????????????? ImageView imageview = imageViewReference.get();
??????????????? BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageview);
??????????????? if ((this == bitmapDownloaderTask) || (mode != Mode.CORRECT)) {
??????????????????? imageview.setImageBitmap(bitmap);
??????????????? }
??????????? }
??????? }
??? }
??????? private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
??????????? super(Color.BLACK);
??????????? bitmapDownloaderTaskReference = new WeakReference<BitmapDownloaderTask>(
??????????????????? bitmapDownloaderTask);
??????? }
??????????? return bitmapDownloaderTaskReference.get();
??????? }
??? }
??????? this.mode = mode;
??????? clearCache();
??? }
??? private static final int DELAY_BEFORE_PURGE = 10 * 1000; // in milliseconds
??? private final HashMap<String, Bitmap> sHardBitmapCache = new LinkedHashMap<String, Bitmap>(
??????????? HARD_CACHE_CAPACITY / 2, 0.75f, true) {
??????? @Override
??????? protected boolean removeEldestEntry(
??????????????? LinkedHashMap.Entry<String, Bitmap> eldest) {
??????????? if (size() > HARD_CACHE_CAPACITY) {
??????????????? // Entries push-out of hard reference cache are transferred to
??????????????? // soft reference cache
??????????????? sSoftBitmapCache.put(eldest.getKey(),
??????????????????????? new SoftReference<Bitmap>(eldest.getValue()));
??????????????? return true;
??????????? } else
??????????????? return false;
??????? }
??? };
??????????? HARD_CACHE_CAPACITY / 2);
??? private final Handler purgeHandler = new Handler();
??? private final Runnable purger = new Runnable() {
??????? public void run() {
??????????? clearCache();
??????? }
??? };
??????? if (bitmap != null) {
??????????? synchronized (sHardBitmapCache) {
??????????????? sHardBitmapCache.put(url, bitmap);
??????????? }
??????? }
??? }
??????? synchronized (sHardBitmapCache) {
??????????? final Bitmap bitmap = sHardBitmapCache.get(url);
??????????? if (bitmap != null) {
??????????????? sHardBitmapCache.remove(url);
??????????????? sHardBitmapCache.put(url, bitmap);
??????????????? return bitmap;
??????????? }
??????? }
??????? SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
??????? if (bitmapReference != null) {
??????????? final Bitmap bitmap = bitmapReference.get();
??????????? if (bitmap != null) {
??????????????? return bitmap;
??????????? } else {
??????????????? sSoftBitmapCache.remove(url);
??????????? }
??????? }
??????? return null;
??? }
??????? sHardBitmapCache.clear();
??????? sSoftBitmapCache.clear();
??? }
??????? purgeHandler.removeCallbacks(purger);
??????? purgeHandler.postDelayed(purger, DELAY_BEFORE_PURGE);
??? }
}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import com.dieke.adapter.GridAllFoodAdapter;
??? private static final String TAG = ImageLoadTask.class.getSimpleName();
??????? Log.v(TAG, "ImageLoadTask()");
??????? this.adapter = adapter;
??? }
??? protected void onPreExecute() {
??????? Log.v(TAG, "onPreExecute()");
??? protected Void doInBackground(Void... voids) {
??????? Log.v(TAG, "doInBackground()");
??????????? FoodInfo bean = adapter.getItem(i);
??????????????? Log.d(TAG, bean.getFoodImgSrc());
??????????????? URL url = new URL(bean.getFoodImgSrc());
??????????????? Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
??????????????? Log.d(TAG, (bitmap == null) + "");
??????????????? if (bitmap != null) {
??????????????????? bean.setBitmap(bitmap);
??????????????????? publishProgress();
??????????????? }
??????????? } catch (Exception e) {
??????????? }
??????? }
??????? return null;
??? }
??? public void onProgressUpdate(Void... voids) {
??????? Log.v(TAG, "onProgressUpdate()");
??????? if (isCancelled())
??????????? return;
??? }
??? protected void onPostExecute(Void result) {
??????? Log.v(TAG, "onPostExecute()");
??? }
}
详细解决方案
android兑现异步加载图片
热度:77 发布时间:2016-05-01 17:27:32.0
1 楼 anyang763 2011-12-01
2 楼 herozhou1314 2012-04-27
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- android 关于服务连接的疑义
- android 两个activity如何通信
- android 怎么实现对view的放大和缩小
- android 教程解决方法
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java)中的java.net能不能和c#的system.net.sockets进行tcp通信,该如何解决
- android ListView 中的onItemClick Intent 没法跳转
- android(java) 中文乱码的有关问题
- c#c++,android,ios(iphone),php,java视屏课程 散分
- android Post文件到ASP.NET的有关问题,能收到参数收不到文件
- RIM 替 Android 开发者提供免费的 PlayBook!2月13日前
- android 动态设立控件高度
- Android test project 编译方法
- android -相机使用教程(1)解决方法