drawable To Grayscale Bitmap - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

drawable To Grayscale 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.PixelFormat;

import android.graphics.drawable.Drawable;

public class Main {

    public static Bitmap drawableToGrayscaleBitmap(Drawable drawable) {
        drawable.getBounds();/*www . j  a va2  s .c  om*/
        int width = drawable.getIntrinsicWidth(); 
        int height = drawable.getIntrinsicHeight();
        Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
                : Config.RGB_565; 
        Bitmap bitmap = Bitmap.createBitmap(width, height, config); 
        Canvas canvas = new Canvas(bitmap); 
        drawable.setBounds(0, 0, width, height);

        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        drawable.setColorFilter(f);
        drawable.draw(canvas);
        return bitmap;
    }
}

Related Tutorials