Android Espresso将具有不同色调的BitmapDrawables匹配 [英] Android Espresso match BitmapDrawables with different tint
问题描述
有什么方法可以比较由BitmapDrawable
包裹的两个Bitmaps
.
Is there some way to compare two Bitmaps
which are wrapped by the BitmapDrawable
.
如果大小不匹配,则比较应该不会失败,但是应该匹配像素和Bitmap
The comparison should not fail if the sizes doesn't match, but it should match pixels and the color of the Bitmap
我不确定Android的本机部分如何绘制Bitmap
,因为因为sameAs
即使色泽颜色不同也返回true.
I am not sure how the native part of Android draws the Bitmap
, because sameAs
returns true even though the tint color is different.
如果大小不同,我可以彼此创建缩放的Bitmap
并将其与之比较.
If the size is different, I can create scaled Bitmap
from the other and the compare these to.
在我的源代码中,我将DrawableCompat.setTint
与ImageViews
Drawable
一起使用,并且在测试代码中,我从资源中加载了Drawable
并对其进行了相同的着色.
In my source code I use DrawableCompat.setTint
with the ImageViews
Drawable
and in the test code I load Drawable
from resources and tint it same way.
有什么想法吗?我想进行一次测试,以根据是否按下ImageView
来验证ImageView
的来源Drawable
和颜色.
Any ideas? I would like to have test which validates the source Drawable
of ImageView
and the color as well based on if it's pressed or not.
注意1:我的可绘制对象为白色,并且使用色调设置颜色. Bitmaps
的循环像素不起作用,因为此时像素为白色,最有可能是Android本机面在绘制时使用淡色.
NOTE 1: My drawables are white, and I use tint to set color. Looping pixels for Bitmaps
doesn't work because they are white at that point, most likely Android native side uses tint color when drawing.
注意2:使用compile 'com.android.support:palette-v7:21.0.0'
和Palette.from(bitmap).generate();
都没有帮助,因为返回的调色板具有0个色板,因此无法在此获取任何颜色信息.
NOTE 2: Using compile 'com.android.support:palette-v7:21.0.0'
and Palette.from(bitmap).generate();
doesn't help either because the returned palette has 0 swatches so can't get any color information there.
这是我当前的匹配者:
public static Matcher<View> withDrawable(final Drawable d) {
return new BoundedMatcher<View, ImageView>(ImageView.class) {
@Override
public boolean matchesSafely(ImageView iv) {
if (d == null) {
return iv.getDrawable() == null;
} else if (iv.getDrawable() == null) {
return false;
}
if (d instanceof BitmapDrawable && iv.getDrawable() instanceof BitmapDrawable) {
BitmapDrawable d1 = (BitmapDrawable) d;
BitmapDrawable d2 = (BitmapDrawable) iv.getDrawable();
Bitmap b1 = d1.getBitmap();
Bitmap b2 = d2.getBitmap();
return b1.sameAs(b2);
}
return iv.getDrawable().getConstantState().equals(d.getConstantState());
}
@Override
public void describeTo(Description description) {
description.appendText("with drawable: ");
}
};
}
谢谢.
推荐答案
我遇到了相反的问题:当可绘制对象的大小和/或色调可以更改时,匹配它.
I've had the opposite issue: matching a drawable when it's size and/or tint can change.
当我将drawable转换为位图时,我使用它的默认大小并应用黑色:
When I convert drawable to bitmap, I use it's default size and apply a black tint:
private fun getBitmapFromDrawable(drawable: Drawable): Bitmap {
val bitmap: Bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.apply {
setBounds(0, 0, canvas.width, canvas.height)
setTint(Color.BLACK)
draw(canvas)
}
return bitmap
}
用法:
val bitmap = getBitmapFromDrawable(currentDrawable)
val otherBitmap = getBitmapFromDrawable(otherDrawable)
return bitmap.sameAs(otherBitmap)
您可以使用新参数来匹配可绘制颜色...
You could use a new parameter for matching the drawable color...
private fun getBitmapFromDrawable(drawable: Drawable, color:Int): Bitmap {
val bitmap: Bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.apply {
setBounds(0, 0, canvas.width, canvas.height)
setTint(color)
draw(canvas)
}
return bitmap
}
用法:
val bitmap = getBitmapFromDrawable(currentDrawable, color)
val otherBitmap = getBitmapFromDrawable(otherDrawable, color)
return bitmap.sameAs(otherBitmap)
这篇关于Android Espresso将具有不同色调的BitmapDrawables匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!