I need help understanding androids LruCache. I want to use to load images into my gridview in order make the loading/scrolling better. Can someone post an example code using LruCache please. Thanks in advance.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
https://techienotes.info/2015/08/28/caching-bitmaps-in-android-using-lrucache/
This link has a full project having sample application to load images into Gridview using LruCache.
This class is using LruCache and taken from the code given in the link
I've found a really easy way that work perfectly for me...
This is the Cache.java class. In this class, the static
getInstance()
method enables us to create only one cache instance in the whole application.getLru()
method is used to retrieve the cached object, it will be shown later how to use it. This cache is generic, meaning you can save any Object type into it. The cache memory size here is set to 1024. It can be changed if it is too small:This is the code in your activity where you save the bitmap to the cache:
This is the code where you retrieve the bitmap from the cache, then set an imageView to this bitmap:
THAT'S ALL! As you can see this is rather easy and simple.
In my application, All the views are saved in class variables so they can be seen by all the methods in the class. In my first activity, I save the image bitmap to the cache in an
onClickButton()
method, right before I start a new activity using intent. I also save a string value in my cache:Then I navigate from the second activity to a third activity also using intents. In the last activity I save other objects into my cache, then go back to the first activity using an intent. Once I'm back in the first activity, the
onCreate()
method will start. In that method, I check if my cache has any bitmap value or any String value separately (based on my application business):Below is a class I made for using LruCache, this is based on the presentation Doing More With Less: Being a Good Android Citizen given at Google I/O 2012.
Check out the movie for more information about what I'm doing in the
TCImageLoader
class:Take a look at Caching Bitmaps where the use of
LruCache
is demonstrated.The relevant portion of the code from the page is as follows:-