rosu alin
rosu alin

Reputation: 5830

How can i change the color of a picture that I got on canvas

I have a canvas, with a background picture, on which i ma able to write, draw lines, and put smaller pictures, to rotate, scale, etc. I need to change the color of one of those bitmaps. I have a color picker which looks like this:

public void colorChanged(int color) {
    if (isText) {
        myView.setTextColor(color);
    } else if(isDrawing) {
        mPaint.setColor(color);
        myView.setPaint(mPaint);
    } else if(ispic) {
           //TODO
    }
}

I tried something similar with what i have on me isText part of code, but it only changes the color of the line that follows where i put, or move my picture (which is currently transparent, if i do not change the color).

myView is a CustomView, on which i have my onDraw methods.

Upvotes: 1

Views: 2038

Answers (2)

Dheeraj Bhaskar
Dheeraj Bhaskar

Reputation: 19039

Option 1:

Paint p = new Paint(Color.RED);
ColorFilter filter = new LightingColorFilter(Color.RED, 1);
p.setColorFilter(filter);

Then draw with that Paint object.

Option 2:

    Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
    float[] colorTransform = {
            0, 1f, 0, 0, 0, 
            0, 0, 0f, 0, 0,
            0, 0, 0, 0f, 0, 
            0, 0, 0, 1f, 0};

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0f); //Remove Colour 
    colorMatrix.set(colorTransform); //Apply Red say

    ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
    Paint paint = new Paint();
    paint.setColorFilter(colorFilter);   

    Display display = getWindowManager().getDefaultDisplay(); 

    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));            


    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, paint);

Upvotes: 1

Raja Asthana
Raja Asthana

Reputation: 2100

You can change the color of the bitmap using ColorMatrix in android.

Visit this post for more information. Example of ColorMatrix here.

Upvotes: 1

Related Questions