make Bitmap Image Transparent - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

make Bitmap Image Transparent

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;
import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.PorterDuffXfermode;

import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class Main {
    public static Bitmap makeTransparent(Bitmap alphaGray, Bitmap src) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] alphaPix = new int[width * height];
        alphaGray.getPixels(alphaPix, 0, width, 0, 0, width, height);
        int count = width * height;
        for (int i = 0; i < count; ++i) {
            alphaPix[i] = alphaPix[i] << 8;
        }//from  w  w w  .  java 2  s .co m
        src.setPixels(alphaPix, 0, width, 0, 0, width, height);

        Paint alphaP = new Paint();
        alphaP.setAntiAlias(true);
        alphaP.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        Bitmap result = Bitmap
                .createBitmap(width, height, Config.ARGB_4444);
        result.eraseColor(Color.BLACK);
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(src, 0, 0, alphaP);
        src.recycle();
        return result;
    }
}

Related Tutorials