Glide 封装--圆形 圆角 高斯模糊图片处理 加载状态以及进度监听

介绍

ImageLoader: 基于Glide + glide:okhttp + Glide图片变换库做的一个简单的封装.极大的简化使用Glide成本:

   GlideImageLoader.getInstance().displayWithDrable(context,url)?.intoTargetView(imageview)

最基础的使用,这样便可实现普通图片以及GIF图的加载显示。

功能

  • 支持图片圆角话圆型化处理。
  • 支持高色模糊处理。
  • 支持图片加载监听以及进度监听。
  • 支持动态修改placeholder以及error展位图。
  • 支持动态修改加载图片的宽高。
  • 支持动态修改加载图片的缓存策略。

Demo

device-2020-08-03-131928 (1).gif
device-2020-08-03-131928.gif

用法

导入:

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
       ...
       maven { url 'https://jitpack.io' }
    }
}

Add the dependency

dependencies {
   implementation 'com.github.colinlibrary:ImageLoader:1.0.11'
}

在Application的onCreate中初始化

@Override
public void onCreate() {
    super.onCreate();
    //当然你也可以不执行此操作,那么所有配置将启用默认配置执行
    GlideImageLoader.apply(DiskCacheMenu.RESOURCE)//设置缓存策略
                //设置展位图资源(支持Drawable和Int类型的资源)
                .apply(R.mipmap.placeholder,R.mipmap.error)
                //设置淡入动画时间(默认500毫秒)
                .apply(600)
                //设置缓存大小默认5M
                .apply(1025 * 1024 * 10L);
                //设置缓存路径默认/data/user/0/***/cache/GlideDisk
                //.apply("自定义缓存路径");
}

如果选用外部存储 请添加相应权限
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
以及动态申请相应权限

使用:

1.普通方式使用

以下方式均 支持bitmap和drawable 想要加载GIF类型的资源选用drawable方式即可

   //context 支持 Activity,Fragment,View,Service类型
   GlideImageLoader.getInstance().displayWithDrawable(context,url)?.intoTargetView(targetView)
   GlideImageLoader.getInstance().displayWithBitmap(context,url)?.intoTargetView(targetView)

2.加载圆形图片

   //context 支持 Activity,Fragment,View,Service类型
   GlideImageLoader.getInstance().displayCircleWithDrawable(context,url)?.intoTargetView(targetView)
   GlideImageLoader.getInstance().displayCircleWithBitmap(context,url)?.intoTargetView(targetView)

3.加载圆角图片

   //context 支持 Activity,Fragment,View,Service类型
   GlideImageLoader.getInstance().displayRoundWithDrawable(context,url,cornerRadius)?.intoTargetView(targetView)
   GlideImageLoader.getInstance().displayRoundWithDrawable(context,url,cornerRadius,cornerTypeMenu)?.intoTargetView(targetView)
   GlideImageLoader.getInstance().displayRoundWithBitmap(context,url,cornerRadius)?.intoTargetView(targetView)
   GlideImageLoader.getInstance().displayRoundWithBitmap(context,url,cornerRadius,cornerTypeMenu)?.intoTargetView(targetView)
   
   cornerTypeMenu一共有 
   ALL,
   TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
   TOP, BOTTOM, LEFT, RIGHT,
   OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
   DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
   种类型可以选择

4.高斯模糊加圆角的方式加载

   //context 支持 Activity,Fragment,View,Service类型
   GlideImageLoader.getInstance().displayWithBlur(context,url,cornerRadius)?.intoTargetView(targetView)
   GlideImageLoader.getInstance().displayWithBlurRound(context,url,blurRadius,cornerRadius)?.intoTargetView(targetView)
   GlideImageLoader.getInstance().displayWithBlurRound(context,url,blurRadius,cornerRadius,cornerTypeMenu)?.intoTargetView(targetView)
   
   
   同样的cornerTypeMenu一共有 
   ALL,
   TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
   TOP, BOTTOM, LEFT, RIGHT,
   OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
   DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
   种类型可以选择

5.资源加载监听

   //context 支持 Activity,Fragment,View,Service类型
   //bitmap方式加载
   GlideImageLoader.getInstance().displayWithBitmap(context,url,object : ImageLoaderListener<Bitmap> {
                override fun onRequestSuccess(resource: Bitmap?) {//成功回调
                    
                }

                override fun onRequestFailed() {//失败回调
                    
                }
            })?.intoTargetView(targetView)
   
   //drawable方式加载
   GlideImageLoader.getInstance().displayWithDrawable(context,url,object : ImageLoaderListener<Drawable> {
                override fun onRequestSuccess(resource: Drawable?) {//成功回调
                    
                }

                override fun onRequestFailed() {//失败回调
                    
                }
            })?.intoTargetView(targetView)
            
   //drawable方式加载     
   GlideImageLoader.getInstance().displayWithDrawable(context,url,object : OnProgressListener<Drawable>{
                override fun onProgress(isComplete: Boolean,percentage: Int,bytesRead: Long,totalBytes: Long) {//进度回调
                    
                }

                override fun onComplete(resource: Drawable?) {//成功回调
                    
                }
                override fun onFailed() {//失败回调
                    
                }
            } )?.intoTargetView(targetView)
            
   //bitmap方式加载     
   GlideImageLoader.getInstance().displayWithBitmap(context,url,object : OnProgressListener<Bitmap>{
                override fun onProgress(isComplete: Boolean,percentage: Int,bytesRead: Long,totalBytes: Long) {//进度回调
                    
                }

                override fun onComplete(resource: Bitmap?) {//成功回调
                    
                }
                override fun onFailed() {//失败回调
                    
                }
            } )?.intoTargetView(targetView)
* 以上方式均支持重置加载资源宽高(针对本次加载生效) 例如:
   GlideImageLoader.getInstance().displayWithDrawable(context,url)?.resetTargetSize(400,400)?.intoTargetView(targetView)
   
   这样就以400*400为目标view去加载资源
* 自定义缓存策略(针对本次加载生效)例如:
   GlideImageLoader.getInstance().displayWithDrawable(context,url)?.resetDiskCacheStrategy(DiskCacheStrategy.DATA)?.intoTargetView(targetView)
*重置占位图(针对本次加载生效)placeholder,error 均支持 drawable和int 两种类型 例如:
   GlideImageLoader.getInstance().displayWithDrawable(context,url)?.resetPlaceHolder(placeholder,error)?.intoTargetView(targetView)
* 重置ScaleType (针对本次加载生效 请谨慎使用)
   GlideImageLoader.getInstance().displayWithDrawable(context,url)?.resetScaleType(ScaleTypeMenu.CenterCrop)?.intoTargetView(targetView)
   
   ScaleTypeMenu 共支持 
   Default,CenterCrop,CenterInside,FitCenter,CircleCrop
   种类型可选择
* 重置CrossFade 过渡时长 (针对本次加载生效)
   //以毫秒为时间单位
   GlideImageLoader.getInstance().displayWithDrawable(context,url)?.resetCrossFadeTime(100)?.intoTargetView(targetView)

6.获取URL资源

  
   //bitmap方式
   GlideImageLoader.getInstance().getUrlWithBitmap(context,url,object :OnProgressListener<Bitmap>{
                override fun onProgress( isComplete: Boolean, percentage: Int,  bytesRead: Long, totalBytes: Long    ) {//进度回调
                    
                }

                override fun onComplete(resource: Bitmap?) {//成功回调
               
                }
                override fun onFailed() {//失败回调
                    
                }
            })
            
    //drawable方式
    GlideImageLoader.getInstance().getUrlWithDrawable(context,url,object :OnProgressListener<Drawable>{
                override fun onProgress( isComplete: Boolean, percentage: Int,  bytesRead: Long, totalBytes: Long    ) {//进度回调
                    
                }

                override fun onComplete(resource: Drawable?) {//成功回调
               
                }
                override fun onFailed() {//失败回调
                    
                }
            })
            
   //bitmap方式
   GlideImageLoader.getInstance().getUrlWithBitmap(context,url,object : ImageLoaderListener<Bitmap> {
                override fun onRequestSuccess(resource: Bitmap?) {//成功回调
                    
                }

                override fun onRequestFailed() {//失败回调
                    
                }
            })
            
    //drawable方式
    GlideImageLoader.getInstance().getUrlWithDrawable(context,url,object : ImageLoaderListener<Drawable> {
                override fun onRequestSuccess(resource: Drawable?) {//成功回调
                    
                }

                override fun onRequestFailed() {//失败回调
                    
                }
            })

7.缓存清理

   /**
     * 内存缓存清理(主线程)
     */
    override fun clearMemoryCache(context: Context?) {
        context?.let { GlideApp.get(it).clearMemory() }
    }

    /**
     * 磁盘缓存清理(子线程)
     */
    override fun clearFileCache(context: Context?) {
        Thread(Runnable { context?.let { GlideApp.get(it).clearDiskCache() } }).start()
    }

缺点

  • 还有很多不足之处以后慢慢改进

项目地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354