1+ package com .guiying .module .common .glide ;
2+ 3+ import android .content .Context ;
4+ import android .graphics .Bitmap ;
5+ import android .graphics .drawable .Drawable ;
6+ import android .view .View ;
7+ import android .widget .ImageView ;
8+ 9+ import com .bumptech .glide .Glide ;
10+ import com .bumptech .glide .load .engine .DiskCacheStrategy ;
11+ import com .bumptech .glide .load .resource .drawable .GlideDrawable ;
12+ import com .bumptech .glide .request .animation .GlideAnimation ;
13+ import com .bumptech .glide .request .target .GlideDrawableImageViewTarget ;
14+ import com .bumptech .glide .request .target .SimpleTarget ;
15+ import com .guiying .module .common .utils .Utils ;
16+ 17+ /**
18+ * <p> 图片加载工具类</p>
19+ *
20+ * @name ImageUtils
21+ */
22+ public class ImageUtils {
23+ 24+ /**
25+ * 默认加载
26+ */
27+ public static void loadImageView (String path , ImageView mImageView ) {
28+ Glide .with (mImageView .getContext ()).load (path ).into (mImageView );
29+ }
30+ 31+ public static void loadImageWithError (String path , int errorRes , ImageView mImageView ) {
32+ Glide .with (mImageView .getContext ())
33+ .load (path )
34+ .crossFade ()
35+ .diskCacheStrategy (DiskCacheStrategy .SOURCE )
36+ .error (errorRes )
37+ .into (mImageView );
38+ }
39+ 40+ /**
41+ * 设置加载中以及加载失败图片
42+ */
43+ public static void loadImageWithLoading (String path , ImageView mImageView , int lodingImage , int errorRes ) {
44+ Glide .with (mImageView .getContext ()).load (path ).placeholder (lodingImage ).
45+ error (errorRes ).into (mImageView );
46+ }
47+ 48+ /**
49+ * 设置加载动画
50+ * api也提供了几个常用的动画:比如crossFade()
51+ */
52+ public static void loadImageViewAnim (String path , int anim , ImageView mImageView ) {
53+ Glide .with (mImageView .getContext ()).load (path ).animate (anim ).into (mImageView );
54+ }
55+ 56+ 57+ /**
58+ * 加载为bitmap
59+ *
60+ * @param path 图片地址
61+ * @param listener 回调
62+ */
63+ public static void loadBitMap (String path , final onLoadBitmap listener ) {
64+ Glide .with (Utils .getContext ()).load (path ).asBitmap ().into (new SimpleTarget <Bitmap >() {
65+ 66+ @ Override
67+ public void onResourceReady (Bitmap bitmap , GlideAnimation <? super Bitmap > glideAnimation ) {
68+ listener .onReady (bitmap );
69+ }
70+ 71+ @ Override
72+ public void onLoadFailed (Exception e , Drawable errorDrawable ) {
73+ listener .onFailed ();
74+ }
75+ });
76+ }
77+ 78+ /**
79+ * 显示加载进度
80+ *
81+ * @param path 图片地址
82+ * @param mImageView 图片控件
83+ * @param loadView 加载view
84+ */
85+ public static void loadImageWithProgress (String path , final ImageView mImageView , final View loadView , int errorRes ) {
86+ Glide .with (mImageView .getContext ()).load (path ).error (errorRes ).into (new GlideDrawableImageViewTarget (mImageView ) {
87+ @ Override
88+ public void onResourceReady (GlideDrawable resource , GlideAnimation <? super GlideDrawable > animation ) {
89+ super .onResourceReady (resource , animation );
90+ loadView .setVisibility (View .GONE );
91+ }
92+ 93+ @ Override
94+ public void onLoadFailed (Exception e , Drawable errorDrawable ) {
95+ super .onLoadFailed (e , errorDrawable );
96+ loadView .setVisibility (View .GONE );
97+ }
98+ });
99+ }
100+ 101+ /**
102+ * 清除view上的图片
103+ *
104+ * @param view 视图
105+ */
106+ public static void clearImageView (View view ) {
107+ Glide .clear (view );
108+ }
109+ 110+ /**
111+ * 清理磁盘缓存需要在子线程中执行
112+ */
113+ public static void GuideClearDiskCache (Context mContext ) {
114+ Glide .get (mContext ).clearDiskCache ();
115+ }
116+ 117+ /**
118+ * 清理内存缓存可以在UI主线程中进行
119+ */
120+ public static void GuideClearMemory (Context mContext ) {
121+ Glide .get (mContext ).clearMemory ();
122+ }
123+ 124+ /**
125+ * 加载bitmap回调
126+ */
127+ public interface onLoadBitmap {
128+ void onReady (Bitmap resource );
129+ 130+ void onFailed ();
131+ }
132+ 133+ }
0 commit comments