Android图片加载策略-ImageLoader

Android

封装图片加载,任意切换图片库

传送门

统一管理图片加载库,随意切换图片加载框架

  • Fresco
  • Picasso
  • Glide
  • Universal-ImageLoader
  • Volley

封装

  • 多个图片加载库切换
  • 图片加载进度回调
  • 自定义配置(如占位图片、错误占位图片、缓存目录、大小等)
  • 清除缓存

使用

  • 独立依赖库

    1
    2
    3
    4
    5
    implementation 'com.excellence:imageloader:_latestVersion'
    // 下面图库三选一,减小安装包大小
    implementation 'com.excellence:imageloader-fresco:_latestVersion'
    implementation 'com.excellence:imageloader-picasso:_latestVersion'
    implementation 'com.excellence:imageloader-glide:_latestVersion'
  • 权限

    1
    2
    3
    4
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • API

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    // 初始化,不同的加载器,有部分独立的方法
    // 可以自定义实现ImageLoader接口,创建新的图库加载器
    ImageLoaderOptions options = new ImageLoaderOptions.Builder().isLogEnable(true).isCache(false).build();
    mImageLoader = new FrescoImageLoader(this, options);
    mImageLoader = new PicassoImageLoader(this, options);
    mImageLoader = new GlideImageLoader(this, options);

    // 统一的接口
    public interface ImageLoader
    {
    /**
    * 加载资源图片
    *
    * @param view
    * @param resId
    */
    void loadImage(@NonNull ImageView view, @DrawableRes int resId);

    void loadImage(@NonNull ImageView view, @DrawableRes int resId, IListener listener);

    /**
    * 加载资源图片,占位图片,错误图片
    *
    * @param view
    * @param resId
    * @param placeholderResId
    * @param errorResId
    */
    void loadImage(@NonNull ImageView view, @DrawableRes int resId, @DrawableRes int placeholderResId, @DrawableRes int errorResId);

    void loadImage(@NonNull ImageView view, @DrawableRes int resId, @DrawableRes int placeholderResId, @DrawableRes int errorResId, IListener listener);

    /**
    * 加载本地图片
    *
    * @param view
    * @param file
    */
    void loadImage(@NonNull ImageView view, @NonNull File file);

    void loadImage(@NonNull ImageView view, @NonNull File file, IListener listener);

    /**
    * 加载本地图片,占位图片,错误图片
    *
    * @param view
    * @param file
    * @param placeholderResId
    * @param errorResId
    */
    void loadImage(@NonNull ImageView view, @NonNull File file, @DrawableRes int placeholderResId, @DrawableRes int errorResId);

    void loadImage(@NonNull ImageView view, @NonNull File file, @DrawableRes int placeholderResId, @DrawableRes int errorResId, IListener listener);

    /**
    * 加载网络图片
    *
    * @param view
    * @param url
    */
    void loadImage(@NonNull ImageView view, @NonNull String url);

    void loadImage(@NonNull ImageView view, @NonNull String url, IListener listener);

    /**
    * 加载网络图片,占位图片,错误图片
    *
    * @param view
    * @param url
    * @param placeholderResId
    * @param errorResId
    */
    void loadImage(@NonNull ImageView view, @NonNull String url, @DrawableRes int placeholderResId, @DrawableRes int errorResId);

    void loadImage(@NonNull ImageView view, @NonNull String url, @DrawableRes int placeholderResId, @DrawableRes int errorResId, IListener listener);

    void clearCache();
    }

Fresco

1
2
3
4
5
6
7
8
9
10
11
12
13
14
implementation 'com.facebook.fresco:fresco:1.9.0'

// 在 API < 14 上的机器支持 WebP 时,需要添加
compile 'com.facebook.fresco:animated-base-support:0.12.0'

// 支持 GIF 动图,需要添加
compile 'com.facebook.fresco:animated-gif:0.12.0'

// 支持 WebP (静态图+动图),需要添加
compile 'com.facebook.fresco:animated-webp:0.12.0'
compile 'com.facebook.fresco:webpsupport:0.12.0'

// 仅支持 WebP 静态图,需要添加
compile 'com.facebook.fresco:webpsupport:0.12.0'
1
2
3
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);

Picasso

1
implementation 'com.squareup.picasso:picasso:2.71828'
1
2
3
4
5
Picasso.get()
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)

缓存路径:data/data/your package name/cache/picasso-cache/(默认路径)

Glide

1
2
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
1
2
3
4
5
6
7
Glide.with(getContext())
.load(url)
.skipMemoryCache(true)
.placeholder(drawable)
.centerCrop()
.animate(animator)
.into(img);

Universal-ImageLoader

Volley

版本更新

版本 描述
1.0.0 封装Fresco、Picasso、Glide图库,简单加载图片 2018-10-11

感谢

谢谢老板,请尽情用红包来蹂躏我吧!!!
0%