当前位置:网站首页>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();
}
}
边栏推荐
- [Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
- Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
- 用 API Factory 产品生成 API 文档
- C# Get system installed .NET version
- 保存Simulink仿真模型为图片或者PDF的方法
- 太卷了... 腾讯一面被问到内存满了,会发生什么?
- Apexsqlrecover无法连接数据库
- ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
- 腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
- Blocking, non-blocking, multiplexing, synchronous, asynchronous, BIO, NIO, AIO all in one pot
猜你喜欢
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
十分钟教会你如何使用VitePress搭建及部署个人博客站点
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
内网穿透工具ngrok使用教程
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
ABP 6.0.0-rc.1的新特性
MySQL principle and optimization of Group By optimization techniques
【小程序】低代码+小游戏=小游戏可视化开发
HAproxy:负载均衡
随机推荐
Say goodbye to the AI era of hand looms
2022牛客多校(六)M. Z-Game on grid
听声辨物,这是AI视觉该干的???|ECCV 2022
自定义VIEW实现应用内消息提醒上下轮播
shell脚本------函数的格式,传参,变量,递归,数组
We really need DApp?Really can't meet our fantasy App?
使用注解将EventBus封装抽取到基类
香港服务器如何进行加密?
Django cannot link mysql database
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
Glory to the Blue Yonder, speeds up the strategic growth
API调用,API传参,面向对接开发,你真的会写接口文档吗?
h264协议
世界第4疯狂的科学家,在103岁生日那天去世了
【小程序】低代码+小游戏=小游戏可视化开发
李开复花上千万投的缝纫机器人,团队出自大疆
Apexsqlrecover cannot connect to database
十分钟教会你如何使用VitePress搭建及部署个人博客站点
Shell之常用小工具(sort、uniq、tr、cut)
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...