Android:在Glide中预加载/缓存图像,加载时图像闪烁 [英] Android : Preloading / Caching images in Glide ,Image blinks while loading

查看:182
本文介绍了Android:在Glide中预加载/缓存图像,加载时图像闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个游戏,在每个场景的开头,我想预加载将要使用的图像,因此每次使用Glide加载图像时,它都不会闪烁.

以下是我的代码.基本上,我只是将Glide加载循环到imageview中,并且应该对图像进行缓存,至少我认为应该这样做:

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

这似乎不起作用.每次第一次加载图像时,它仍然会闪烁.意味着图像没有被缓存.我在做什么错了?

解决方案

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

 <代码> .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">

这篇关于Android:在Glide中预加载/缓存图像,加载时图像闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆