Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static Bitmap createBitmapWithAlphaMask(Bitmap bmpSource, Bitmap bmpMask) {
        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);
    }
}