如何在 canvas 中绘制 3 个位图

How to draw 3 bitmaps in canvas

我正在尝试将 3 个图像视图合并到一个我正在使用的位图中 canvas,这里是函数

private Bitmap createSingleImageFromMultipleImages() {

    Bitmap formBitmap = getBitmapFromImageView(formView);
    Bitmap idFrontBitmap = getBitmapFromImageView(idFrontView);
    Bitmap idBackBitmap = getBitmapFromImageView(idBackView);

    Bitmap allBitmaps = null;

    int width, height = 0;

    width = formBitmap.getWidth() + idFrontBitmap.getWidth() + idBackBitmap.getWidth();
    if (formBitmap.getHeight() > idFrontBitmap.getHeight()) {
        height = formBitmap.getHeight();
    } else {
        height = idFrontBitmap.getHeight();
    }

    allBitmaps = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(allBitmaps);

    comboImage.drawBitmap(formBitmap, formBitmap.getWidth(), 0f, null); //this is not drawn
    comboImage.drawBitmap(idFrontBitmap, formBitmap.getWidth(), 0f, null); //this is not drawn
    comboImage.drawBitmap(idBackBitmap, idFrontBitmap.getWidth(), 0f, null); //this is drawn

    return allBitmaps;
}

//这将ImageView转换为位图

public Bitmap getBitmapFromImageView(ImageView imageView) {
    imageView.setDrawingCacheEnabled(true);
    Bitmap scaledBitmap = imageView.getDrawingCache();
    return scaledBitmap;
}

目前只绘制了一张图片,其他部分为空 我已经确认 ImageView 不为空

结果截图。

如评论中所述,您在彼此的顶部绘制位图,因此只有最后一项可见。

您必须正确放置图像,而不是随处绘制。

Canvas 有多种方法可以实现这一点,一种可能性是像您一样使用 drawBitmap(bitmap, left, top, paint),但您应该使用不同的偏移值。

// first, x = 0
comboImage.drawBitmap(formBitmap, 0f, 0f, null);
// second, offset by first width
comboImage.drawBitmap(idFrontBitmap, formBitmap.getWidth(), 0f, null);
// last, offset by first and second width
comboImage.drawBitmap(idBackBitmap, formBitmap.getWidth() + idFrontBitmap.getWidth(), 0f, null);

这应该有效。