create Bitmap With Alpha Mask - Android Graphics

Android examples for Graphics:Bitmap Color

Description

create Bitmap With Alpha Mask

Demo Code


//package com.book2s;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

public class Main {
    public static Bitmap createBitmapWithAlphaMask(Bitmap bmpSource,
            Bitmap bmpMask) {//from w  ww. jav a2 s.  c  o  m
        int width = bmpSource.getWidth();
        int height = bmpSource.getHeight();
        int size = width * height;

        if (width != bmpMask.getWidth() || height != bmpMask.getHeight())
            bmpMask = resize(bmpMask, width, height);

        int[] result = new int[size];
        int[] mask = new int[size];
        bmpSource.getPixels(result, 0, width, 0, 0, width, height);
        bmpMask.getPixels(mask, 0, width, 0, 0, width, height);

        int alphamask = 0xff000000;
        int colormask = 0x00ffffff;
        for (int i = 0; i < size; i++) {
            result[i] = (mask[i] & alphamask) | (result[i] & colormask);
        }

        // ensuring the bitmap is mutable
        Bitmap bmpResult = Bitmap.createBitmap(width, height,
                Config.ARGB_8888);
        bmpResult.setPixels(result, 0, width, 0, 0, width, height);

        return bmpResult;
    }

    public static Bitmap resize(Bitmap src, int w2, int h2) {
        int w1 = src.getWidth();
        int h1 = src.getHeight();
        int[] pxSource = new int[w1 * h1];
        int[] pxResult = new int[w2 * h2];

        src.getPixels(pxSource, 0, w1, 0, 0, w1, h1);
        double x_ratio = w1 / (double) w2;
        double y_ratio = h1 / (double) h2;
        double px, py;
        for (int i = 0; i < h2; i++) {
            for (int j = 0; j < w2; j++) {
                px = Math.floor(j * x_ratio);
                py = Math.floor(i * y_ratio);
                pxResult[(i * w2) + j] = pxSource[(int) ((py * w1) + px)];
            }
        }

        return Bitmap.createBitmap(pxResult, w2, h2, Config.ARGB_8888);
    }
}

Related Tutorials