当前位置:网站首页>Glidemodule appglidemodule and generated API details
Glidemodule appglidemodule and generated API details
2022-07-24 22:11:00 【ScottePerk】
GlideModule yes 3.x There is , and AppGlideModule and Generated API yes 4.x There are only functions .
GlideModule Has been abandoned , The alternative is AppGlideModule This class . And it has new functions Generated API.Generated API The function is very easy to use , Examples are given below , The main function is to replace the original RequestOption Writing ,Glide I think this writing method can be further improved , So there was Generated API.
AppGlideModule
because GlideModule Has been abandoned , And it is not recommended for performance reasons , It can be used AppGlideModule replace ,GlideModule I will make a brief introduction at the end .
What is? AppGlideModule? Simply speaking ,AppGlideModule Namely Glide Global configuration file for . Or say , Originally, we may need to write these configurations Application The inside of the class , It must be more convenient to write one alone .
Use AppGlideModule need 4.x
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
Inherit AppGlideModule And add a @GlideModule Just annotate , You can write nothing , Because there is a default implementation .
@GlideModule
public class GlideConfig extends AppGlideModule {
}
After adding comments , Will be in build There are several more documents in it . What we need is this GlideApp class . Later on .
adopt AppGlideModule, We can configure Glide, For example, the following code can configure the cache policy and cache size . Just copy applyOptions Method .MemorySizeCalculator Class can automatically obtain the appropriate cache size according to the screen size , Very easy to use . This is an example given by the official .
@GlideModule
public class GlideConfig extends AppGlideModule {
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
// This class can automatically get the appropriate cache size according to the screen size
MemorySizeCalculator calculator=new MemorySizeCalculator.Builder(context)
// I don't know what the number of screens means , There are two screens ( Use it first )? The default value is 2
.setMemoryCacheScreens(2)
.build();
// Use Lru Cache policy
builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
}
}
Integrate Okhttp
Glide It can integrate the third-party network request Library , The most powerful is undoubtedly OkHttp, You can use interceptors to do many things . Third party modules need to inherit LibraryGlideModule, Realization registerComponents, The following writing is an example given by the official . In this way, it has been successfully integrated OkHttp 了 . The following package needs to be introduced . If there is already OkHttp It needs to be removed , Avoid repetition .
implementation "com.github.bumptech.glide:okhttp3-integration:4.11.0"
@GlideModule
public class OkHttpGlideModule extends LibraryGlideModule {
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
glide.getRegistry().replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
}
Generated API
Next, we will introduce one of the most important functions Generated API. The code inside is very common Glide The usual way of writing , but Glide Think RequestOptions This way of writing is not particularly elegant . So we improved the writing method . This new way of writing is through Generated API Realized .
ImageView imageView = findViewById(R.id.iv);
RequestOptions options=new RequestOptions().centerCrop();
Glide.with(this)
.load("")
.apply(options)
.into(iv);
For example, the following code , Let's write a class , And use @GlideExtension Comment mark . We will write the configuration of setting the avatar into this class . This customized method requires @GlideOption annotation , And the first parameter must be BaseRequestOptions, The following parameters can be defined by yourself .
@GlideExtension
final class GlideExtensions {
private GlideExtensions() {
}
/** * Avatar configuration * @param options * @param size Picture memory size * @return */
@GlideOption
public static BaseRequestOptions<?> applyAvatar(BaseRequestOptions<?> options, int size) {
return options.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background)
.override(144);
}
}
Next, the method just defined can be directly chained in the code , Just want to Glide Switch to GlideApp, You can see , We defined it ourselves applyAvatar It can be called directly , Isn't it amazing ?
GlideApp This way is Glide4.x Recommended . I think it's very elegant , The design is very clever , It's very worth learning . The design is very artistic .
GlideApp.with(this)
.load("")
.applyAvatar(144)
.into(imageView);
That's about it . adopt Generated API You can do some strange things .
GlideModule Introduce ( It is not recommended to use )
GlideModule yes 3.x Under the , Has been abandoned , It is still necessary to briefly introduce .
First define ,GlideModule What is it , The first sentence of the source code document tells us to use to do “ Lazy configuration ” Of . But I didn't find out where I was lazy ? Official documents do not say , I thought about it for a long time. , I found that I may have misunderstood , there “ lazy ” Not at all “ Delay ” It means , It is “ lazy ” It means . In short, unified configuration , In this way, you don't need to configure every one when using . So we can “ lazy ” 了 . Is not so much “ Lazy configuration ”, I think it uses “ Preconfigured “ Maybe better .
Then the official gave an example .Flickr The meaning of album sharing , That is to say, the example here is to configure photos . I don't know why these large companies don't write all the document examples , The following code cannot be run , Because several classes in it are not given , He has assumed that you can implement these classes yourself ( I know why I still read the document ?), and glide.register The only way is 3.x The versions of are ,4.x There is no , In fact, this class has been abandoned , If you must , It has to be used. 3.x Version of . Maybe there is something wrong with the early design , This kind of behavior of directly deleting the lower version from the higher version is not appropriate .
The following code is actually from glide The official one is Flickr Of sample, But I can't run in this example , The compilation has passed , Big company demo It's all like this , Unusually large . But you can see that MyModelLoader The implementation of the . But there is too much code .
public class FlickrGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDecodeFormat(DecodeFormat.ALWAYS_ARGB_8888);
}
@Override
public void registerComponents(Context context, Glide glide) {
glide.register(Model.class, Data.class, new MyModelLoader());
}
}
GlideModule Itself is to interface , There is also very little code ,GlideModule stay Glide4.0 It has been abandoned ,AppGlideModule replace . The reason why estimates are abandoned is 3.x Of GlideModule The bottom layer provides reflection implementation , and AppGlideModule Is to provide apt Realized . Better performance .
边栏推荐
- Metauniverse: technological evolution, industrial ecology and big country game
- PCL点云处理之pcd文件转txt文件(单个或多个批量转换)(六十三)
- 巧妙使用sort(List&lt;T&gt;,Comparator&lt;? super T&gt;)比较器
- C# 回看委托与事件
- 【ICML2022】气候变化与机器学习:机遇、挑战与考虑,121页ppt
- How to drain the applet correctly? Three positions of whole network drainage!
- Visual studio input! No prompt
- Circom 2.0: A Scalable Circuit Compiler
- 一键编译安装redis6.2.4
- VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题
猜你喜欢
随机推荐
Redefine analysis - release of eventbridge real-time event analysis platform
【考研词汇训练营】Day 12 —— native,separate,figure,contribute,species,assumption,suppose
Apipost签约中国电信!携手加速企业数字化变革
【二分好题】
Morris遍历
PCL点云处理之平面规则化(五十五)
"Paper reproduction" bidaf code implementation process (4) model training + verification
Plane regularization of PCL point cloud processing (55)
深入理解事务
How to adjust the default output of vscode to the debugging console to the terminal and the problem of garbled code in both
Use of templates
Tencent +360+ Sogou school recruitment test + summary of knowledge points
How to output position synchronization of motion control
运动控制卡应用开发教程之调用激光振镜控制
MySQL forced indexing
Helm —— 强大的 Kubernetes 应用的包管理工具
My love lesson 2 - remove webpage pop-up
Build Tencent cloud website server at low cost (build your own website server)
AC automata
VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题








