图片加载框架
整理常用的图片加载框架
- Glide
- Fresco
- Volley
- Picasso
- Universal Image Loader
1. Glide接入使用
gradle配置
dependencies {
//glide 图片加载
compile 'com.github.bumptech.glide:glide:3.7.0'
}
ImageView im=(ImageView) findViewById(R.id.iv_image1);
Glide.with(this).load("http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg").into(im);
2. Fresco接入使用
gradle配置
//fresco图片加载
compile 'com.facebook.fresco:fresco:1.3.0'
自定义 Application
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//
Fresco.initialize(this);
}
布局文件xml添加
添加:xmlns:fresco="http://schemas.android.com/apk/res-auto"
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.doublechina.autoimageload.ImageActivity">
添加:com.facebook.drawee.view.SimpleDraweeView
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iv_fresco"
android:layout_width="match_parent"
android:layout_height="400dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
代码调用
//需要指定宽高,要不图片加载不出来
Uri uri = Uri.parse("http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.iv_fresco);
draweeView.setImageURI(uri);
3. Volley接入使用
gradle配置
//volley 官方clone工程,volley官方不在提供支持
compile 'com.mcxiaoke.volley:library:1.0.19'
代码调用
final ImageView im=(ImageView) findViewById(R.id.iv_volley);
ImageRequest imageRequest = new ImageRequest(
"http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg",
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
im.setImageBitmap(response);
}
}, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
im.setImageResource(R.mipmap.ic_launcher);
}
});
//volley 官方clone工程,volley官方不在提供支持
compile 'com.mcxiaoke.volley:library:1.0.19'
4. picasso接入使用
gradle配置
//picasso图片加载
compile 'com.squareup.picasso:picasso:2.5.2'
代码调用
ImageView im=(ImageView) findViewById(R.id.iv_picasso_image);
Picasso.with(getApplicationContext()).
load("http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg").into(im);
5. Universal Image Loader接入使用
gradle配置
//Android-Universal-Image-Loader
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
自定义 Application
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//ImageLoader
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.build();
ImageLoader.getInstance().init(config);
}
代码调用
ImageView im=(ImageView) findViewById(R.id.iv_image_load);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg", im);
imageLoader.loadImage("http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg", new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
Toast.makeText(ImageLoadActivity.this, ""+imageUri, Toast.LENGTH_SHORT).show();
// Do whatever you want with Bitmap
}
});
下图是5个框架的内存对比图: