簡體   English   中英

如何在畫布上繪制3位圖

[英]How to draw 3 bitmaps in canvas

我正在嘗試將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;
}

//這會將ImageView轉換為位圖

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

目前只繪制一個圖像,其他部分為空,我已經確認ImageView不為null

結果的屏幕截圖。

在此處輸入圖片說明

如評論中所述,您將位圖繪制在彼此的頂部,因此只有最后一項可見。

您將必須正確放置圖像,而不僅僅是將它們繪制在任何地方。

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

這應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM