需要加的权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
xml里面的代码
<?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=".MainActivity">
<Button
android:id="@+id/send_Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送" />
<ImageView
android:id="@+id/Get_Image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
设置DisplayImageOptions
package com.example.imageloader.util;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
public class ImageOptionsUtil {
public static DisplayImageOptions getDisplay() {
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
.showStubImage(0).showImageForEmptyUri(0).showImageOnFail(0).cacheOnDisk(true)
.cacheInMemory(true).imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(android.graphics.Bitmap.Config.RGB_565)
.displayer(new RoundedBitmapDisplayer(50))
.build();
return displayImageOptions;
}
}
//显示的 图片
package com.example.imageloader.imageurl;
public class ImageUrls {
public final static String[] imageUrls = new String[]{
"http://img.mukewang.com/54780ea90001f3b406000338.jpg",
"http://img.mukewang.com/547ed1c9000150cc06000338.jpg",
"http://img.mukewang.com/54214727000160e306000338.jpg",
"http://img.mukewang.com/54125edc0001ce6306000338.jpg",
"http://img.mukewang.com/548165820001b4b006000338.jpg",
"http://img.mukewang.com/53d74f960001ae9d06000338.jpg",
"http://img.mukewang.com/547d5a45000156f406000338.jpg",
"http://img.mukewang.com/549bda090001c53e06000338.jpg",
"http://img.mukewang.com/530f0ef700019b5906000338.jpg",
"http://img.mukewang.com/550a87da000168db06000338.jpg",
"http://img.mukewang.com/550a836c0001236606000338.jpg",
"http://img.mukewang.com/550a78720001f37a06000338.jpg",
"http://img.mukewang.com/5513e20600017c1806000338.jpg",
"http://img.mukewang.com/5513a1b50001752806000338.jpg",
"http://img.mukewang.com/550a33b00001738a06000338.jpg",
"http://img.mukewang.com/551380400001da9b06000338.jpg",
"http://img.mukewang.com/54c87c73000150cf06000338.jpg",
"http://img.mukewang.com/5518bbe30001c32006000338.jpg",
"http://img.mukewang.com/5518ecf20001cb4e06000338.jpg",
"http://img.mukewang.com/551916790001125706000338.jpg",
"http://img.mukewang.com/550b86560001009406000338.jpg",
"http://img.mukewang.com/551b98ae0001e57906000338.jpg",
"http://img.mukewang.com/5518c3d7000175af06000338.jpg",
"http://img.mukewang.com/551b92340001c9f206000338.jpg",
"http://img.mukewang.com/552640c300018a9606000338.jpg",
"http://img.mukewang.com/551de0570001134f06000338.jpg",
"http://img.mukewang.com/551e470500018dd806000338.jpg",
"http://img.mukewang.com/5523711700016d1606000338.jpg",
"http://img.mukewang.com/55249cf30001ae8a06000338.jpg",
"http://img.mukewang.com/55237dcc0001128c06000338.jpg"
};
}
写一个application类,进行注册,必须
package com.example.imageloader.applica;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class MyAppLica extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration loaderConfiguration = ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(loaderConfiguration);
}
}
清单文件中注册application
android:name="com.example.imageloader.applica.MyAppLica"
Activity里面的代码
package com.example.imageloader;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.example.imageloader.imageurl.ImageUrls;
import com.example.imageloader.util.ImageOptionsUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button send_Btn;
private ImageView Get_Image;
private ImageLoader mImageLoader = ImageLoader.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
send_Btn = (Button) findViewById(R.id.send_Btn);
Get_Image = (ImageView) findViewById(R.id.Get_Image);
send_Btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_Btn:
mImageLoader.displayImage(ImageUrls.imageUrls[3],Get_Image,ImageOptionsUtil.getDisplay());
break;
}
}
}
以上