Hallo there,
I am creating a custom check box in which I wish to support an infinite number
of colours for the foreground or the background.
To do this I have a background image, and a foreground image which is a tick
that will be drawn over the top of the background.
To set the colours I am adding a colour filter to the foreground or background
drawable, however this is getting 'lost' once the button is drawn.
IF I set the colorFilter on the actual StateListDrawable (sl), then this works
fine, however I want the colour filters to be applied to the underlying images.
Does anyone know what could be going on here? It appears as though the
StateListDrawable is not respecting the colorFilters of it's children.
Cheers,
Peter.
CODE I'M USING BELOW:
CheckBox b = new CheckBox(context);
StateListDrawable sl = new StateListDrawable();
int checked = android.R.attr.state_checked;
int enabled = android.R.attr.state_enabled;
int focused = android.R.attr.state_focused;
int pressed = android.R.attr.state_pressed;
int wFocused = android.R.attr.state_window_focused;
Drawable background =
context.getResources().getDrawable(R.drawable.btn_check_off).mutate();
Drawable foreground =
context.getResources().getDrawable(R.drawable.btn_check_center).mutate();
Drawable selectedBackground =
context.getResources().getDrawable(R.drawable.btn_check_off_selected);
Drawable pressedBackground =
context.getResources().getDrawable(R.drawable.btn_check_off_pressed);
Drawable disabledBackground =
context.getResources().getDrawable(R.drawable.btn_check_off_disable).mutate();
Drawable focusBorder =
context.getResources().getDrawable(R.drawable.focus_border);
if (style != null && style.getForegroundColour() != null)
foreground.setColorFilter(style.getForegroundColour(),
Mode.MULTIPLY);
else
foreground.setColorFilter(Color.GREEN, Mode.MULTIPLY);
if (style != null && style.getBackgroundColour() != null)
{
background.setColorFilter(style.getBackgroundColour(),
Mode.MULTIPLY);
disabledBackground.setColorFilter(style.getBackgroundColour(),
Mode.MULTIPLY);
}
sl.addState(new int[] {checked, -wFocused, enabled}, new
LayerDrawable(new Drawable[] {background, foreground}));
sl.addState(new int[] {-checked, -wFocused, enabled}, background);
sl.addState(new int[] {checked, pressed, enabled}, new
LayerDrawable(new Drawable[] {pressedBackground, foreground}));
sl.addState(new int[] {-checked, pressed, enabled}, pressedBackground);
sl.addState(new int[] {checked, focused, enabled}, new
LayerDrawable(new Drawable[] {selectedBackground, foreground}));
sl.addState(new int[] {-checked, focused, enabled}, selectedBackground);
sl.addState(new int[] {checked, enabled}, new LayerDrawable(new
Drawable[] {background, foreground}));
sl.addState(new int[] {-checked, enabled}, background);
sl.addState(new int[] {checked, -wFocused}, new LayerDrawable(new
Drawable[] {disabledBackground, foreground}));
sl.addState(new int[] {-checked, -wFocused}, disabledBackground);
sl.addState(new int[] {checked, focused}, new LayerDrawable(new
Drawable[] {disabledBackground, focusBorder, foreground}));
sl.addState(new int[] {-checked, focused}, new LayerDrawable(new
Drawable[] {disabledBackground, focusBorder}));
sl.addState(new int[] {checked}, new LayerDrawable(new Drawable[]
{disabledBackground, foreground}));
sl.addState(new int[] {-checked}, disabledBackground);
b.setButtonDrawable(sl);
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en