使用ColorFilter的Ninepatch Drawable

3
我正在创建一些日历视图,我想要做的是为一个可点击的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;

但我想做一些额外的事情。我希望用户能够传递一个颜色,以便将其用于显示背景。因此,背景变量必须是可变的,但它必须基于九宫格可绘制对象。
所以我想我可以这样做:
background.setColorFilter(Color.RED, PorterDuff.Mode.DST_IN);

需要将Color.RED替换为用户选择的颜色。

但是似乎并没有起作用。九宫格图案被完美地创建,但是颜色过滤器未被应用。

我还尝试了其他PoterDuff.Mode:

  • SRC
  • SRC_ATOP
  • DST_IN
  • ...

如果您知道我做错了什么或者有其他解决方法,请告诉我!:-)

Kr,

Dirk


据我所知,状态列表会移除所有附加在可绘制对象上的颜色过滤器。因此问题可能不是9patch部分,而是状态列表。 您能否尝试移除状态列表,将颜色过滤器应用于可绘制对象并反馈结果? - chuck258
1个回答

2

我认为您不能为StateListDrawable中的每个Drawable分配ColorFilters。原因是:当StateListDrawable更改状态时,ColorFilter将被删除/替换。要看到此操作,请更改语句的顺序,例如:

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

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

StateListDrawable允许您设置ColorFilter:StateListDrawable#setColorFilter(ColorFilter)。这是如何使用提供的(或null)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。LinearLayout没有前景属性。
// StateListDrawable
frameLayout.setBackground(sld);

// For tint
frameLayout.setForeground(sldOverlay);

这确实涉及到了过度绘制,使其成为一种次优的解决方案/变通方法。也许您可以考虑扩展StateListDrawable和DrawableContainer。由于您没有为StateListDrawable使用ColorFilter,因此可以从重写的DrawableContainer#selectDrawable(int)中删除d.setColorFilter(mColorFilter)。


嗯...我得好好考虑一下。虽然我不是最初提问者,但它确实涵盖了我在办公时间聊天中遇到的一个有趣场景。能够在运行时选择颜色并应用于九宫格图案,基本上需要使用ColorFilter,尽管我在Android的这个特定领域远非专家。StateListDrawable是因为在许多情况下,这个九宫格图案将被应用于应该是可点击元素的东西。感谢您的意见! - CommonsWare
@CommonsWare “…这个九宫格图案将被应用于应该是可点击元素的某些东西上”- 令人惊讶的是StateListDrawable是以这种方式实现的。为单个Drawable传递的ColorFilters作为StateListDrawable#addState()的附加参数将提供更多的控制/选择。或者,在为单个Drawable设置/覆盖ColorFilter之前进行空值检查,并使用在StateListDrawable本身上设置的ColorFilter来替换单个Drawable的ColorFilter也可以起作用。好吧,本来可以做到的,现在只能通过“extends”… - Vikram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接