Mainactivity
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.day6_imageloader.image.ImageUrl;
import com.example.day6_imageloader.util.ImageOptionsUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button All_btn;
private ImageView Get_Img;
private ImageLoader mImageLoader=ImageLoader.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
All_btn = findViewById(R.id.All_btn);
Get_Img = findViewById(R.id.Get_Img);
All_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.All_btn:
mImageLoader.displayImage(ImageUrl.imageUrls[3],Get_Img,ImageOptionsUtil.getDisplay());
break;
} }}
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);
}
}
------注意要在AndroidManifest.xml中注册
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)
//加载时显示的图片
.showImageOnLoading(R.mipmap.ic_launcher)
//加载错误时显示
.showImageOnFail(0)
//配置是否磁盘缓存
.cacheOnDisk(true) //default
//配置是否内存缓存
.cacheInMemory(true) //default
//配置图片如何缩放
.imageScaleType(ImageScaleType.IN_SAMPLE_INT) //default
//配置色彩模式
.bitmapConfig(android.graphics.Bitmap.Config.RGB_565) //default
//显示效果:圆形
//.displayer(new CircleBitmapDisplayer())
//显示效果:圆角
.displayer(new RoundedBitmapDisplayer(20))
//淡入效果
.displayer(new FadeInBitmapDisplayer(2000))
.build();
return displayImageOptions;
}
}
图片资源
public class ImageUrl {
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"
};
}
布局文件