get Gray Bitmap - Android Graphics

Android examples for Graphics:Bitmap Color

Description

get Gray Bitmap

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.Canvas;

import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;

import android.graphics.Paint;

public class Main {

    public static Bitmap getGrayBitmap(Bitmap mBitmap) {
        Bitmap mGrayBitmap = Bitmap.createBitmap(mBitmap.getWidth(),
                mBitmap.getHeight(), Config.ARGB_8888);
        Canvas mCanvas = new Canvas(mGrayBitmap);
        Paint mPaint = new Paint();
        ColorMatrix mColorMatrix = new ColorMatrix();
        mColorMatrix.setSaturation(0);//from  w w  w  . jav  a  2 s .  co m
        ColorMatrixColorFilter mColorFilter = new ColorMatrixColorFilter(
                mColorMatrix);
        mPaint.setColorFilter(mColorFilter);
        mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);

        return mGrayBitmap;
    }
}

Related Tutorials