merge android bitmaps - Android Graphics

Android examples for Graphics:Bitmap Combine

Description

merge android bitmaps

Demo Code

/**/*from  w  w w  .  j  a v  a 2  s .  co  m*/
 * This file is part of Owlet.
 * 
 * Owlet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * Owlet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with Owlet.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Main {
    /**
     * The stupid merge for android bitmaps
     * How use:
     *    Drawable dr = getResources().getDrawable(R.drawable.cherry);
       Bitmap bitmap = BitmapHelper.mergeBitmaps(
          ((BitmapDrawable)dr).getBitmap(), 
          ((BitmapDrawable)dr).getBitmap(), 
          ((BitmapDrawable)dr).getBitmap());
       Drawable result = new BitmapDrawable(getResources(), bitmap);
     *
     * @param bitmaps
     *            the list of bitmaps
     * @return merged bitmap
     */
    public static Bitmap mergeBitmaps(Bitmap... bitmaps) {
        Bitmap cs = null;

        int width, height = 0;
        int size = bitmaps.length;
        if (size == 1) {
            return bitmaps[0];
        } else if (size == 2) {
            final Bitmap first = Bitmap.createScaledBitmap(bitmaps[0],
                    bitmaps[0].getWidth() / 2, bitmaps[0].getHeight() / 2,
                    true);
            final Bitmap second = Bitmap.createScaledBitmap(bitmaps[1],
                    bitmaps[1].getWidth() / 2, bitmaps[1].getHeight() / 2,
                    true);
            width = bitmaps[0].getWidth();
            height = bitmaps[0].getHeight();
            cs = Bitmap
                    .createBitmap(width, height, Bitmap.Config.ARGB_8888);

            Canvas twiseImage = new Canvas(cs);

            twiseImage
                    .drawBitmap(first, width - first.getWidth(), 0f, null);
            twiseImage.drawBitmap(second, 0f, height - second.getHeight(),
                    null);
        } else if (size == 3) {
            final Bitmap first = Bitmap.createScaledBitmap(bitmaps[0],
                    bitmaps[0].getWidth() / 2, bitmaps[0].getHeight() / 2,
                    true);
            final Bitmap second = Bitmap.createScaledBitmap(bitmaps[1],
                    bitmaps[1].getWidth() / 2, bitmaps[1].getHeight() / 2,
                    true);
            final Bitmap third = Bitmap.createScaledBitmap(bitmaps[2],
                    bitmaps[2].getWidth() / 2, bitmaps[2].getHeight() / 2,
                    true);
            width = bitmaps[0].getWidth();
            height = bitmaps[0].getHeight();
            cs = Bitmap
                    .createBitmap(width, height, Bitmap.Config.ARGB_8888);

            Canvas twiseImage = new Canvas(cs);

            twiseImage.drawBitmap(first, width - first.getWidth(), height
                    - second.getHeight(), null);
            twiseImage.drawBitmap(second, 0f, height - second.getHeight(),
                    null);
            twiseImage.drawBitmap(third, (width - second.getWidth()) / 2,
                    0f, null);
        } else if (size == 4) {
            final Bitmap first = Bitmap.createScaledBitmap(bitmaps[0],
                    bitmaps[0].getWidth() / 2, bitmaps[0].getHeight() / 2,
                    true);
            final Bitmap second = Bitmap.createScaledBitmap(bitmaps[1],
                    bitmaps[1].getWidth() / 2, bitmaps[1].getHeight() / 2,
                    true);
            final Bitmap third = Bitmap.createScaledBitmap(bitmaps[2],
                    bitmaps[2].getWidth() / 2, bitmaps[2].getHeight() / 2,
                    true);
            final Bitmap fourth = Bitmap.createScaledBitmap(bitmaps[2],
                    bitmaps[2].getWidth() / 2, bitmaps[2].getHeight() / 2,
                    true);
            width = bitmaps[0].getWidth();
            height = bitmaps[0].getHeight();
            cs = Bitmap
                    .createBitmap(width, height, Bitmap.Config.ARGB_8888);

            Canvas twiseImage = new Canvas(cs);

            twiseImage.drawBitmap(first, width - first.getWidth(), height
                    - second.getHeight(), null);
            twiseImage.drawBitmap(second, 0f, height - second.getHeight(),
                    null);
            twiseImage.drawBitmap(third, 0f, 0f, null);
            twiseImage.drawBitmap(fourth, (width - second.getWidth()), 0f,
                    null);
        }
        return cs;
    }
}

Related Tutorials