Android implementation reads the picture from the cache and asynchronously loads the function class _android

Source: Internet
Author: User
Tags sqlite database stub

This article illustrates an Android implementation that reads pictures from the cache and loads the function classes asynchronously. Share to everyone for your reference, specific as follows:

In the microblog list of Sina Weibo pictures, in order to speed up its display also in order to speed up the program's response, you can refer to the picture asynchronous load class implementation.

The public class Asyncimageloader {//softreference is a soft reference, is for better system recycling variables private hashmap<string, softreference<drawab
  Le>> Imagecache;
  Public Asyncimageloader () {Imagecache = new hashmap<string, softreference<drawable>> ();
    Public drawable loaddrawable (final String imageurl,final ImageView ImageView, final imagecallback imagecallback) { if (Imagecache.containskey (IMAGEURL)) {//get softreference<drawable> SoftReference = Imagecache.get from cache (
      IMAGEURL);
      drawable drawable = Softreference.get ();
      if (drawable!= null) {return drawable; } final Handler Handler = new Handler () {public void Handlemessage (Message message) {Imagecallba
      Ck.imageloaded ((drawable) message.obj, Imageview,imageurl);
    }
    }; Create a new thread to download a new threads () {@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; }//Callback interface public interface Imagecallback {public void imageloaded (drawable imagedrawable,imageview ImageView, Stri
  ng ImageUrl);

 }
}

The

methods used in adapter are:

public class Weiboadapater extends baseadapter{private Asyncimageloader asyncimageloader;
    @Override public int GetCount () {//TODO auto-generated a stub return wblist.size (); @Override public Object getitem (int position) {//TODO auto-generated method stub return Wblist.get
    (position);
    @Override public long getitemid (int position) {//TODO auto-generated method stub return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {//TODO Auto-generat
      ed method Stub Convertview = Layoutinflater.from (Getapplicationcontext ()). Inflate (R.layout.weibo, NULL);
      Weiboholder WH = new Weiboholder ();
      Wh.wbicon = (ImageView) Convertview.findviewbyid (R.id.wbicon);
      Wh.wbtext = (TextView) Convertview.findviewbyid (R.id.wbtext);
      Wh.wbtime = (TextView) Convertview.findviewbyid (r.id.wbtime); Wh.wbuser = (TextView) convertview.findviewByid (R.id.wbuser);
      Wh.wbimage= (ImageView) Convertview.findviewbyid (r.id.wbimage);
      Weiboinfo wb = wblist.get (position);
        if (WB!= null) {Convertview.settag (Wb.getid ());
        Wh.wbuser.setText (Wb.getusername ());
        Wh.wbtime.setText (Wb.gettime ());
        Wh.wbtext.setText (Wb.gettext (), TextView.BufferType.SPANNABLE); drawable cachedimage = asyncimageloader.loaddrawable (Wb.getusericon (), Wh.wbicon, New Imagecallback () {public VO ID imageloaded (drawable imagedrawable,imageview imageview,string imageUrl) {imageview.setimagedrawable (imageDr
          awable);
        }
        });
        if (cachedimage = = null) {Wh.wbicon.setImageResource (R.drawable.usericon);
        }else{wh.wbicon.setImageDrawable (cachedimage);
    } return Convertview;

 }
}

More interested readers of Android related content can view the site: "Summary of the use of Android controls," "The Summary of Android view tips," "Android File Operation tips Summary", "Android operation SQLite Database Skills Summary", " Android operations JSON format Data tips summary, "Android Database Operation skills Summary", "Android programming activity Operation Skills Summary", "Android programming development of SD card operation method Summary", "Android Development and advanced tutorial And the Android resource Operation Skills Summary

I hope this article will help you with the Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.