Question

I have two bitmaps, namely bm1 and bm2, and I'd like to create (as quick as possible) another bitmap which is a fading mix between bm1 and bm2 where bm1 is weighted with weight and bm2 with 1-weight.

My current implementation is as follows:

private Bitmap Fade(Bitmap bm1, Bitmap bm2, double weight)
{
    int width = bm1.getWidth();
    int height = bm1.getHeight();

    if (width != bm2.getWidth() || height != bm2.getHeight())
        return null;

    Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
        {
            int pix_s = bm1.getPixel(x, y);
            int pix_d = bm2.getPixel(x, y);

            int r_s =  (pix_s >> 16) & 0xFF;
            int g_s = (pix_s >> 8) & 0xFF;
            int b_s = pix_s & 0xFF;

            int r_d =  (pix_d >> 16) & 0xFF;
            int g_d = (pix_d >> 8) & 0xFF;
            int b_d = pix_d & 0xFF;

            int r = (int)((1-weight) * r_s + weight * r_d);
            int g = (int)((1-weight) * g_s + weight * g_d);
            int b = (int)((1-weight) * b_s + weight * b_d);
            int pix = 0xff000000 | (r << 16) | (g << 8) | b;
            bm.setPixel(x, y, pix);
        }
    return bm;  
}

As you can see, it simply sets the RGB components of each pixel of the generated image with an interpolation between the corresponding RGB components of pixels in bm1 and bm2.

However, this function is very slow as it scans all the pixels of the two input bitmaps.
Is there a more efficient way to do the same?
For instance, by someway acting onto the transparency attributes?

Was it helpful?

Solution

Here it shows you how to change the opacity of your bitmap, it changes the alpha channel of the RGB color. and Here it shows you how to overlay in a canvas two bitmaps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top