Convert grayscale 32 To Alpha 32 - Android Graphics

Android examples for Graphics:Bitmap Color

Description

Convert grayscale 32 To Alpha 32

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 grayscale32ToAlpha32(Bitmap grayscaleBitmap) {
        Bitmap alphaBitmap = Bitmap.createBitmap(
                grayscaleBitmap.getWidth(), grayscaleBitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        float[] matrix = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1, 0, 0, 0, 0 };
        Paint grayToAlpha = new Paint();
        grayToAlpha.setColorFilter(new ColorMatrixColorFilter(
                new ColorMatrix(matrix)));
        Canvas alphaCanvas = new Canvas(alphaBitmap);
        alphaCanvas.setDensity(Bitmap.DENSITY_NONE);
        alphaCanvas.drawBitmap(grayscaleBitmap, 0, 0, grayToAlpha);
        return alphaBitmap;
    }//  w  ww.  jav  a 2s.  com
}

Related Tutorials