且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:在Glide中预加载/缓存图像,加载时图像闪烁

更新时间:2023-12-05 16:41:10

如果要缓存图像,则可能必须添加

  .diskCacheStrategy(DiskCacheStrategy.ALL) 

为使它在加载后不会闪烁,请检查下面的代码以供参考.

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

如果您必须像加载普通游戏一样在同一图像视图中一张一张地加载图像,请选中此选项

  int delay = 1000;处理程序处理程序= new Handler();handler.postDelayed(new Runnable(){公共无效run(){//做一点事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;}别的{i = 0;Glide.with(MainActivity.this).load(i).placeholder(menuIcons [i]).override(200,200).centerCrop().dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(myImage);i = i + 1;}}}, 延迟); 

注意:您应该将android:largeHeap ="true"添加到清单文件中,以避免类似Android

 <应用程序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"> 

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?

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">