gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:23:01 25 4
gpt4 key购买 nike

这里是一个相对的代码示例。基本上我想做的是预加载一些图像(因此是 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?
}
});
}
}
});

}

最佳答案

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

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

}

关于java - 预加载一堆图像,并在所有图像完成后返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48217537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com