圖片加載框架
整理常用的圖片加載框架
- 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" />
代碼調(diào)用
//需要指定寬高枫浙,要不圖片加載不出來
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'
代碼調(diào)用
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'
代碼調(diào)用
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);
}
代碼調(diào)用
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個(gè)框架的內(nèi)存對(duì)比圖: