Android asynchronous load full parse the beginning of the nonsense

Source: Internet
Author: User

Android Asynchronous loading overviewAndroid asynchronous loading is widely used in Android, in addition to avoiding network operations in the main thread, but also to avoid the display of the ANR due to too long time, increasing the smoothness of the display, especially like the ListView, GridView and other controls, If the getview time is too long, it can cause very serious lag, which greatly affects the performance.
This series will show you how to perform an asynchronous load operation 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, then download the method of use, very much, 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 simple, even did not do overtime and other processing, here to steal a lazy.
Drawable
drawable D = Drawable.createfromstream (IS, "url");

Also very simple, just need to make the next conversion.
ListviewThis article as the opening, we still come to tear down, this listview, everyone has used, the most commonly used optimization is the use of Viewholder mode for reuse, avoid repetitive 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 simple, huh, the most basic Viewholder mode uses the ListView, but here's something to note:
ViewHolder.imageView.setTag (URL);

This is actually very important, why is important we will continue to say later. In addition to this place, the rest of the section, if you can write independently, believe that you have defeated 10% of the developers, and later on how to beat the remaining 90% of the developers.
ImageImage we can from the network album inside to obtain, here accidentally find 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 (...);    }

Test the image and we'll keep ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ behind us.


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.