预加载一堆图像,并在所有图像完成后返回

时间:2018-01-11 23:42:41

标签: java android android-glide

这是一个相对代码示例。基本上我要做的是预加载一些图像(因此for循环)。预加载工作,但我不知道什么时候完成。我知道有听众,但我不确定这些是如何工作的

当完全加载for循环中的所有图像时,是否存在仅从此方法返回的解决方案。这有意义吗?

public void method(){
    final Activity activity = getCurrentActivity();
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < sources.size(); i++) {


                        final ReadableMap source = sources.getMap(i);
                        final GlideUrl glideUrl = FastImageViewConverter.glideUrl(source);
                        final Priority priority = FastImageViewConverter.priority(source);
                        Glide
                                .with(activity.getApplicationContext())
                                .load(glideUrl)
                                .priority(priority)
                                .placeholder(TRANSPARENT_DRAWABLE)
                                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                                .preload()
                                .listener(new RequestListener<Uri, GlideDrawable>() {
                                    @Override public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
                                        return false;
                                    }
                                    @Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                        // easy
                                        return false;
                                        // impossible?
                                    }
                                });
                    }
                }
            });

}

2 个答案:

答案 0 :(得分:1)

如果您知道要预加载多少图像,也许您可​​以设置一个计数器,并在onResourceReady中增加此计数器。一旦它达到你想要预加载的图像数量,你就完成了,你可以继续执行其他的东西

答案 1 :(得分:1)

这不是最好的答案,但可能适合您的情况:)

public void method(){
    final Activity activity = getCurrentActivity();
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    int counter = sources.size();
                    for (int i = 0; i < sources.size(); i++) {


                        final ReadableMap source = sources.getMap(i);
                        final GlideUrl glideUrl = FastImageViewConverter.glideUrl(source);
                        final Priority priority = FastImageViewConverter.priority(source);
                        Glide
                                .with(activity.getApplicationContext())
                                .load(glideUrl)
                                .priority(priority)
                                .placeholder(TRANSPARENT_DRAWABLE)
                                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                                .preload()
                                .listener(new RequestListener<Uri, GlideDrawable>() {
                                    @Override public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
                                        counter--;
                                        if counter == 0 {
                                            return your result here
                                        }
                                        return false;
                                    }
                                    @Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                        counter--;
                                        if counter == 0 {
                                            return your result here
                                        }
                                        return false;
                                        // impossible?
                                    }
                                });
                    }
                }
            });

}