How to concate/combine two bitmaps and make them o

2020-07-26 11:32发布

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

3条回答
戒情不戒烟
2楼-- · 2020-07-26 12:07

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);

查看更多
▲ chillily
3楼-- · 2020-07-26 12:07

I would add the Bitmaps to ImageViews, put them in a LinearLayout and then take a screenshot of the LinearLayout using getDrawingCache() if you are trying to concatenate the images instead of overlay them.

查看更多
劳资没心,怎么记你
4楼-- · 2020-07-26 12:09

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();
        }

    }
 }
查看更多
登录 后发表回答