overlay two Bitmap Images - Android Graphics

Android examples for Graphics:Bitmap Combine

Description

overlay two Bitmap Images

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Color;

import android.graphics.Bitmap.Config;

public class Main {
    private static Bitmap overlay(Bitmap src, Bitmap dst) {
        final int width = src.getWidth();
        final int height = src.getHeight();
        final int size = width * height;

        int[] pixSrc = new int[size];
        src.getPixels(pixSrc, 0, width, 0, 0, width, height);

        int[] pixDst = new int[size];
        dst.getPixels(pixDst, 0, width, 0, 0, width, height);

        int[] pixOverlay = new int[size];

        for (int y = 0; y < height; y++) {
            // from left to right
            for (int x = 0; x < width; x++) {
                int index = y * width + x;
                // red
                int rSrc = (pixSrc[index] >> 16) & 0xff;
                int rDst = (pixDst[index] >> 16) & 0xff;

                int rBlended = blendChanelOverlay(rSrc, rDst);
                // green
                int gSrc = (pixSrc[index] >> 8) & 0xff;
                int gDst = (pixDst[index] >> 8) & 0xff;

                int gBlended = blendChanelOverlay(gSrc, gDst);
                // blue
                int bSrc = (pixSrc[index]) & 0xff;
                int bDst = (pixDst[index]) & 0xff;

                int bBlended = blendChanelOverlay(bSrc, bDst);

                pixOverlay[index] = Color.rgb(rBlended, gBlended, bBlended);

            }/* w w  w . j  av  a2 s . c  om*/
        }
        Bitmap overlay = Bitmap.createBitmap(pixOverlay, width, height,
                Config.ARGB_4444);
        return overlay;
    }

    static int blendChanelOverlay(int A, int B) {
        return (((B < 128) ? (2 * A * B / 255) : (255 - 2 * (255 - A)
                * (255 - B) / 255)));
    }
}

Related Tutorials