Change the color of a bitmap with a function

I need a function that will accept a bitmap and return a bitmap with a changed color. It should be quick and easy.

The goal is to change the color, also this is a png with alpha.

I looked online, but I can’t use canvas or anything external. The function is in an external object (do not ask ..)

Here is what I have had so far (not working). I know that I'm very close, just a matter of sorting the color matrix and getting alpha for the job.

public Bitmap changeBitmapColor(Bitmap sourceBitmap, int deg) { int width, height; height = sourceBitmap.getHeight(); width = sourceBitmap.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); //figure out color matricies. ColorMatrix cm = new ColorMatrix(); //cm.setSaturation(0); cm.set(new float[] { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 }); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(sourceBitmap, 0, 0, paint); return bmpGrayscale; } 

Any help would be great!

------- FIXED --------

I fixed this by changing the color matrix, now the bitmap will change color and not display alpha values.

First of all, this is the matrix:

  cm.set(new float[] { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 140, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 }); 

The second thing I had to change is a line of code:

 Bitmap newBM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

Good luck readers!

+4
source share
4 answers

------- FIXED --------

I fixed this by changing the color matrix, now the bitmap will change color and not display alpha values.

First of all, this is the matrix:

 cm.set(new float[] { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 140, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 }); 

The second thing I had to change is a line of code:

Raster image newBM = Bitmap.createBitmap (width, height, bitmap .Config.ARGB_8888);

Good luck readers!

0
source

I do not understand why autonomy precludes the use of canvas. You can create a new bitmap, create a canvas to draw it, and then draw the original bitmap using Paint, which has a set of color filters (using setColorFilter ). The PorterDuffColorFilter class can help with this.

0
source

I did not understand what is your purpose with changing colors, and what does int deg mean? For an alpha object, this can help change the color model in Bitmap bmpGrayscale from RGB_565 to ARGB_8888 , since it has an alpha channel. Look at Bitmap.Config

0
source

You did not specify exactly what changes you are trying to make in the colors. The cm matrix you defined looks like it has an extra row and it removes the red color, leaves green and alpha alone and multiplies the blue by 255. I assume that is not what you want.

I am going with documentation at http://developer.android.com/reference/android/graphics/ColorMatrix.html

If you are trying to turn the tint, you can get good information from this answer: fooobar.com/questions/180128 / ...

0
source

Source: https://habr.com/ru/post/1402769/


All Articles