不同色调的Android Espresso match BitmapDrawables

不同色调的Android Espresso match BitmapDrawables,android,android-drawable,android-bitmap,android-espresso,Android,Android Drawable,Android Bitmap,Android Espresso,是否有某种方法可以比较两个位图,这两个位图由BitmapDrawable包装 如果大小不匹配,比较不应该失败,但应该匹配像素和位图的颜色 我不确定Android的本机部分是如何绘制位图的,因为sameAs返回true,即使色调不同 如果大小不同,我可以从另一个创建缩放的位图,并将其与之进行比较 在我的源代码中,我将Drawable compat.setTint与imageviewDrawable一起使用,在测试代码中,我从参考资料中加载Drawable,并以相同的方式对其着色 有什么想法吗?我想

是否有某种方法可以比较两个
位图
,这两个位图由
BitmapDrawable
包装

如果大小不匹配,比较不应该失败,但应该匹配像素和
位图的颜色

我不确定Android的本机部分是如何绘制
位图的,因为
sameAs
返回true,即使色调不同

如果大小不同,我可以从另一个创建缩放的
位图
,并将其与之进行比较

在我的源代码中,我将
Drawable compat.setTint
imageview
Drawable
一起使用,在测试代码中,我从参考资料中加载
Drawable
,并以相同的方式对其着色

有什么想法吗?我想做一个测试,验证
ImageView
的源代码
Drawable
,以及基于是否按下的颜色

注1:我的可拉丝布是白色的,我使用色调来设置颜色。为
位图
循环像素不起作用,因为此时它们是白色的,最有可能的是Android本机端在绘制时使用着色颜色

注2:使用
编译'com.android.support:palete-v7:21.0.0'
调色板.from(位图).generate()也没有帮助,因为返回的调色板有0个样例,因此无法在那里获取任何颜色信息

这是我目前的匹配者:

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: ");
        }
    };
}
公共静态匹配器可撤销(最终可撤销d){
返回新的BoundedMatcher(ImageView.class){
@凌驾
公共布尔匹配安全(ImageView iv){
如果(d==null){
return iv.getDrawable()==null;
}else if(iv.getDrawable()==null){
返回false;
}
if(d instanceof BitmapDrawable&&iv.getDrawable()instanceof BitmapDrawable){
BitmapDrawable d1=(BitmapDrawable)d;
BitmapDrawable d2=(BitmapDrawable)iv.getDrawable();
位图b1=d1.getBitmap();
位图b2=d2.getBitmap();
返回b1.sameAs(b2);
}
return iv.getDrawable().getConstantState().equals(d.getConstantState());
}
@凌驾
公共无效说明(说明){
description.appendText(“带可绘制:”);
}
};
}

谢谢。

我使用弗兰基·萨多(Frankie Sardo)写的匹配器来实现这个目的-。有几个匹配者必须解决您的问题。所有的功劳都归功于他。

我遇到了一个相反的问题:当一个可拉丝面料的尺寸和/或色调可以改变时,匹配它

将drawable转换为位图时,我使用其默认大小并应用黑色色调:

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)
val bitmap = getBitmapFromDrawable(currentDrawable, color)
val otherBitmap = getBitmapFromDrawable(otherDrawable, color)

return bitmap.sameAs(otherBitmap)
您可以使用一个新参数来匹配可绘制的颜色

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)
val otherBitmap = getBitmapFromDrawable(otherDrawable)

return bitmap.sameAs(otherBitmap)
val bitmap = getBitmapFromDrawable(currentDrawable, color)
val otherBitmap = getBitmapFromDrawable(otherDrawable, color)

return bitmap.sameAs(otherBitmap)

比较结果似乎与我使用Bitmap.sameAs的方式完全相同,我不明白为什么这对两种不同颜色的BitmapDrawable是正确的。请看这里。第一个答案为您提供了如何获得颜色的提示。也许这就是你想要的。我的资产是白色的,我使用着色,所以Android的本机端似乎也保持位图像素的白色,并且在绘图或类似的事情时进行着色。由于这个原因,两个位图之间的像素比较不起作用。我的上一次尝试:)-“您可以使用调色板类中的getter方法从图像中检索突出的颜色,例如palete.getVibrantColor。”从这里我也尝试过,两个位图调色板都没有样例,因此也无法从中获取任何颜色信息