Detailed explanation of the powerful picture loading framework glide configuration (by the way, some features of Collapsingtoolbarlayout)

Source: Internet
Author: User

Reprint please indicate the source: Wang 亟亟 's way of Daniel

Toss a day, the net in the unit finally good really egg ache, and then today speak Glide (originally was prepared yesterday, alas)

The introduction of the theory directly from the internet point of contact, from the beginning of the code word no meaning, no more nonsense, start!

A theoretical thing can be seen http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html

This is a comparison of glide and Picasso, and the analysis is pretty good.

Let's stick to today's example.

All of the images are asynchronously downloaded, although the picture itself is not large, but the load is still very smooth.

The front of the portal has been the general description of the text is said, then I do?

To remove a configuration, the general use of glide only requires
Glide.with(this).load(Config.IMAGE_URL).into(imageView);, but there may be some custom customizations in your actual scenario, so you need to do a job that you want to do in the application class in Imageloader.

Starting in Glide 3.5, you can use the Glidemodule interface to lazily configure Glide and registers components like ModelL Oaders automatically when the first Glide request is made.

Starting with the Glide3.5 version, you can use the Glidemodule interface to lazily initialize and declare components when the first load request appears.

So how do you use it?

Implement Glidemodule, like this.

 Public  class glidemoduleconfig implements glidemodule {    //Create settings here, the image quality mentioned in the previous article can be set here    //You can also set cache pool parameters for what    @Override     Public void applyoptions(context context, Glidebuilder Builder)    {Builder.setdecodeformat (decodeformat.prefer_argb_8888); }//Register here for Modelloaders    //You can clear the cache here.    @Override     Public void registercomponents(context context, Glide Glide)    {Glide.cleardiskcache (); }}

and add our module to our Androidmanifest.xml file.
Like this

 <applicationandroid:allowbackup="true"android:icon="@mipmap/ic_ Launcher "android:label=" @string/app_name "android:supportsrtl=" true "  android:theme="@style/apptheme">                                                <meta-dataandroid:name="Sample.wjj.glidedemo.GlideModuleConfig"android: Value="Glidemodule" />                                <activity android:name=". Mainactivity ">            <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </Application>

Glide is allowed to have multiple settings model so there is bound to be a conflict (if there are multiple LIB projects), you can use

<meta-data android:name=”sample.wjj.glidedemo.GlideModuleConfig” tools:node=”remove” />来避免这一类的问题
Disk Cache

You can use the Glidebuilder's Setdiskcache () method to set the location and/or maximum size of the disk cache. You can also disable the cache entirely using Diskcacheadapter or replace it with your own implementation of the DiskCache Interface. Disk caches is built on background threads to avoid strict mode violations using the Diskcache.factory interface.

You can use the Glidebuilder Setdiskcache () method to set the storage location and the size of some storage space. You can also choose to disable or write your own implementation. The disk cache is implemented in a background thread, and it uses the Diskcache.factory interface

Size

By default Glide uses the Internalcachediskcachefactory class to build disk caches. The internal cache factory places the disk cache in your application ' s internal cache directory and sets a maximum size of 250MB. Using the cache directory rather than the external SD card means no other applications would be able to access the Images you download. See Android ' s Storage Options doc for more details.

This string is so long that I have to name the key. The default is stored inside the application, not the external directory such as the SD card, so that other programs will not be able to access the cached content, the default maximum size is 250M.

Default in this:
int DEFAULT_DISK_CACHE_SIZE = 250 * 1024 * 1024;
String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";

So how do we set our own "Factory value"?

   builder.setDiskCache( new InternalCacheDiskCacheFactory(context,100*1024*1024));

Location

So what if we don't want to be in memory and want to have an SD card?

 builder.setDiskCache(new ExternalCacheDiskCacheFactory(context"MY_CACHE_LOCATION"10010241024));需要传入3个参数context对象,缓存的路径名称,缓存大小

He's actually sdcard/android/data/your app package name the/cache/directory has a cached path name folder to hold your cache content.

Memory caches and pools

Size

Default sizes is determined by the Memorysizecalculator class. The Memorysizecalculator class takes into account the screens size available memory of a given device to come up with Reaso Nable default sizes. You can construct your own instance if you want to adjust Glide ' s defaults:

By default, the size of the cached content is controlled by the Memorysizecalculator class, so how do I control it? Cache depending on the size of the control you want (that is, the portal article writes about storing images based on the size of the ImageView), as well as if you want to customize it yourself.

new MemorySizeCalculator(context);int defaultMemoryCacheSize = calculator.getMemoryCacheSize();int defaultBitmapPoolSize = calculator.getBitmapPoolSize();

When you use the process of a certain stage of memory has a special operation, you can

Do the following in the Registercomponents method.

 glide.setMemoryCategory(MemoryCategory.HIGH);

He has some pre-set enumeration values that you can choose from (that is, zooming in and out, which refers to the memory cache section and the picture pool)

/**     * Tells Glide‘s memory cache and bitmap pool to use at most half of their initial maximum size.     */    LOW(0.5f),    /**     * Tells Glide‘s memory cache and bitmap pool to use at most their initial maximum size.     */    NORMAL(1f),    /**     * Tells Glide‘s memory cache and bitmap pool to use at most one and a half times their initial maximum size.     */    HIGH(1.5f);

Memory Cache

Glide's memory cache is used to hold resources in memory so, they is instantly available without have to perform i/ O.

Glide's memory caches are stored in memory so that they can be used immediately, rather than having to walk the IO operating area to read and write

You can use Glidebuilder's Setmemorycache () method to set the size and/or implementation you wish to use for your memory C Ache. The Lruresourcecache class is Glide ' s default implementation. You can set a custom maximum in memory byte size by passing in the size of want to the Lruresourcecache constructor:

This paragraph is also very verbose, anyway is to tell us can use Lruresourcecache this class to customize the maximum use of memory bytes (see here A lot of small partners have doubts, why there are 2 so-called memory space, simply say is a you come to put the container, one is quick to use)

builder.setMemoryCache(new LruResourceCache(100*1024*1024));

Bitmap Pool

Glide ' s bitmap pool is used to allow Bitmaps of a variety of different sizes to be re-used which can substantially reduce Jank inducing garbage collections caused by pixel array allocations while images is being decoded.

Under the vernacular explanation, the picture pool allows a variety of different sizes of images to be reused, anyway, depending on the size of the control, it will greatly reduce the presence of junk images, while the image is decoded due to pixel array allocation

You can use Glidebuilder's Setbitmappool () method to set the size and/or implementation of the bitmap pool. The Lrubitmappool class is Glide ' s default implementation. The Lrubitmappool class uses an LRU algorithm to retain the most recently used sizes of Bitmaps. You can set a custom maximum in memory byte size by passing the size of want to the Lrubitmappool constructor:

This paragraph anyway means you can also setbitmappool from the size of the image pool, like this

builder.setBitmapPool(new LruBitmapPool(200*1024*1024));

Bitmap Format

The Glidebuilder class also allows you to set a global default for the preferred Bitmap configuration for your application .

By default Glide prefers rgb_565 because it requires only the bytes
per pixel (each) and therefore has half the memory footprint of the
Higher quality and system default argb_8888. rgb_565 however can have
Issues with banding in certain images and also does
Transparency.

If banding is a problem in your application and/or you want the
Highest possible image quality, you can use Glidebuilder ' s
Setdecodeformat method to set decodeformat.always_argb_8888 as Glide ' s
Preferred Bitmap Configuration:

builder.setDecodeFormat(DecodeFormat.ALWAYS_ARGB_8888);

This part is mentioned in the portal, not translated does not explain, hit so many hands tired.

Summary: Simple and easy to use powerful, good performance, fast, very recommended

Configuration is also more convenient than the next door old king

Git:https://github.com/bumptech/glide

Now, let's add a little bit of knowledge.

The last mentioned MD dialog mentioned Collapsingtoolbarlayout and then there are some small friends how to turn that piece of color into a picture or something, then I realized the next, the effect is this

And then put away after itself transparent toolbar also become green, originally green collapsingtoolbarlayout background into a picture, then this is how to do it?

<android. Support. Design. Widgets. CollapsingtoolbarlayoutAndroid:id="@+id/toolbar_layout"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:background="@drawable/gee"Android:fitssystemwindows="true"app:contentscrim="@color/dialogcolor"App:expandedtitlemarginbottom="70DP"App:expandedtitlemarginend="64DP"app:expandedtitlemarginstart="48DP"app:layout_scrollflags="scroll|exituntilcollapsed"app:title="Try Effect">

Contentscrim Set the background color (which is so green) when fully collapsingtoolbarlayout folded (shrunk)

Then I did not find the tag to set the title color of the operation, but the Java method is a

Declaring objects within a class

 CollapsingToolbarLayout toolbar_layout;

Find it in OnCreate and set it up a bit.

  toolbar_layout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);        toolbar_layout.setExpandedTitleColor(getResources().getColor(R.color.dialogColor));

It's not recommended that one imageview go in and then plug in TextView or something. After all, android:background="@drawable/gee" why not?

Source Address: Https://github.com/ddwhan0123/BlogSample/blob/master/GlideDemo.zip

Detailed explanation of the powerful picture loading framework glide configuration (by the way, some features of Collapsingtoolbarlayout)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.