Issue
I have a custom imageview that should have the possibility to set the colour of an icon either programmatically or from the layouts. This works.
Lets say I use a white icon on a black background.
Then, if the same icon drawable in another place of the app and invert the colours, black icon on white back ground. Then only the icons that has been used before stays in its original colour. And I can't change it either from the layout or programmatically. Same activity but different fragments.
This is the class.
public class IconView extends ImageView {
public IconView(Context context) {
this(context, null);
}
public IconView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.iconViewStyle);
}
public IconView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.IconView, defStyle, 0);
int color = attributes.getColor(R.styleable.IconView_icon_color, Color.WHITE);
ColorFilter cf = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN);
if (getDrawable() != null) getDrawable().setColorFilter(cf);
attributes.recycle();
}
public void setIconColor(int color) {
getDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
Am I doing something wrong, or might android reuse drawables somehow that once they are drawn they stay that way (seems weird)!
Solution
It's likely that the Drawable is being cached, hence the filter is being re-used.
Instead of getDrawable.setColorFilter
, try this.setColorFilter
, which should set the filter on a copy of the drawable, that is not shared with other instances.
Answered By - GreyBeardedGeek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.