首先要先添加一个ImageLoader依赖
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
创建universalimageloader的封装类
public static DisplayImageOptions MyDisplayImageOptions() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) //设置图片在下载期间显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher) //设置图片加载/解码过程中错误时候显示的图片
.cacheInMemory(true)//设置下载的图片是否缓存在内存中
.cacheOnDisk(true)
.considerExifParams(true) //是否考虑JPEG图像EXIF参数(旋转,翻转)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
.displayer(new RoundedBitmapDisplayer(360))//是否设置为圆角,弧度为多少
.displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
.build();//构建完成
return options;
}
上面的代码,也可以放入Activity中,这已经是一个方法可直接使用代码进行调用
String imageUri = "http://d.hiphotos.baidu.com/image/pic/item/d52a2834349b033b58ef816c1fce36d3d539bd24.jpg";
ImageLoader.getInstance().displayImage(imageUri, meFragmentImageView , MyDisplayImageOptions());
此Url为图片地址,可以直接在百度搜索,或借口内的url地址进行更换
meFragmentImageView 为xml中的Image组件的id
MyDisplayImageOptions()为上面的方法
这个时候运行可能会出现问题,提示ImageLoader没有进行初始化,一定不要忘记
private void initImageLoader() {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).imageDownloader(
new BaseImageDownloader(this, 60 * 1000, 60 * 1000)) // connectTimeout超时时间
.build();
ImageLoader.getInstance().init(config);
}
可以在Activity中调用此方法将ImageLoader进行初始化
如果还有更懒得人,以下是xml的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/meFragmentImageView"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:contentDescription="ssssss"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
如果转载请说明来源,尊敬原创从我做起!