如何在安卓系统中连接/合并两个位图并使其成为一个图像[英] How to concate/combine two bitmaps and make them one image in android

本文是小编为大家收集整理的关于如何在安卓系统中连接/合并两个位图并使其成为一个图像的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

谁能告诉我如何将两个位图图像组合到Android中?

中?

推荐答案

您可以尝试油漆. 这里有一个简短的例子:

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

    }
 }

其他推荐答案

我的方法是使用Canvas和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);

其他推荐答案

我会将Bitmap s添加到ImageView s,将它们放入LinearLayout中,然后使用LinearLayout的屏幕截图,如果您试图将图像置为getDrawingCache()覆盖它们.

本文地址:https://www.itbaoku.cn/post/102484.html

问题描述

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

推荐答案

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

    }
 }

其他推荐答案

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

其他推荐答案

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.