I have an imageView
that uses an alpha channel, is round and basically made up only from black outlines. In a simple instance, I need to, however, display the imageview
not as black, but as white. Everything else needs to stay the same. After a bit of googling, this is what I came up with:
ImageButton btnHeader = new ImageButton(this);
btnHeader.SetBackgroundResource(Resource.Drawable.mainmenu_btn_news_bol);
btnHeader.SetColorFilter(new Color(Color.White), PorterDuff.Mode.SrcIn);
This should at least have some effect on the ImageView
, but it just doesn't. It is, as if the 3rd line of code just gets jumped. Can anyone maybe explain to me if this is a bug or me?
Setting a color filter directly on the ImageButton
will affect only its source image, not the background. That is, it will affect those images set with the SetImage*()
methods. If that's what you mean to do, then simply change the SetBackgroundResource()
call to SetImageResource()
.
If, instead, you are trying to filter the background specifically, you'll need to set the filter directly on the background Drawable
. For example:
btnHeader.Background.SetColorFilter(new Color(Color.White), PorterDuff.Mode.SrcIn);
Furthermore, separate Drawable
s created from the same resource will share a common state by default, so if you have multiple View
s using the same resource image, then setting a filter on one will affect all of the others, as well. You can force a new, separate state to be created for a particular Drawable
by calling Mutate()
on it first, before applying the filter. For example, with the background image:
btnHeader.Background.Mutate().SetColorFilter(new Color(Color.White), PorterDuff.Mode.SrcIn);