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;

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

所以我想我可以做这样的事情:

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

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

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

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

  • SRC
  • SRC_ATOP
  • DST_IN
  • ...

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

氪,

短剑

4

1 回答 1

2

我认为您不能为 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,因此您可以d.setColorFilter(mColorFilter);从overridden 中删除DrawableContainer#selectDrawable(int)

于 2013-10-23T15:12:14.527 回答