如何连接/组合两个位图,并在android中使它们成为一个图像

时间:2010-09-17 15:39:40

标签: android image graphics

有谁能告诉我如何在android中将两个位图图像组合或连接成一个?

3 个答案:

答案 0 :(得分:3)

您可以尝试使用Paint。 这是一个简短的例子:

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

    }
 }

答案 1 :(得分:1)

我的方法是使用CanvasPath


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

答案 2 :(得分:1)

我会将Bitmap添加到ImageView s,将它们放在LinearLayout中,然后使用LinearLayout截取getDrawingCache()试图连接图像而不是覆盖它们。