【问题标题】:Comparing two PorterDuffColorFilter objects比较两个 PorterDuffColorFilter 对象
【发布时间】:2017-11-17 12:15:18
【问题描述】:

我需要比较两个 PorterDuffColorFilter 对象以检查 ProgressBarEspresso 测试

中是否显示正确的颜色

我试过直接比较两个对象,但是下面的方法总是返回false

// Creating a PorterDuffColorFilter Object
weakOrNonePasswordColorFilter =
            new PorterDuffColorFilter(ContextCompat.getColor(mActivityRule.getActivity(), R.color.red), PorterDuff.Mode.SRC_IN);
...

// Retrieving the ProgressBar ColorFilter and compare
public boolean matchesSafely(ProgressBar progressBar, PorterDuffColorFilter colorFilter) {
  LayerDrawable drawable = (LayerDrawable) progressBar.getProgressDrawable();
  Drawable progressDrawable = drawable.getDrawable(1);
  progressDrawable.setColorFilter(colorFilter);
  return ((PorterDuffColorFilter)progressDrawable.getColorFilter()).equals(colorFilter);
}

查看PorterDuffColorFilter有一个

public int getColor() {
    return mColor;
}

应该可以解决我的问题的方法。但是,我无法访问此方法。 Android Studio 仅显示“对象”方法,如果我尝试使用 .getColor() 编译应用程序,则会显示一条错误消息,指出找不到该方法。

这个问题是不是因为这个类在android.graphics包中,所以无法访问?

如果是这样,我如何检索ProgressBar 条的颜色并将其与颜色进行比较?

【问题讨论】:

  • 能否分享您的代码以便我们提供帮助?
  • 你可以扩展PorterDuffColorFilter,但老实说你的问题闻起来像XY problem
  • 如果是这样,我如何在 Espresso 测试中测试进度条的颜色是否正确?
  • getColor()@hiden 所以 this 可以提供帮助 - 您也可以在测试中使用自定义 PorterDuffColorFilter

标签: android progress-bar android-graphics


【解决方案1】:

PorterDuffColorFilter 覆盖 equals,因此您应该可以使用:filterA.equals(filterB)

// From PorterDuffColorFilter.java

@Override
public boolean equals(Object object) {
    if (this == object) {
        return true;
    }
    if (object == null || getClass() != object.getClass()) {
        return false;
    }
    final PorterDuffColorFilter other = (PorterDuffColorFilter) object;
    return (mColor == other.mColor && mMode.nativeInt == other.mMode.nativeInt);
}

【讨论】:

  • 我已经尝试过了(编辑了原帖),但不幸的是这不起作用,因为它总是返回 false。
  • 聪明的解决方案,谢谢。它对我有用。 @Vanethos 为了让它工作,我不得不进入调试器中的 equals() 来弄清楚如何让它相等。就我而言,一个问题是,当我的特殊情况需要 imageView.getDrawable().getColorFilter() 时,我通过了 imageView.getColorFilter()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 2014-01-26
  • 2012-08-17
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多