gpt4 book ai didi

android - 带有 ColorFilter 的 Ninepatch Drawable

转载 作者:太空狗 更新时间:2023-10-29 13:31:06 27 4
gpt4 key购买 nike

我正在创建一些日历 View ,我想做的是为可点击的 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 必须是可变的,但它必须基于 nine-patch drawable。

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

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

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

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

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

  • 资源中心
  • SRC_ATOP
  • DST_IN
  • ...

如果您知道我做错了什么或者我还能做些什么来解决我的问题,请告诉我! :-)

德克

最佳答案

我认为您不能为 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);

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

关于android - 带有 ColorFilter 的 Ninepatch Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15359159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com