我正在创建一些日历视图,我想要做的是为一个点击的LineairLayout创建一个背景。
因此,我创建了一个包含两个图像的StateListDrawable:
到目前为止,这段代码使用的是:
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的:
如果你知道我做错了什么或者我能做些什么来解决我的问题,请告诉我!:-)
Kr,
德克
发布于 2013-10-23 15:12:14
我不认为您可以为每个可绘制的ColorFilters分配StateListDrawable。原因:当ColorFilter更改状态时,StateListDrawable将被移除/替换。若要查看此操作,请更改语句的顺序,以便:
background.setColorFilter(Color.RED, PorterDuff.Mode.DST_IN);在创建StateListDrawable之后。您将看到应用了ColorFilter。但是,一旦状态发生变化(单击,然后发布),ColorFilter就不再存在了。
StateListDrawables允许您设置一个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。LinearLayouts没有前台属性。
// StateListDrawable
frameLayout.setBackground(sld);
// For tint
frameLayout.setForeground(sldOverlay);它确实涉及透支,使它成为一个次优的解决方案/解决办法。也许您可以考虑扩展StateListDrawable和DrawableContainer。而且,由于您没有对ColorFilter使用StateListDrawable,所以可以从覆盖的DrawableContainer#selectDrawable(int)中删除d.setColorFilter(mColorFilter);。
https://stackoverflow.com/questions/15359159
复制相似问题