Android XUTILS3 Use-Get network pictures

Source: Internet
Author: User

This main record xutils framework of the Bitmaputils module

Bitmaputils module: For the network image loading and local image reference, using this framework can not take into account the memory overflow and picture dislocation these circumstances, and can set the size of the cache, can be said to be very convenient.

Loading a picture requires only one x.image (). The bind (,) method is fine .

There are two parameters in the Bind method above, the first parameter is the ImageView object, the second argument is the URL string address

Below I use a custom listview to load the network picture, simple to xutils this bitmaputils module to be used

Step one: Of course, you should first introduce a rack package, and then initialize the xutils in the subclass of the global class application class

Here I have customized a MyApp class to inherit application

Package com.contentprovide.liuliu.myapplication;import Android.app.application;import org.xutils.x; /*  */Publicclass  MyApp extends application    {    @Override  publicvoid  onCreate () {        super.oncreate ();        X.ext.init (   This );    }}

One place to note is that the MyApp class has to be registered in the Androidmanifest file.

Step two: Next we will be able to put the picture of the ListView related content to write good

First, the contents of the Activity_main.xml file

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:app= "Http://schemas.android.com/apk/res-auto"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
tools:context= "Com.contentprovide.liuliu.myapplication.MainActivity" >

<listview
Android:id= "@+id/my_list"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:dividerheight= "10DP" ></ListView>

</LinearLayout>

Then the layout content of the custom list in the ListView, Item.xml file

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Horizontal"> <ImageView Android:id="@+id/my_ima"Android:layout_width="200DP"Android:layout_height="200DP"/> <TextView Android:id="@+id/my_te"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_marginleft="30DP"Android:layout_margintop="30DP"Android:textcolor="#000000"/></linearlayout>

Step three: Now you can load the network picture in the Mainactivity.java file operation

Package Com.contentprovide.liuliu.myapplication;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.listview;import android.widget.textview;import org.xutils.image.imageoptions;import org.xutils.x; Public classMainactivity extends Appcompatactivity {//defines a Imageoptions object that is used to set more propertiesimageoptions imageoptions;    ListView my_list;  Publicstring[] str = {"Picture One","Picture Two","Picture Three","Picture Four"};  PublicString[] pic = {"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1690800971,3082549883&fm=27&gp=0.jpg ",            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3932266323,1616123448&fm=27&gp=0.jpg ",            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3555104010,781975830&fm=27&gp=0.jpg",            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3725021825,1940836077&fm=27&gp=0.jpg "}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); My_list=(ListView) Findviewbyid (r.id.my_list); Myadapt myadapt=Newmyadapt ();    My_list.setadapter (MYADAPT); }    //create an adapter to add content to your custom listview     Public classMyadapt extends Baseadapter {@Override Public intGetCount () {returnstr.length; } @Override PublicObject GetItem (inti) {returni; } @Override Public LongGetitemid (inti) {returni; } @Override PublicView GetView (intI, view view, ViewGroup ViewGroup)            {Myholder myholder; if(View = =NULL) {//Convert custom views to view to reflect the flexibility of custom adaptersView = Layoutinflater. from(Getapplicationcontext ()). Inflate (R.layout.item,NULL); Myholder=NewMyholder (); Myholder.my_ima=View.findviewbyid (R.id.my_ima); Myholder.my_te=View.findviewbyid (R.id.my_te);            View.settag (Myholder); } Else{Myholder=(Myholder) View.gettag (); }//Display the default picture by Xutils object settings when the picture fails to load            imageoptions = new Imageoptions.builder (). Setfailuredrawableid (R.mipmap.ic_launcher). build (); //set a picture by Xutils to item in the ListView x.image (). Bind (Myholder.my_ima, pic[i],imageoptions);            Myholder.my_te.setText (Str[i]); returnview; }        //defines an inner class that gets the spatial object in the custom item        classMyholder {ImageView My_ima;        TextView My_te; }    }}

The code in the Mainactivity file above that actually loads the image is a few lines of red code, and the rest is about the use of the ListView.

The following is the final implementation effect:

Because I set the default picture when I failed to load the picture, so I turned off the network reload effect is the following

Here are some ways to use the objects of the Imageoptions class

. SetSize ( -, -)                //if the size of the ImageView is not defined as wrap_content, do not crop.. Setcrop (true)                //loading or ScaleType of the wrong picture//. Setplaceholderscaletype (ImageView.ScaleType.MATRIX). Setimagescaletype (ImageView.ScaleType.CENTER_CROP)//set up a picture in the loading process. Setloadingdrawableid (R.mipmap.ic_launcher)//to set a picture after a failed load. Setfailuredrawableid (R.mipmap.ic_launcher)//set up to use the cache. Setusememcache (true)                //Set Support GIF. Setignoregif (false)                //Set Display circle picture. Setcircular (true)
Set the arc of a corner
. Setradius (DENSITYUTIL.DIP2PX (5))

Android XUTILS3 Use-Get network pictures

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.