Objective:
Before learning the simple use of glide (http://www.cnblogs.com/whoislcj/p/5558168.html), today to learn glide a little more complex use.
Glidemodule using:
Glidemodule is an abstract method that globally alters the behavior of Glide in a way that is configured by the global Glidemodule Glide, with the GlideBuilder
set option, registered with GlideModelLoader等。
1.) Customizing a Glidemodule
Public class Implements Glidemodule { publicvoid applyoptions (context context, Glidebuilder Builder) { // Apply options to the builder here. } publicvoid registercomponents (context context, Glide Glide) { // register modelloaders here. }}
2.) Androidmanifest.xml Registration
<Manifest...> <!--. .. permissions - <Application...> <Meta-dataAndroid:name= "Com.mypackage.MyGlideModule"Android:value= "Glidemodule" /> <!--... activities and other components - </Application></Manifest>
3.) Add obfuscation
-keepnames class com.mypackage.myglidemodule# or more Generally:#-keep public class * Implements Com.bumptech.glide.module.GlideModule
4.) Multiple Glidemodule conflict issues
Glidemodule cannot specify the order of calls, so you should avoid conflicting option settings between different glidemodule, consider putting all the settings in a glidemodule, or excluding a module from a manifest file. The code is as follows:
<android:nametools:node/>
GlideBuilder
Setting options: 1.) Set the glide memory cache size
int maxmemory = (int) runtime.getruntime (). MaxMemory (); // gets the total amount of memory allocated to the app by the system int memorycachesize = MAXMEMORY/8; // // set memory cache size Builder.setmemorycache (new Lruresourcecache (memorycachesize));
Get the default memory usage calculation function
New memorysizecalculator (context); int defaultmemorycachesize = calculator.getmemorycachesize (); int defaultbitmappoolsize = calculator.getbitmappoolsize ();
2.) Set the glide disk cache size
File Cachedir = Context.getexternalcachedir (); // Specifies the cached address of the data int diskcachesize = 1024 * 1024 * 30; // // Set disk cache size Builder.setdiskcache (new disklrucachefactory ( Cachedir.getpath (), "glide", diskcachesize));
There are two ways in which you can
// Store in data/data/xxxx/cache/ Builder.setdiskcache (new internalcachediskcachefactory (context, "glide", diskcachesize )); // Store the external file browser Builder.setdiskcache (new externalcachediskcachefactory (context, "glide", Diskcachesize));
3.) Set the image decoding format
// set Picture decoding format builder.setdecodeformat (decodeformat.prefer_argb_8888);
The default format rgb_565 uses half the memory of argb_8888, but the picture quality is not that high, and the transparency is not supported
4.) Set the cache memory size
// set Bitmappool cache memory Size Builder.setbitmappool (new Lrubitmappool (memorycachesize));
5.) set a executorservice to retrieve resource that are not in the cache
In order for the thumbnail request to work correctly, the implementation class must prioritize the request based on priority precedence.
Builder.setdiskcacheservice (Executorservice service); Builder.setresizeservice (Executorservice service);
To customize a data source using Modelloader:
For example, we use seven cow cloud storage, to request different sizes of images according to different requirements, then we can use a custom data source
1.) Define processing URL interface
Public Interface Idatamodel { String builddatamodelurl (intint height);}
2.) Implement processing URL interface
Jpgdatamodel
Public classJpgdatamodelImplementsIdatamodel {PrivateString Datamodelurl; PublicJpgdatamodel (String datamodelurl) { This. Datamodelurl =Datamodelurl; } @Override PublicString Builddatamodelurl (intWidthintheight) { //http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg?imageView2/1/w/200/h/200/format/jpg returnString.Format ("%s?imageview2/1/w/%d/h/%d/format/jpg", Datamodelurl, width, height); }}
Webpdatamodel
Public classWebpdatamodelImplementsIdatamodel {PrivateString Datamodelurl; PublicWebpdatamodel (String datamodelurl) { This. Datamodelurl =Datamodelurl; } @Override PublicString Builddatamodelurl (intWidthintheight) { //HTTP://78RE52.COM1.Z0.GLB.CLOUDDN.COM/RESOURCE/GOGOPHER.JPG?IMAGEVIEW2/1/W/200/H/200/FORMAT/WEBP returnString.Format ("%S?IMAGEVIEW2/1/W/%D/H/%D/FORMAT/WEBP", Datamodelurl, width, height); }}
3.) Implement Modelloader
Public classMydataloaderextendsBaseglideurlloader<idatamodel> { PublicMydataloader (Context context) {Super(context); } PublicMydataloader (Modelloader<glideurl, inputstream>UrlLoader) { Super(UrlLoader,NULL); } @OverrideprotectedString GETURL (Idatamodel model,intWidthintheight) { returnmodel.builddatamodelurl (width, height); } /** */ Public Static classFactoryImplementsModelloaderfactory<idatamodel, inputstream>{@Override PublicModelloader<idatamodel, inputstream>Build (context context, genericloaderfactory factories) {return NewMydataloader (Factories.buildmodelloader (Glideurl.class, InputStream.class)); } @Override Public voidteardown () {} }}
4.) Use different strategies to load images according to different requirements
// loading JPG images Glide.with (this). The using (new mydataloader(this)). Load (new Jpgdatamodel ( IMAGEURL)). into (ImageView); // loading WEBP pictures Glide.with (this). The using (new mydataloader(this)). Load (new Webpdatamodel (IMAGEURL)). into (ImageView);
5.) How to skip. using ()
Public class Implements glidemodule { ... @Override publicvoid registercomponents (context context, Glide Glide) { Glide.register (Idatamodel. class, InputStream. class , New myurlloader.factory ());} }
The above implementation skips the using ()
// loading JPG images Glide.with (this). Load (new Jpgdatamodel (IMAGEURL)). into (ImageView); // loading WEBP pictures Glide.with (this). Load (new Webpdatamodel (IMAGEURL)). into (ImageView);
Use signature () to implement a custom CacheKey:
Glide to the URL, viewwidth, viewheight, screen resolution and so on as a joint key, the official API does not provide a function to delete the image cache, the official provides the signature () method, Add an additional data to the cache key, multimedia storage data, the use of Mediastoresignature class as an identifier, the file modification time, mimetype and other information as part of the CacheKey, By changing the key to achieve the image failure is equivalent to soft deletion.
1.) Use Stringsignature
Glide.with (this). Load (Yourfiledatamodel). Signature (new stringsignature ("1.0.0")). into ( ImageView);
2.) Use
MediaStoreSignature
Glide.with (this). Load (Mediastoreuri). Signature (new mediastoresignature (MimeType, DateModified, Orientation)). into (view);
3.) Using a custom signature
Public classIntegerversionsignatureImplementsKey {Private intCurrentVersion; PublicIntegerversionsignature (intCurrentVersion) { This. CurrentVersion =CurrentVersion; } @Override Public Booleanequals (Object o) {if(Oinstanceofintegerversionsignature) {Integerversionsignature Other=(integerversionsignature) o; returnCurrentVersion =other.currentversion; } return false; } @Override Public inthashcode () {returnCurrentVersion; } @Override Public voidUpdatediskcachekey (messagedigest md) {messagedigest.update (Bytebuffer.allocate (integer.size). PutInt ( Signature). Array ()); }}
Summary:
Learn some of Glide's custom extensions today, and then learn about the combination of glide and okhttp, which is an easy way to analyze internal use.
Android Explorer image cache <glide advanced > (iv)