当前位置:网站首页>Simple encapsulation of glide tool class
Simple encapsulation of glide tool class
2022-08-09 13:15:00 【Xie Dong_】
开题:Actually to request frame images,I have always admired and skills in the use of a good,As early as I had for long beforeUniversal-image-loaderWritten technology sharing blog,With its more mature,And more stable to use,So when doing project usually preferredUniversal-image-loader,But for it if not in the project plan not packaging,It is easy to cause code redundancy,Just recently in writing for the new project,So going to try a few more famous pictures other loading framework,After do a contrast,决定使用Glid.
This blog will start simple introduceGlide的简单使用,I will use through a few simpleGlideThe scenario to introduceGlide,Then summarize some for youGlide常用的方法,At the end of the blog I will post in my project encapsulated tools for your reference.关于Glide的优缺点对比,我就不在此赘述了,网上比比皆是,The purpose of this blog is to tell you a piece of the use of image again request frame encapsulation,More convenient we developed using.
一、Glide使用场景分析
1.使用GlideThe basic method for the image
/**
* Simple get photo
*
* @param path
* @param view
*/
public static void getImage(String path, ImageView view) {
Glide.with(context)
.load(path)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(view);
}
2.The size of the load in the loaded pictures
/**
* According to the specified size loading pictures
*
* @param path
* @param width
* @param height
* @param imageView
*/
public static void getImageBySize(String path, int width, int height, ImageView imageView) {
Glide.with(context)
.load(path)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.override(width, height)
.into(imageView);
}
3.Set loading picturesanimmation动画
/**
* Animate the loading pictures
*
* @param path
* @param imageView
* @param anim
*/
public static void getImageWithAnim(String path, ImageView imageView, int anim) {
Glide.with(context)
.load(path)
.animate(anim)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.into(imageView);
}
4.Even the exciting loadGif,Is simply a few lines of code to accomplish,你会发现原来glideThe image is so elegant
/**
* 加载动态Gif
*
* @param path
* @param imageView
*/
public static void getImageByDynamicGif(String path, ImageView imageView) {
Glide.with(context)
.load(path)
.asGif()
.centerCrop()
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(imageView);
}
5.Other functions request frame imagesGlide毫不逊色,使用GlideSet the listener when loading pictures
/**
* Take the listener loading pictures
*
* @param path
* @param imageView
* @param requstlistener
*/
public static void getImageWithListener(String path, ImageView imageView, RequestListener<String, GlideDrawable> requstlistener) {
Glide.with(context)
.load(path)
.centerCrop()
.listener(requstlistener)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(imageView);
}
Simple combing the use of a few common scenarios for you,More method signature I would put a I finally based on the packagingGlideUtilUtility class for your reference.
二、关于Glide的常用方法
1.thumbnail(float sizeMultiplier). Request a given coefficient thumbnails.If the thumbnail before full size chart after loading,According to shrink
略图,否则就不显示.系数sizeMultiplier必须在(0,1)之间,Can recursive calls the method.
2.sizeMultiplier(float sizeMultiplier). Before loading resources toTargetSize set coefficient.
3.diskCacheStrategy(DiskCacheStrategy strategy).设置缓存策略.DiskCacheStrategy.SOURCE:缓存
原始数据,DiskCacheStrategy.RESULT:Cache transform(如缩放、裁剪等)After the resource data,
DiskCacheStrategy.NONE:什么都不缓存,DiskCacheStrategy.ALL:缓存SOURC和RESULT.
默认采用DiskCacheStrategy.RESULT策略,对于download only操作要使用DiskCacheStrategy.SOURCE.
4.priority(Priority priority). 指定加载的优先级,优先级越高越优先加载,But does not guarantee that all pictures
All sequential loading.枚举Priority.IMMEDIATE,Priority.HIGH,
Priority.NORMAL,Priority.LOW.默认为Priority.NORMAL.
5.dontAnimate(). Remove all the animation.
6.animate(int animationId). In asynchronous loading resources to complete will perform the animation.
7.animate(ViewPropertyAnimation.Animator animator). In asynchronous loading resources to complete will perform the animation.
8.placeholder(int resourceId). Set the resource in the process of loading placeholderDrawable.
9.placeholder(Drawable drawable). Set the resource in the process of loading placeholderDrawable.
10.fallback(int resourceId). 设置modelIs empty to showDrawable.如果没设置fallback,
modelWhen null will showerror的Drawable,如果error的Drawable也没设置,
就显示placeholder的Drawable.
11.fallback(Drawable drawable).设置modelIs empty, displayDrawable.
12.error(int resourceId).设置loadDisplayed when failureDrawable.
13.error(Drawable drawable).设置loadDisplayed when failureDrawable.
14.listener(RequestListener<? super ModelType, TranscodeType> requestListener). Monitor resources to add
Load state of the request,You can use two callback:onResourceReady(R resource, T model,
Target<R> target, boolean isFromMemoryCache, boolean isFirstResource)
和onException(Exception e, T model, Target<R> target, boolean isFirstResource)
But not every request using the new listener,To avoid unnecessary memory application,Can use the singleton unified exception monitoring and processing.
15.skipMemoryCache(boolean skip). Set whether to skip the memory cache,But does not guarantee that must not be cached
(Such as request haven't set in the load resources and skip the memory cache,The resources will be cached in memory).
16.override(int width, int height). 重新设置Target的宽高值(单位为pixel).
17.into(Y target).Set of resources will be loaded into theTarget.
18.into(ImageView view). Set of resources will be loaded into theImageView.取消该ImageViewBefore all of the loading and release resources.
19.into(int width, int height). Background threads load to load resources wide high value(单位为pixel).
20.preload(int width, int height). 预加载resource到缓存中(单位为pixel).
21.asBitmap(). Whether the resource isgif动画,都作为Bitmap对待.如果是gifThe animation will stop in the first frame.
22.asGif().The resources asGifDrawable对待.如果资源不是gifThe animation will fail,会回调.error().
三、基于GlideSimple packaging tool class
/**
* Glide简单封装
* Created by 谢栋 on 2017/10/26.
*/
public class GlideUtil {
private static Context context = AppManage.getInstance().currentActivity();
/**
* Simple get photo
*
* @param path
* @param view
*/
public static void getImage(String path, ImageView view) {
Glide.with(context)
.load(path)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(view);
}
/**
* According to the specified size loading pictures
*
* @param path
* @param width
* @param height
* @param imageView
*/
public static void getImageBySize(String path, int width, int height, ImageView imageView) {
Glide.with(context)
.load(path)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.override(width, height)
.into(imageView);
}
/**
* Specified load waiting with loading failure pictures
*
* @param path
* @param imageView
* @param errorRes
* @param loadingRes
*/
public static void getImageWithErrorLoadingImg(String path, ImageView imageView, int errorRes, int loadingRes) {
Glide.with(context)
.load(path)
.placeholder(loadingRes)
.error(errorRes)
.into(imageView);
}
/**
* Whether to skip loading
*
* @param path
* @param view
* @param isSkipMemoryCache
*/
public static void getImage(String path, ImageView view, boolean isSkipMemoryCache) {
Glide.with(context)
.load(path)
.skipMemoryCache(isSkipMemoryCache)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.into(view);
}
/**
* Animate the loading pictures
*
* @param path
* @param imageView
* @param anim
*/
public static void getImageWithAnim(String path, ImageView imageView, int anim) {
Glide.with(context)
.load(path)
.animate(anim)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.into(imageView);
}
/**
* 加载动态Gif
*
* @param path
* @param imageView
*/
public static void getImageByDynamicGif(String path, ImageView imageView) {
Glide.with(context)
.load(path)
.asGif()
.centerCrop()
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(imageView);
}
/**
* 加载静态Gif
*
* @param path
* @param imageView
*/
public static void getImageByStaticGif(String path, ImageView imageView) {
Glide.with(context)
.load(path)
.asBitmap()
.centerCrop()
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(imageView);
}
/**
* 加载图片centerCrop()
*
* @param path
* @param view
*/
public static void getImageCenterCrop(String path, ImageView view) {
Glide.with(context)
.load(path)
.centerCrop()
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(view);
}
/**
* Take the listener loading pictures
*
* @param path
* @param imageView
* @param requstlistener
*/
public static void getImageWithListener(String path, ImageView imageView, RequestListener<String, GlideDrawable> requstlistener) {
Glide.with(context)
.load(path)
.centerCrop()
.listener(requstlistener)
.placeholder(R.drawable.icon_calendar)
.error(R.drawable.error_circle)
.priority(Priority.NORMAL) //Download the priority
.into(imageView);
}
/**
* 清理内存缓存
*/
public static void clearMemaryCache() {
Glide.get(context)
.clearMemory();
}
/**
* 清理磁盘缓存
*/
public static void clearDiskCache() {
Glide.get(context)
.clearDiskCache();
}
}
边栏推荐
- The grep command Shell regular expressions, the three musketeers
- Gumbel_Softmax 概要
- goalng-sync/atomic原子操作
- WeChat Mini Program Payment and Refund Overall Process
- Flutter入门进阶之旅(十)Dialog&Toast
- 国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
- 京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
- Flutter入门进阶之旅(二)Hello Flutter
- 微信支付开发流程
- Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
猜你喜欢
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
Common gadgets of Shell (sort, uniq, tr, cut)
【微服务~远程调用】整合RestTemplate、WebClient、Feign
基于STM32+铂电阻设计的测温仪
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
Flutter入门进阶之旅(六)Layout Widget
Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers
redis库没法引入
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
随机推荐
Gumbel_Softmax 概要
redis库没法引入
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
26、管道参数替换命令xargs
关于Retrofit网络请求URL中含有可变参数的处理
Flutter入门进阶之旅(一)-初识Flutter
WPF implements a MessageBox message prompt box with a mask
世界第4疯狂的科学家,在103岁生日那天去世了
基于CAP组件实现补偿事务与幂等性保障
Django cannot link mysql database
FFmpeg compiles and installs on win10 (configure libx264)
IDEA close/open reference prompt Usages
ABAP 报表中如何以二进制方式上传本地文件试读版
Flutter入门进阶之旅(三)Text Widgets
We really need DApp?Really can't meet our fantasy App?
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
h264协议
Golang学习之路(五):Golang的函数
World's 4th mad scientist dies on his 103rd birthday