Create bitmap Filter - Android Graphics

Android examples for Graphics:Bitmap Create

Description

Create bitmap Filter

Demo Code


//package com.java2s;

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

public class Main {
    public static Bitmap bitmapFilter(Bitmap original, float[] matrix) {
        return bitmapFilter(original, new ColorMatrixColorFilter(matrix));
    }//from  ww  w .  j a  va 2s  . c o  m

    public static Bitmap bitmapFilter(Bitmap original,
            ColorMatrixColorFilter cmcf) {
        Bitmap bmpGrayscale = Bitmap.createBitmap(original.getWidth(),
                original.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        if (cmcf == null) {
            ColorMatrix cm = new ColorMatrix();
            cm.setSaturation(0);
            cmcf = new ColorMatrixColorFilter(cm);
        }
        paint.setColorFilter(cmcf);
        c.drawBitmap(original, 0, 0, paint);
        return bmpGrayscale;
    }
}

Related Tutorials