The Universal-Image-Loader framework teaches you to learn and simple demo

Teach you how to configure the Universal-Image-Loader framework and the first simple network loading image

The effect achieved is: Click on the empty picture Imageview to automatically load the network picture

【tips】:

There are still a lot of tutorials on the Internet, but after my own test, I found that although the writing is detailed, it still encounters a lot of problems for a novice to operate. Therefore, according to my experience, for the initial construction to the later successful operation, here is one Detailed explanation, the content will not be very detailed, mainly for introductory learning, because for novices, it is God to be able to run

===================================================================================

[1]: For the new android project, everything is based on default, which is convenient for corresponding files later, so it is recommended that you do not change it first. For Universal-Image-Loader, I believe everyone knows it well, and there are many benefits. this URL:

https://github.com/nostra13/Android-Universal-Image-Loader  English version, Chinese version can also be searched by yourself

First, we need to download Universal-Image-Loader.jar and import it into the project libs. If you download it, there is a download in the above URL.



 (The overall frame diagram is clear at a glance, you can see that the jar has imported libs)

[2] Create a new class to inherit Application, and add it to the configuration file (AndroidManifest.xml)

android:name="ImageLoaderConfig”

 Each Application can include many Activities, so the specific location is:


 Then remember to add permissions

<uses-permission android:name="android.permission.INTERNET" />  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
   

 The main reason is that it can be connected to the Internet and can be written to the external memory card SD card is generally
[3]: inherit the class of Application, because the name I created myself is ImageLoaderConfig, so I will call it that and start the configuration.

public class ImageLoaderConfig extends Application{
	public void onCreate(){
		super.onCreate();
		//Create default ImageLoader configuration parameters
		ImageLoaderConfiguration config=ImageLoaderConfiguration.createDefault(this);
		//Initialize ImageLosder and use the above default configuration
		ImageLoader.getInstance().init(config);
		
	}

}

 Most of the configurations you may see on the Internet are more complicated than this. In fact, when we actually develop, we do configure the memory size, the number of thread pools loaded and the size configuration according to our own needs. Where to save it, but today's class It's entry, the requirements are simple, rude, and most importantly, foolish.
[4]: Add Imageview to the layout file activity_main to display the loaded image

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="loadimage"
        android:src="@drawable/ic_empty" />

</RelativeLayout>

 The above src material is one I found casually, which is used to display the state of no picture

 

【5】:MainActivity.java

public class MainActivity extends Activity {



	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		}
	
	public void loadimage(View v){  
        final ImageView mImageView = (ImageView) findViewById(R.id.image);  
        // imageUrl represents the URL address of the image, and imageView represents the IMAGEVIEW control that carries the image         
        String imageUrl="http://c.hiphotos.baidu.com/lbs/pic/item/241f95cad1c8a7864bd84f5e6f09c93d71cf50eb.jpg";       
        ImageLoader.getInstance().displayImage(imageUrl, mImageView);
       }
}

 After clicking on the image (because the imageview has bound this click method in the layout file in advance),

Go to the loadimage method and load the URL of the output image. If the URL is the image that you can see on the Internet, you can copy the URL of the image by right-clicking the mouse.

A method purely for loading an image with the default configuration   :


  public void displayImage(String uri, ImageView imageView) {}

That is, the fool method, everything is configured by default, only one line is required for display, and there is no monitoring of the download progress, whether it is successful or not

The program ends here

The effect of running it is as follows:



 



 

Provide demo download: you can find it in the download attachment

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326642088&siteId=291194637