glide-Custom Cache

Source: Internet
Author: User
Tags int size parent directory
Preface:

in the previous article, we covered the basics of how to make the default configuration for glide, and now we'll show you how to configure a custom cache Glide Series Catalogue 1.glide-Getting Started tutorial 2.glide-taking up bitmaps and loading animations 3.glide-loading local pictures 4.glide-loading gif 5.glide-binding lifecycle 6.glide-memory cache with disk cache 7. glide-Modules Custom Glide 8.glide-Custom cache 9.glide-image compression 10.glide-picture preprocessing (fillet, Gaussian blur, etc.) 11.glide-picture clipping (scaletype) 12.glide-Source Detailed 1. How to use Glidemodule

first of all, we want to glide the default configuration, need to know how to use Glidemodule, the previous article has done a detailed introduction, this is no longer introduced

How to use Glidemodule

http://blog.csdn.net/yulyu/article/details/55194237

The basics of glide caching are also described earlier, and it's interesting to see

Glide Cache Basics

http://blog.csdn.net/yulyu/article/details/55096713 2. Customizing the Memory Cache

You can customize the size of the memory cache by calling the builder's Setxxx method.

(Memorycach and Bitmappool are related to memory cache)

public class Xiayuglidemodule implements Glidemodule {

    @Override public
    void Applyoptions (context context, Glidebuilder builder) {
        //config memory cache size 10MB
        builder.setmemorycache (New Lruresourcecache (10*1024*1024));
        Configure the picture pool size   20MB
        builder.setbitmappool (New Lrubitmappool (20*1024*1024));
    }

    @Override public
    void registercomponents (context context, Glide Glide) {

    }
}

but the size of the memory cache value should not be arbitrarily configured, glide's internal default value is obtained through a series of calculations, such as determining whether the mobile phone is high-configuration mobile phone, etc. (interested can go to see his source code, or wait for the source code analysis later in this series)
so there's a trickery way to get the default settings for glide, and then make changes based on this setting.

public class Xiayuglidemodule implements Glidemodule {

    @Override public
    void Applyoptions (context context, Glidebuilder builder) {
        //Get Memory calculator
        memorysizecalculator calculator = new Memorysizecalculator (context);
        Get glide default memory cache size
        int                  defaultmemorycachesize = calculator.getmemorycachesize ();
        Get glide default picture pool size
        int                  defaultbitmappoolsize  = Calculator.getbitmappoolsize ();
        Change the value to the previous 1.1 times times
        int                  mymemorycachesize  = (int) (1.1 * defaultmemorycachesize);
        int                  mybitmappoolsize   = (int) (1.1 * defaultbitmappoolsize);
        Modify the default value
        Builder.setmemorycache (new Lruresourcecache (mymemorycachesize));
        Builder.setbitmappool (New Lrubitmappool (mybitmappoolsize));
    }

    @Override public
    void registercomponents (context context, Glide Glide) {

    }
}
3. Customizing the disk cache

The disk cache can be divided into two types according to access rights and paths
-Private cache (available in your app, directory under data/data/app package name)
-External cache (all accessible, directories within the extended space, such as SD card) 3.1 private Cache

The following configuration, mainly to modify the cache size, the cache path is still the default path (that is, the data/data/application package name/cache/image_manager_disk_cache)

public class Xiayuglidemodule implements Glidemodule {

    @Override public
    void Applyoptions (context context, Glidebuilder builder) {
        //Set the disk cache size
        int size = * 1024x768 * 1024x768;
        Set the disk cache
        Builder.setdiskcache (new internalcachediskcachefactory (context, size));
    }

    @Override public
    void registercomponents (context context, Glide Glide) {

    }
}

if the need to modify the cache path, you need to add parameters (to data/data/application package name/cache/xiayu)

    Set the disk cache size
    int size = * 1024x768 * 1024x768;
    String dir = "Xiayu";
    Set the disk cache
    Builder.setdiskcache (new Internalcachediskcachefactory (Context,dir, size));
3.2 External Cache

with this configuration, the cache will be in the sdcard/android/data/app package name/cache/image_manager_disk_cache directory

public class Xiayuglidemodule implements Glidemodule {

    @Override public
    void Applyoptions (context context, Glidebuilder builder) {
        //Set the disk cache size
        int size = * 1024x768 * 1024x768;
        Set the disk cache
        Builder.setdiskcache (new externalcachediskcachefactory (context, size));
    }

    @Override public
    void registercomponents (context context, Glide Glide) {

    }
}

if the need to modify the cache path, you need to add parameters (to sdcard/android/data/application package name/cache/xiayu)

    Set the disk cache size
    int size = * 1024x768 * 1024x768;
    String dir = "Xiayu";
    Set the disk cache
    Builder.setdiskcache (new Externalcachediskcachefactory (Context,dir, size));
3.3 fully customizable paths

The above two caches are the cache of the parent directory is dead, can change only subdirectories, Glide also provides a way to fully customize the cache directory

(Note that the path here is the absolute path of the configuration, so if it is not specified in the SD card directory, it is not directly visible)

public class Xiayuglidemodule implements Glidemodule {

    @Override public
    void Applyoptions (context context, Glidebuilder builder) {
        //Set the disk cache size
        int size = * 1024x768 * 1024x768;
        String dirfolder = "Xia";
        String dirName = "Yu";
        Set the disk cache
        Builder.setdiskcache (new Disklrucachefactory (Dirfolder, size));

        Builder.setdiskcache (New Disklrucachefactory (Dirfolder, dirname,size));
    }

    @Override public
    void registercomponents (context context, Glide Glide) {

    }
}
Popular ArticlesOkhttputils Ultimate Package Facebook launches debug artifact Android Code optimizer super Tool

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.