我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

android实用技巧:android实现listview异步加载图片-创新互联

针对listview异步加载图片这个问题,麦子学院android开发老师讲了一种非常实用的方法,麦子学院android开发老师说凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码:

成都创新互联公司始终坚持【策划先行,效果至上】的经营理念,通过多达十余年累计超上千家客户的网站建设总结了一套系统有效的全网营销解决方案,现已广泛运用于各行各业的客户,其中包括:成都资质代办等企业,备受客户赞誉。

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

  public AsyncImageLoader() {

   p_w_picpathCache = new HashMap>();

  }

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

    if (p_w_picpathCache.containsKey(p_w_picpathUrl)) {

      SoftReference 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));

        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线程。

几个辅助类文件:

package cn.wangmeng.test;

public class ImageAndText {

  private String p_w_picpathUrl;

  private String text;

  public ImageAndText(String p_w_picpathUrl, String text) {

    this.p_w_picpathUrl = p_w_picpathUrl;

    this.text = text;

  }

  public String getImageUrl() {

    return p_w_picpathUrl;

  }

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

  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 (p_w_picpathView == null) {

      p_w_picpathView = (ImageView) baseView.findViewById(R.id.p_w_picpath);

    }

    return p_w_picpathView;

  }

}

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 {

  private ListView listView;

  private AsyncImageLoader asyncImageLoader;

  public ImageAndTextListAdapter(Activity activity, List 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,大家仔细阅读就知道了。

最后贴出布局文件:

       android:orientation="horizontal"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content">

    

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          />

    

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"/>

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:android实用技巧:android实现listview异步加载图片-创新互联
文章地址:http://mswzjz.cn/article/dgegic.html

其他资讯