In the following I am trying to change color of of Ic_Launcher image by using color transform matrix method. While it works for red static matrix present in the code its not working for other colors that I am trying to convert form hex code to ARGB and finally the color transform matrix. something wrong in my code?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher);
ImageView iv=(ImageView)findViewById(R.id.imageView);
changeBitmapColor(icon,iv);
}
public void changeBitmapColor(Bitmap sourceBitmap,ImageView iv){
float[] colorTransformRed = {
0, 1f, 0, 0, 0,
0, 0, 0f, 0, 0,
0, 0, 0, 0f, 0,
0, 0, 0, 1f, 0
};
float[] colorTransform=rgbToFloatArray(hexToRGBArray("FF00FF"));
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0.0f); //Remove Colour
colorMatrix.set(colorTransform); //Apply the new color
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(sourceBitmap.getHeight() * 0.15), sourceBitmap.getWidth(), (int)(sourceBitmap.getHeight() * 0.75));
iv.setImageBitmap(resultBitmap);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, paint);
}
public float[] rgbToFloatArray(float[]rgb){
float a=1f,b=0f,c=0f,d=0f,e=0f,
f=0f,g=1f,h=0f,i=0f,j=0f,
k=0f,l=0f,m=1,n=0f,o=0f,
p=0f, q=0f,r=0f,s=1f,t=0f;
float R=Float.parseFloat(rgb[0]+"");
float G=Float.parseFloat(rgb[1]+"");
float B=Float.parseFloat(rgb[2]+"");
float A=Float.parseFloat(rgb[3]+"");
System.out.println("R---"+R+"==G--"+G+"==B---"+B+"==A---"+A);
float[] tm= {
a * R, b * G, c * B, d * A, e,
f * R, g * G, h * B, i * A , j,
k * R, l * G, m * B, n * A, o,
p * R, q * G, r * B,s*A , t
};
return tm;
}
public float[] hexToRGBArray(String hex){
float[]rgb=new float[4];
int color = (int)Long.parseLong(hex, 16);
float r= (color >> 16) & 0xFF;
float g= (color >> 8) & 0xFF;
float b= (color >> 0) & 0xFF;
float a= (color >> 24) & 0xFF;
rgb[0]=r;
rgb[1]=g;
rgb[2]=b;
rgb[3]=a;
return rgb;
}