views:

73

answers:

2

Can anyone tell me how to combine or concatenate two bitmap images into one in android?

+1  A: 

You can try with Paint. Here a short example:

package com.cyrilmottier.android.masking; 
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new IconView(this));
}

private class IconView extends View {

    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Bitmap mIcon;
    private Bitmap mIconGlossy;
    private Bitmap mIconMask;

    public IconView(Context context) {
        super(context);

        // Prepares the paint that will be used to draw our icon mask. Using
        // PorterDuff.Mode.DST_IN means the image that will be drawn will
        // mask the already drawn image.
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        // Let's retrieve all icon pieces as Bitmaps.
        final Resources res = context.getResources();
        mIcon = BitmapFactory.decodeResource(res, R.drawable.icon_metromap_fake);
        mIconGlossy = BitmapFactory.decodeResource(res, R.drawable.icon_glossy);
        mIconMask = BitmapFactory.decodeResource(res, R.drawable.icon_mask);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();

        // Translate the canvas in order to draw the icon in the center of
        // the view
        canvas.translate((getWidth() - mIcon.getWidth()) >> 1, (getHeight() - mIcon.getHeight()) >> 1);

        // We're now ready to drawn our iPhone-like icon :)
        canvas.drawBitmap(mIcon, 0, 0, null);
        canvas.drawBitmap(mIconGlossy, 0, 0, null);
        canvas.drawBitmap(mIconMask, 0, 0, mPaint);

        canvas.restore();
    }

}

}

carlovv
A: 

My approach would be to use a Canvas and a Path.


Canvas canvas = new Canvas(bmOverlay);  

//Add the background
canvas.drawBitmap(bitMapOriginal, new Matrix(), null);  

//Build the path and add content.
Path path = new Path();
//path.lineTo(   ...Add a few points to the path...

//Do the masking
canvas.clipPath(path, Op.REPLACE);  

//Add the overlay
canvas.drawBitmap(bitMapOverlay, new Matrix(), null);  

//Use the modified bitmap
functionUsingBitmap(bmOverlay);

rndStr