简体   繁体   English

如何在画布上绘制3位图

[英]How to draw 3 bitmaps in canvas

I am trying to merge 3 imageViews into one bitmap i am using canvas and here is the function 我正在尝试将3个imageViews合并为一个位图,而我使用的是画布,这是函数

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

//this converts ImageView to bitmap //这会将ImageView转换为位图

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

currently only one image is drawn the other parts are empty i have confirmed that the ImageView are not null 目前只绘制一个图像,其他部分为空,我已经确认ImageView不为null

Screenshot of the result. 结果的屏幕截图。

在此处输入图片说明

As mentioned in the comments, you are drawing your bitmaps at the top of each other, hence only the last item is visible. 如评论中所述,您将位图绘制在彼此的顶部,因此只有最后一项可见。

You will have to properly place the images, and not just draw them anywhere. 您将必须正确放置图像,而不仅仅是将它们绘制在任何地方。

Canvas has multiple methods that can achieve this, one possibility is to use drawBitmap(bitmap, left, top, paint) like you do, but you should use different values for the offsets. 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);

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM