Android uses volley asynchronous loading data Complete concrete Demo sample (ii)

Source: Internet
Author: User

Mainactivity such as the following:
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 of simple use of volley, see: * http://blog.csdn.net/lfdfhl/article/ details/12223345 * Recently Guolin wrote a few articles specifically introduced volley, so this is more systematic to learn a bit. * In this demo sample, mainly includes: * 1 use imagerequest for imageview load network picture * 2 use Imageloader for ImageView load network picture * 3 Use Networkimageview to load network images 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 code analysis, please see: * http://blog.csdn.net/guolin_blog/article/details/17656437 * 4 Your own definition of xmlrequest and gsonrequest, please 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 {p Rivate Context mcontext;private 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 the network picture 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 the network picture 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://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 the network image 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 such as the following:

<?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 "> <textview android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "learning Volley (ii)" Android:layou T_centerhorizontal= "true"/> <imageview android:id= "@+id/imageview" android:layout_width= " Wrap_content "android:layout_height=" Wrap_content "android:layout_centerhorizontal=" true "Android:lay out_margintop= "50dip"/> <com.android.volley.toolbox.networkimageview android:id= "@+id/networkI Mageview "android:layout_width=" 120dip "android:layout_height=" 120dip "Android:layout_centerhorizonta L= "true" android:layout_margintop= "150dip"/></relativelayout>



Android uses volley to asynchronously load data complete concrete Demo sample (ii)

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.