Example using Androids lrucache

前端 未结 5 2027
予麋鹿
予麋鹿 2020-12-02 09:02

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

5条回答
  •  春和景丽
    2020-12-02 09:29

    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:

    import android.support.v4.util.LruCache;
    
    public class Cache {
    
        private static Cache instance;
        private LruCache lru;
    
        private Cache() {
    
            lru = new LruCache(1024);
    
        }
    
        public static Cache getInstance() {
    
            if (instance == null) {
    
                instance = new Cache();
            }
    
            return instance;
    
        }
    
        public LruCache getLru() {
            return lru;
        }
    }
    

    This is the code in your activity where you save the bitmap to the cache:

    public void saveBitmapToCahche(){
    
            //The imageView that you want to save it's bitmap image resourse
            ImageView imageView = (ImageView) findViewById(R.id.imageViewID);
    
            //To get the bitmap from the imageView
            Bitmap bitmap = ((BitmapDrawable)imageview.getDrawable()).getBitmap();
    
            //Saving bitmap to cache. it will later be retrieved using the bitmap_image key
            Cache.getInstance().getLru().put("bitmap_image", bitmap);
        }
    

    This is the code where you retrieve the bitmap from the cache, then set an imageView to this bitmap:

    public void retrieveBitmapFromCache(){
    
            //The imageView that you want to set to the retrieved bitmap
            ImageView imageView = (ImageView) findViewById(R.id.imageViewID);
    
            //To get bitmap from cache using the key. Must cast retrieved cache Object to Bitmap
            Bitmap bitmap = (Bitmap)Cache.getInstance().getLru().get("bitmap_image");
    
            //Setting imageView to retrieved bitmap from cache
            imageView.setImageBitmap(bitmap));
    
    }
    

    THAT'S ALL! As you can see this is rather easy and simple.

    • EXAMPLE:

    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:

    public void onClickButton(View v){
    
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        String name = textEdit.getText().toString();
    
        Cache.getInstance().getLru().put("bitmap_image", bitmap);
        Cache.getInstance().getLru().put("name", name);
    
        Intent i = new Intent(FirstActivity.this, SecondActivity.class);
        startActivity(i);
    }
    

    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):

    public ImageView imageView;
    public EditText editText;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
    
            //...Other code...
    
            //The imageView that you want to save it's bitmap image resourse
            imageView = (ImageView) findViewById(R.id.imageViewID);
    
            //The editText that I want to save it's text into cache
            editText = (EditText)findViewById(R.id.editTextID);
    
            if(Cache.getInstance().getLru().get("name")!=null){
                 editText.setText(Cache.getInstance().getLru().get("name").toString());
            }
            if(Cache.getInstance().getLru().get("bitmap_image")!=null){
                 imageView.setImageBitmap((Bitmap)Cache.getInstance().getLru().get("bitmap_image"));
            }
    
            //...Other code...
        }
    

提交回复
热议问题