gpt4 book ai didi

android - 与 Glide 同步加载图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:06 31 4
gpt4 key购买 nike

我想编写一个 Espresso 匹配器来验证“ImageView”是否具有特定的位图集。由于该应用程序通过 Glide 加载图像,我想我必须在测试端做同样的事情来考虑裁剪/居中,然后才能真正比较预期和实际的位图。

这是我到目前为止的想法:

BitmapRequestBuilder<Uri, Bitmap> bitmapRequest = Glide.with(imageView.getContext())
.load(Uri.parse("file:///android_asset/" + mPath))
.asBitmap();

switch (imageView.getScaleType()) {
case CENTER_CROP:
bitmapRequest.centerCrop();
break;
case FIT_CENTER:
case FIT_START:
case FIT_END:
bitmapRequest.fitCenter();
break;
default:
// no scaling applied to the ImageView under test
}

AtomicReference<Bitmap> bmRef = new AtomicReference<>();
bitmapRequest.into(new SimpleTarget<Bitmap>(
imageView.getMeasuredWidth(),
imageView.getMeasuredHeight()
) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
bmRef.set(resource);
}
});

// ???

try {
Bitmap expected = bmRef.get();
return expected.sameAs(bitmap);
} catch (Exception e) {
throw new IllegalStateException("could not load asset " + mPath, e);
}

现在这里的问题当然是我遇到了僵局。我在主线程上(匹配器在主线程 IIRC 上执行),Glide 希望后端线程加载位图,然后返回主线程(在“onResourceReady”中)本身。所以我需要从外部等待内部发布的结果,同时保持主线程运行。

我(未成功)尝试通过 Looper.loop()//??? 中推进当前循环程序,并且还尝试了常规的锁定/等待方法,但是没有任何效果。我没有想法...

最佳答案

对于这种情况,我使用简单的 sleep,这不是最佳选择,但对我有用。

public static void waitForActivity(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

关于android - 与 Glide 同步加载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625412/

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