如何在画布

时间:2016-02-27 10:23:18

标签: android android-canvas android-bitmap

我正在尝试将3个imageViews合并到一个位图我正在使用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不为空

结果的屏幕截图。

enter image description here

1 个答案:

答案 0 :(得分:1)

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

您必须正确放置图像,而不是将它们绘制到任何地方。

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

这应该有用。