This source code analysis based on 4.12.0, if there is a description error, please comment pointed out.
A, Glide usage
// Use RecyclerView to load images
@Override
public void onBindViewHolder(PhotoViewHolder holder, int position) {
GlideApp.with(holder.itemView).load(list.get(position))
.transform(new RoundedCorners(40))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.into(holder.imageView);
}
Copy the code
Glide, Glide, Glide, Glide
-
2.1 How does Glide sense the life cycle of Application, Activity and Fragment?
Q: What would you do if you wanted to sense the application’s out-of-memory methods?
Source decryption:
Q: How does Glide sense Activity/Fragment onDestroy when it loads an ImageView and returns it immediately?
Is, of course, the corresponding Act or fragments to insert a blank SupportRequestManagerFragment implementation.
GlideApp.with(activity).load("FM = https://t7.baidu.com/it/u=3652245443, 3894439772 & 193 & f = GIF").into(view);
GlideApp.with(fragment).load("FM = https://t7.baidu.com/it/u=3652245443, 3894439772 & 193 & f = GIF").into(view);
Copy the code
If our Activity has an ImageView and it has two fragments that load the ImageView, the with method should be based on the context in which the ImageView is located, and the Act is passed, The preach fragments is Framgent yes, as a result, Glide embedded 3 SupportRequestManagerFragment entered our page. That’s kind of impressive.
But if you accidentally write something like this inside a Fragment
/ / the ImageView fragments
GlideApp.with(view).load("FM = https://t7.baidu.com/it/u=3652245443, 3894439772 & 193 & f = GIF").into(view);
Copy the code
I used a simple demo to simulate this scenario. Glide could not find the Fragment from the View. }{#ff0000}}{View is a Fragment that contains a View.} So be careful when you write the with method.
-
What is the default size of Glide MemoryCache(LruResourceCache), LruBitmapPool and DiskLruCache?
final int MEMORY_CACHE_TARGET_SCREENS = 2;
final int BITMAP_POOL_TARGET_SCREENS =Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 4 : 1;
int widthPixels =context.getResources().getDisplayMetrics().widthPixels;
int heightPixels =context.getResources().getDisplayMetrics().heightPixels;
int screenSize = widthPixels * heightPixels * BYTES_PER_ARGB_8888_PIXEL;
// The size of the image
int targetBitmapPoolSize = Math.round(screenSize * BITMAP_POOL_TARGET_SCREENS);
//2 screen size
int targetMemoryCacheSize = Math.round(screenSize * MEMORY_CACHE_TARGET_SCREENS);
int DEFAULT_DISK_CACHE_SIZE = 250 * 1024 * 1024;
String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";
File cacheDirectory = context.getCacheDir();
Copy the code
The memory cache LruResourceCache defaults to only 2 screen size images. LruBitmapPool by default only 1 or 4 screen size bitmaps can be reused. DiskLruCache is installed with a built-in SD card by default and occupies 250 MB of space.
-
2.3 When does the addition and removal of level 3 cache occur?
WeakReference Memory cache LruResourceCache Disk cache
Once the same Bitmap is removed from LruResourceCache, it will only have weak references in the cache. If the weak references are removed, then LruCache is added to the cache. ??
The result of the current test: once the image is loaded ok, add the weak reference cache first. If it is recyclerView list, item reuse, ImageView will be into many times, the weak reference corresponding to the Bitmap has long been gone, then the Bitmap will add LruResourceCache. If it is not a list but a page-loaded ImageView, weak references to the Bitmap are cleared when the page is closed and LruResourceCache is added. If LruResourceCache is out of memory, trim it.
DiskLruCache is more likely to result in an OOM if the cache policy is not selected correctly.
-
How can Glide distinguish a Url content is PNG, JPG, or GIF?
Let’s take a look at it and we’ll talk about it later.