covert Bitmap To Blue - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

covert Bitmap To Blue

Demo Code


//package com.java2s;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;

public class Main {
    public static Bitmap covertToBlue(Context context, int resImage) {
        return changeImageColor(context, resImage, 21f, 101f, 192f, 1);
    }//ww  w .j  a  va 2s  .  c  o  m

    public static Bitmap changeImageColor(Context context, int resImage,
            float R, float G, float B, float transparency) {
        final ColorMatrix colorMatrix = new ColorMatrix();

        float progressR = R / 128f;
        float progressG = G / 128f;
        float progressB = B / 128f;
        float progressA = transparency;

        float[] color = new float[] { progressR, 0, 0, 0, 0, 0, progressG,
                0, 0, 0, 0, 0, progressB, 0, 0, 0, 0, 0, progressA, 0 };

        colorMatrix.set(color);

        Bitmap srcBitmap = BitmapFactory.decodeResource(
                context.getResources(), resImage);
        //
        Bitmap newBitmap = Bitmap.createBitmap(srcBitmap.getWidth(),
                srcBitmap.getHeight(), srcBitmap.getConfig());

        //
        Canvas canvas = new Canvas(newBitmap);
        //
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //?
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        paint.setAntiAlias(true);
        //?
        canvas.drawBitmap(srcBitmap, new Matrix(), paint);
        return newBitmap;

    }
}

Related Tutorials