Android asynchronous load full parse the beginning of the nonsense

Source: Internet
Author: User

Android Asynchronous Load OverviewAndroid Async onboarding is widely used in Android, except for the avoidance of network operations in the main thread. In order to avoid the display when the time is too long to cause the ANR, add the smoothness of the display, especially like the ListView, the GridView control. Assuming that the getview time is too long, it can cause serious lag, which affects performance.
This series will show you how to do asynchronous onboarding in Android and use the ListView as the object of your presentation.
How to download imagesDownload nature is required to use the network, the use of the network can not be in the main thread. The main thread will explode. So we have to download the image in the non-main thread. Ok. So download the method of use. Many, here is a simple list of several
HttpURLConnection
private static Bitmap Getbitmapfromurl (String urlstring) {    Bitmap Bitmap;    InputStream is = null;    try {        url url = new URL (urlstring);        HttpURLConnection conn = (httpurlconnection) url.openconnection ();        is = new Bufferedinputstream (Conn.getinputstream ());        Bitmap = Bitmapfactory.decodestream (is);        Conn.disconnect ();        return bitmap;    } catch (Exception e) {        e.printstacktrace ();    } finally {        try {            if (is! = null)                is.close ();        } catch (IOException e) {        }    }    return null;}

Very easy. Did not even do time-outs and other processing. Here to steal a lazy.
Drawable
drawable D = Drawable.createfromstream (IS, "url");

Also very easy, just need to make the next conversion.
ListviewThis one begins. We still come to rip, this listview, everyone has used, the most often used optimization is the use of Viewholder mode for reuse, avoid repeated inflate and findviewbyid and affect efficiency, I believe most of the developers are already familiar with, Here we still put the adapter code:
Package Com.imooc.listviewacyncloader;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.ImageView    ; Import Java.util.list;public class Myadapternotusecaches extends Baseadapter {private Layoutinflater minflater;    Private list<string> Mdata;        Public Myadapternotusecaches (context context, list<string> data) {this.mdata = data;    Minflater = Layoutinflater.from (context);    } @Override public int getcount () {return mdata.size ();    } @Override public Object getItem (int position) {return mdata.get (position);    } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {String URL = mdata.get (PO        Sition);        Viewholder viewholder = null; if (Convertview = = null) {VIEWHolder = new Viewholder ();            Convertview = minflater.inflate (R.layout.listview_item, NULL);            Viewholder.imageview = (ImageView) Convertview.findviewbyid (R.id.iv_lv_item);        Convertview.settag (Viewholder);        } else {Viewholder = (Viewholder) convertview.gettag ();        } viewHolder.imageView.setTag (URL);        ViewHolder.imageView.setImageResource (R.drawable.ic_launcher);    return convertview;    } public class viewholder{public ImageView ImageView; }}

It's really easy, huh? The main Viewholder mode uses the ListView. Just, here's something to note:
ViewHolder.imageView.setTag (URL);

This is actually important and why it's important we'll continue to say it later.

In addition to this place, the rest of the section, suppose you can write it independently. I believe you've beaten 10% of developers, and we'll talk about how to beat the remaining 90% of developers later.


ImageImage we can get from the network album inside, here accidentally found Guo Shen of a blog inside the image address, it is shameless to take to use:
public class Images {public    final static string[] Image_urls = new string[] {            "http://img.my.csdn.net/uploads/20 1407/26/1406383299_1976.jpg ",            " http://img.my.csdn.net/uploads/201407/26/1406383291_6518.jpg ",            " http.// Img.my.csdn.net/uploads/201407/26/1406383291_8239.jpg ",            " http://img.my.csdn.net/uploads/201407/26/ 1406383290_9329.jpg ",            " http://img.my.csdn.net/uploads/201407/26/1406383290_1042.jpg ",            " http.// Img.my.csdn.net/uploads/201407/26/1406383275_3977.jpg ",            " http://img.my.csdn.net/uploads/201407/26/ 1406383265_8550.jpg ",
......
So we can import these images in mainactivity:
Package Com.imooc.listviewacyncloader;import Android.app.activity;import Android.os.bundle;import Android.widget.listview;import Java.util.arrays;import Java.util.list;public class Mainactivity extends Activity {    private ListView Mlistview;    Private list<string> Mdata;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Mlistview = (ListView) Findviewbyid (r.id.lv);        Mdata = Arrays.aslist (images.image_urls);        Mlistview.setadapter (...);    }

We're going to go back to ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.


my github
My video mu-class network





Android asynchronous load full parse the beginning of the nonsense

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.