Expo Speta
Expo Speta

Reputation: 57

Android : Preloading / Caching images in Glide ,Image blinks while loading

I am developing a game and at the start of every scene I would like to preload the images I will be using so it doesn't do the blinking everytime I load the images with Glide.

The following is my code. Basically I just loop Glide load into the imageview and the image should get cached, at least that's what I think it should do :

for (int i : images) {
    Glide.with(this).load(i).into(imageView);
}

imageView.setDrawable(null); 

This doesn't seem to work though. It still does the blink every first time the image is loaded. Meaning the image didn't get cached. What am I doing wrong?

Upvotes: 1

Views: 794

Answers (1)

Sharon Joshi
Sharon Joshi

Reputation: 498

if you want to cach the image then you may have to add

.diskCacheStrategy(DiskCacheStrategy.ALL)

so that it will not blink after loading, please check the below Code for references.

Glide.with(this)
                .load(i)
                .placeholder(R.drawable.default_image)
                .error(R.drawable.default_image)
                .override(200, 200)
                .centerCrop()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);

and if you have to load images one by one in the same image view just like the stuff which the usual game loading's do, just check this

 int delay = 1000;
    Handler handler = new Handler();
handler.postDelayed(new Runnable() {
            public void run() {
                //do something
                handler.postDelayed(this, delay);

                if(i<imageArray.length){
                    Glide.with(MainActivity.this)
                        .load(i)
                        .placeholder(menuIcons[i])
                        .override(200, 200)
                        .centerCrop()
                            .dontAnimate()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(myImage);

                    i=i+1;
                }else{
                    i=0;
                      Glide.with(MainActivity.this)
                        .load(i)
                        .placeholder(menuIcons[i])
                        .override(200, 200)
                        .centerCrop()
                            .dontAnimate()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(myImage);

                    i=i+1;
                }
            }
        }, delay); 

NOTE : you should add android:largeHeap="true" to your manifest file for avoiding Android:java.lang.OutOfMemoryError , just like this

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

Upvotes: 3

Related Questions