[英]Loading image synchronously with Glide
我想写一个Espresso matcher来验证`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()
来推进当前的Looper.loop()
器 并尝试了常规的锁定/等待方法,但没有任何效果。 我没有想法......
对于这样的情况,我使用简单的sleep
,它不是最好的选择,但它适用于我。
public static void waitForActivity(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
一种方法是将比较逻辑保存在onResourceReady中,然后如果使用任何事件总线,则使用结果触发事件,并将UI逻辑放入订阅此事件的方法中。 如果没有使用任何事件总线,那么LocalBroadcastManager来广播结果。
例如:
private BroadcastReceiver receiver;
private void doWhateverYouWantWithResult(boolean result) {
}
bitmapRequest.into(new SimpleTarget<Bitmap>(
imageView.getMeasuredWidth(),
imageView.getMeasuredHeight()
) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
bmRef.set(resource);
Bitmap expected = resource;
boolean result = expected.sameAs(bitmap);
Intent localIntent =
new Intent(BROADCAST_ACTION)
// Puts the status into the Intent
.putExtra(MATCH_RESULT, result);
// Broadcasts the Intent to receivers in this app.
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
});
@Override
public void onResume(){
super.onResume();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean result = intent.getBooleanExtra(MATCH_RESULT);
doWhateverYouWantWithResult(result);
}
};
LocalBroadcastManager.getInstance(getContext())
.registerReceiver(receiver, new IntentFilter(BROADCAST_ACTION));
}
@Override
public void onPause(){
super.onPause();
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(receiver);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.