Android leverages volley asynchronous load data Full Detail example (ii)

Source: Internet
Author: User

Mainactivity as follows:
Package Cc.y;import Android.app.activity;import Android.content.context;import android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.os.bundle;import Android.util.lrucache;import Android.widget.imageview;import Com.android.volley.requestqueue;import Com.android.volley.response;import Com.android.volley.volleyerror;import Com.android.volley.toolbox.imageloader;import Com.android.volley.toolbox.imageloader.imagecache;import Com.android.volley.toolbox.ImageLoader.ImageListener; Import Com.android.volley.toolbox.imagerequest;import Com.android.volley.toolbox.networkimageview;import com.android.volley.toolbox.volley;/** * Demo Description: * Last year a project was simply used volley, see: * http://blog.csdn.net/lfdfhl/article/ details/12223345 * Recently Guolin wrote a few articles detailed introduction volley, so in this comparative systematic study. * In this example, the main include: * 1 use imagerequest for imageview load network picture * 2 use Imageloader for ImageView load network picture * 3 Use Networkimageview to load network pictures for ImageView * * NOTE: * 1 introduction of the JAR package Volley.jar * 2 network permissions <uses-permission android:name= "Android.perm Ission. InterneT "/> * 3 volley source analysis, see: * http://blog.csdn.net/guolin_blog/article/details/17656437 * 4 custom XMLRequest and Gsonrequest , see: * http://blog.csdn.net/guolin_blog/article/details/17612763 * * Learning materials: * http://blog.csdn.net/guolin_blog/ article/details/17482165 * Thank you very much * */public class Mainactivity extends Activity {private Context MCONTEXT;PR Ivate requestqueue mrequestqueue;private String murl= "http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";p rivate ImageView Mimageview;private imagerequest mimagerequest;private imageloader mimageloader;private ImageListener mImageListener;    Private Networkimageview Mnetworkimageview;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        Init ();        Volley_imagerequest ();        Volley_imageloader ();    Volley_networkimageview ();    } private void Init () {mcontext=this;    mimageview= (ImageView) Findviewbyid (R.id.imageview);mnetworkimageview= (Networkimageview) Findviewbyid (R.id.networkimageview); }//Use Imagerequest to load network pictures for imageview private void Volley_imagerequest () {mrequestqueue = Volley.newrequestqueue (mconte XT); mimagerequest = new Imagerequest (Murl, New response.listener<bitmap> () {@Override public void Onresponse (BITM AP Response) {Mimageview.setimagebitmap (response);}}, 0, 0, config.argb_8888, new Response.errorlistener () {@O verridepublic void Onerrorresponse (volleyerror error) {System.out.println ("Get picture failed, so load a default picture"); Mimageview.setimagedrawable (Mcontext.getresources (). getdrawable (R.drawable.ic_launcher));}); Mrequestqueue.add (mimagerequest);} Use Imageloader to load network pictures for imageview private void Volley_imageloader () {mrequestqueue = Volley.newrequestqueue (Mcontext); Mimageloader=new Imageloader (Mrequestqueue, New Imagecacheimpl ()); Mimagelistener=imageloader.getimagelistener ( Mimageview, R.drawable.ic_launcher, R.drawable.ic_launcher); Mimageloader.get (Murl, mimagelistener);//mImageLoadEr.get (Murl, mimagelistener,250,250);} Using LRUCache to implement a picture caching mechanism//See also://http://blog.csdn.net/lfdfhl/article/details/18350601private class Imagecacheimpl  Implements Imagecache {private lrucache<string, bitmap> mlrucache;public Imagecacheimpl () {int maxSize = 15 * 1024 * 1024;mlrucache = new lrucache<string, bitmap> (maxSize) {@Overrideprotected int sizeOf (String key, Bitmap Bitmap) {R Eturn bitmap.getrowbytes () * Bitmap.getheight ();};} @Overridepublic Bitmap getbitmap (String URL) {return mlrucache.get (URL);} @Overridepublic void Putbitmap (String url, Bitmap Bitmap) {mlrucache.put (URL, Bitmap);}} Use Networkimageview to load network pictures for imageview private void Volley_networkimageview () {mrequestqueue = Volley.newrequestqueue ( Mcontext); mimageloader=new Imageloader (Mrequestqueue, New Imagecacheimpl ()); Mnetworkimageview.setdefaultimageresid (R.drawable.ic_launcher); Mnetworkimageview.seterrorimageresid ( R.drawable.ic_launcher); Mnetworkimageview.setimageurl (Murl, Mimageloader);}}

Main.xml as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "study Xi Volley (ii) "android:layout_centerhorizontal=" true "/> <imageview android:id=" @+id/imagevi EW "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_centerhor         Izontal= "true" android:layout_margintop= "50dip"/> <com.android.volley.toolbox.networkimageview        Android:id= "@+id/networkimageview" android:layout_width= "120dip" android:layout_height= "120dip" Android:layout_centerhorizontal= "true" android:layout_margintop= "150dip"/></relativelayout>


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.