【问题标题】:Ninepatch Drawable with ColorFilterNinepatch Drawable 与 ColorFilter
【发布时间】:2013-02-27 20:15:19
【问题描述】:

我正在创建一些日历视图,我想做的是为可点击的 LineairLayout 创建一个背景。

因此,我创建了一个包含两个图像的 StateListDrawable:

  1. 背景图片
  2. 项目被按下时的图像

到目前为止,这段代码可以工作:

    NinePatchDrawable background = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.calendar_item);
    Drawable backgroundFocus = context.getResources().getDrawable(R.drawable.calendar_focus);

    int stateFocused = android.R.attr.state_focused;
    int statePressed = android.R.attr.state_pressed;

    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[]{ stateFocused,  statePressed}, backgroundFocus);
    sld.addState(new int[]{-stateFocused,  statePressed}, backgroundFocus);
    sld.addState(new int[]{-stateFocused}, background);
    return sld;

但我想做一些额外的事情。我希望用户能够传递他想要用来显示背景的颜色。所以背景var必须是可变的,但必须是基于九个补丁的drawable。

所以我想我可以这样做:

background.setColorFilter(Color.RED, PorterDuff.Mode.DST_IN);

其中 Color.RED 必须替换为用户选择的颜色。

但这似乎不起作用。完美地创建了九个补丁,但没有应用颜色过滤器。

我还尝试了其他 PoterDuff.Mode 的:

  • SRC
  • SRC_ATOP
  • DST_IN
  • ...

如果您有任何线索我做错了什么或我可以做些什么来解决我的问题,请告诉我! :-)

氪,

德克

【问题讨论】:

  • AFAIK 状态列表会删除所有附加到可绘制对象的颜色过滤器。所以问题可能不在于 9patch 部分,而可能是状态列表。能否请您测试以删除状态列表,将颜色过滤器应用于可绘制对象并提供反馈?

标签: android nine-patch android-drawable porter-duff statelistdrawable


【解决方案1】:

我认为您不能为 StateListDrawable 中的每个 Drawable 分配 ColorFilters。原因:当 StateListDrawable 改变状态时,ColorFilter 将被移除/替换。要查看实际情况,请更改语句的顺序,以便:

background.setColorFilter(Color.RED, PorterDuff.Mode.DST_IN);

在创建 StateListDrawable 之后出现。您会看到已应用 ColorFilter。但是,一旦状态发生变化(单击,然后释放),ColorFilter 就不再存在了。

StateListDrawables 允许您设置 ColorFilter:StateListDrawable#setColorFilter(ColorFilter)。这就是使用提供的(或空的)ColorFilter 的方式:

StateListDrawable#onStateChange(int[])

@Override
protected boolean onStateChange(int[] stateSet) {
    ....
    if (selectDrawable(idx)) {    // DrawableContainer#selectDrawable(int)
        return true;
    }
    ....
}

DrawableContainer#selectDrawable(int)

public boolean selectDrawable(int idx) {
    ....
    if (idx >= 0 && idx < mDrawableContainerState.mNumChildren) {
        Drawable d = mDrawableContainerState.mDrawables[idx];
        mCurrDrawable = d;
        mCurIndex = idx;

        if (d != null) {
            ....

            // So, at this stage, any ColorFilter you might have supplied
            // to `d` will be replaced by the ColorFilter you
            // supplied to the StateListDrawable, or `null`
            // if you didn't supply any.                 
            d.setColorFilter(mColorFilter);

            ....
        }
    } else {
        ....
    }
}

解决方法

如果可能,请使用 ImageView(尺寸为 match_parent)进行视觉交流。将您创建的 StateListDrawable 设置为 ImageView 的背景。为叠加层创建另一个 StateListDrawable:

StateListDrawable sldOverlay = new StateListDrawable();

// Match Colors with states (and ultimately, Drawables)
sldOverlay.addState(new int[] { statePressed }, 
                         new ColorDrawable(Color.TRANSPARENT));

sldOverlay.addState(new int[] { -statePressed }, 
                         new ColorDrawable(Color.parseColor("#50000000")));

// Drawable that you already have
iv1.setBackground(sld);

// Drawable that you just created
iv1.setImageDrawable(sldOverlay);

另一种可能性:使用 FrameLayout 代替 LinearLayout。 LinearLayouts 没有前景属性。

// StateListDrawable
frameLayout.setBackground(sld);

// For tint
frameLayout.setForeground(sldOverlay);

它确实涉及透支,使其成为次优解决方案/解决方法。也许你可以看看扩展 StateListDrawable 和 DrawableContainer。由于您没有为 StateListDrawable 使用 ColorFilter,您可以从覆盖的 DrawableContainer#selectDrawable(int) 中删除 d.setColorFilter(mColorFilter);

【讨论】:

  • 嗯...我将不得不考虑这些。虽然我不是这个问题的最初提问者,但它确实巧妙地涵盖了我在办公时间聊天中提出并发现有趣的场景。能够在运行时选择一种颜色以应用于九个补丁几乎需要ColorFilter AFAIK,尽管我在 Android 的这个特定角落还远非专家。 StateListDrawable 是因为在许多情况下,这九个补丁将应用于应该是可点击元素的东西。感谢您的意见!
  • @CommonsWare ... this nine-patch would be applied to something that ought to be a clickable element - StateListDrawable 以这种方式实现有点令人惊讶。 ColorFilters 对于作为附加参数传递给 StateListDrawable#addState() 的单个 Drawables 将提供更多控制/选择。或者,在设置/覆盖具有 ColorFilter 集 on StateListDrawable 本身的单个 Drawable 的 ColorFilter 之前进行空检查也可以工作。好吧,应该有,应该有,extends ...
猜你喜欢
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 2011-05-18
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多