android 异步上传图片,Android实现异步加载图片

ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,不用让用户等待下去,下面就说实现方法,先贴上主方法的代码:

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 >>  p_w_picpathCache;

public  AsyncImageLoader() {

p_w_picpathCache  =   new  HashMap < String, SoftReference < Drawable >> ();

}

public  Drawable loadDrawable( final  String p_w_picpathUrl,  final  ImageCallback p_w_picpathCallback) {

if  (p_w_picpathCache.containsKey(p_w_picpathUrl)) {

SoftReference < Drawable >  softReference  =  p_w_picpathCache.get(p_w_picpathUrl);

Drawable drawable  =  softReference.get();

if  (drawable  !=   null ) {

return  drawable;

}

}

final  Handler handler  =   new  Handler() {

public   void  handleMessage(Message message) {

p_w_picpathCallback.p_w_picpathLoaded((Drawable) message.obj, p_w_picpathUrl);

}

};

new  Thread() {

@Override

public   void  run() {

Drawable drawable  =  loadImageFromUrl(p_w_picpathUrl);

p_w_picpathCache.put(p_w_picpathUrl,  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  p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl);

}

}

以上代码是实现异步获取图片的主方法,SoftReference是软引用,是为了更好的为了系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。

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 >  p_w_picpathAndTexts, ListView listView) {

super (activity,  0 , p_w_picpathAndTexts);

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.p_w_picpath_and_text_row,  null );

viewCache  =   new  ViewCache(rowView);

rowView.setTag(viewCache);

}  else  {

viewCache  =  (ViewCache) rowView.getTag();

}

ImageAndText p_w_picpathAndText  =  getItem(position);

//  Load the p_w_picpath and set it on the ImageView

String p_w_picpathUrl  =  p_w_picpathAndText.getImageUrl();

ImageView p_w_picpathView  =  viewCache.getImageView();

p_w_picpathView.setTag(p_w_picpathUrl);

Drawable cachedImage  =  asyncImageLoader.loadDrawable(p_w_picpathUrl,  new  ImageCallback() {

public   void  p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl) {

ImageView p_w_picpathViewByTag  =  (ImageView) listView.findViewWithTag(p_w_picpathUrl);

if  (p_w_picpathViewByTag  !=   null ) {

p_w_picpathViewByTag.setImageDrawable(p_w_picpathDrawable);

}

}

});

if  (cachedImage  ==   null ) {

p_w_picpathView.setImageResource(R.drawable.default_p_w_picpath);

} else {

p_w_picpathView.setImageDrawable(cachedImage);

}

//  Set the text on the TextView

TextView textView  =  viewCache.getTextView();

textView.setText(p_w_picpathAndText.getText());

return  rowView;

}

}

ImageAndTextListAdapter是实现ListView的Adapter,里面有个技巧就是p_w_picpathView.setTag(p_w_picpathUrl),setTag是存储数据的,这样是为了保证在回调函数时,listview去更新自己对应item,大家仔细阅读就知道了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值