android imageview programatically change color -


if have grayscale image displayed in imageview, can programatically change color? if matters, image has background transparency need remain transparent. want change color of actual image part.

i write simple custom imageview before

below code reference only:

public class colorimageview extends imageview { private context context; private boolean showcolor; private paint mpaint; private colormatrix cm; private bitmap mbitmap;  private float[] color = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,         0, 0, 0, 0 }; private float[] normal = { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,         0, 0, 1, 0 };  public colorimageview(context context) {     super(context);     init(context); }  public colorimageview(context context, attributeset attrs) {     super(context, attrs);     init(context); }  public colorimageview(context context, attributeset attrs, int defstyle) {     super(context, attrs, defstyle);     init(context); }  private void init(context context) {     this.context = context;     showcolor = false;     mpaint = new paint(paint.anti_alias_flag);     cm = new colormatrix(); }  @override public void setimageresource(int resid) {     // todo auto-generated method stub     super.setimageresource(resid);     mbitmap = bitmapfactory.decoderesource(context.getresources(), resid);     invalidate(); }  @override protected void ondraw(canvas canvas) {     // super.ondraw(canvas);     paint paint = mpaint;     paint.setcolorfilter(null);     canvas.drawbitmap(mbitmap, 0, 0, paint);     if (isshowcolor())     {         cm.set(color);     }     else     {         cm.set(normal);     }     paint.setcolorfilter(new colormatrixcolorfilter(cm));     canvas.drawbitmap(mbitmap, 0, 0, paint); }  public void setcolor(int color) {     float red = color.red(color);     float green = color.green(color);     float blue = color.blue(color);     float alpha = color.alpha(color);     // 0,6,12,18     this.color[0] = red / 0xff;     this.color[6] = green / 0xff;     this.color[12] = blue / 0xff;     this.color[18] = alpha / 0xff;     setshowcolor(true); }  public boolean isshowcolor() {     return showcolor; }  //set true show custom color public void setshowcolor(boolean showcolor) {     this.showcolor = showcolor; } } 

usage:

1.put colorimageview in xml

2.set imageview's src in code

3.setcolor(${color}) custom color

4.setshowcolor(true) if want show color.

5.colorimageview.invalidate().(forget if necessary)

then color offset imageview show.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -