2011-11-13 84 views
10

例如,我想在位圖的所有4邊都有一個10像素的白色邊框。我不使用它的imageview 我目前使用此代碼來裁剪圖像。我可以知道我可以在其中添加白色邊框嗎?如何在位圖周圍創建白色邊框?

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { 
    int sourceWidth = source.getWidth(); 
    int sourceHeight = source.getHeight(); 

    // Compute the scaling factors to fit the new height and width, respectively. 
    // To cover the final image, the final scaling will be the bigger 
    // of these two. 
    float xScale = (float) newWidth/sourceWidth; 
    float yScale = (float) newHeight/sourceHeight; 
    float scale = Math.max(xScale, yScale); 

    // Now get the size of the source bitmap when scaled 
    float scaledWidth = scale * sourceWidth; 
    float scaledHeight = scale * sourceHeight; 

    // Let's find out the upper left coordinates if the scaled bitmap 
    // should be centered in the new size give by the parameters 
    float left = (newWidth - scaledWidth)/2; 
    float top = (newHeight - scaledHeight)/2; 

    // The target rectangle for the new, scaled version of the source bitmap will now 
    // be 
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); 

    // Finally, we create a new bitmap of the specified size and draw our new, 
    // scaled bitmap onto it. 
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); 
    Canvas canvas = new Canvas(dest); 
    canvas.drawBitmap(source, null, targetRect, null); 

    return dest; 
} 

回答

13

至於這樣做的方式。你的位圖比你添加的位圖大,然後用你想要的背景填充畫布。如果您需要添加其他效果,則可以查看畫布選項以剪切矩形和添加圓角等。

RectF targetRect = new RectF(left+10, top+10, left + scaledWidth, top + scaledHeight); 
Bitmap dest = Bitmap.createBitmap(newWidth+20, newHeight+20, source.getConfig()); 
Canvas canvas = new Canvas(dest); 
canvas.drawColor(Color.WHITE); 
canvas.drawBitmap(source, null, targetRect, null); 
+0

無法正常工作。圖像白色邊框不會出現在4側。只有頂部和底部的邊框 – ericlee

+2

好吧,你有基本的想法玩targetRect和位圖大小來獲得你想要的效果。 – caguilar187

0

一個超級簡單的方法是將ImageView背景設置爲白色並添加填充值。

如果這不起作用,請創建一個帶有w/h wrap_content的FrameLayout,將其背景設置爲白色,將ImageView放入此處,並將ImageView的邊距設置爲所需的邊框寬度。

+1

位圖,不使用的ImageView ... – ericlee

0

它不是優雅,但你總是可以只畫背後的矩形,你已經有代碼來做到這一點,任何業績的影響將是無法察覺

+0

嗨,你能告訴我它怎麼正在做?我試圖想像如何繪製一個白色的重彩圖像bg – ericlee

+0

是否你想要一個方形位圖的邊框?如果是這樣,在其後面繪製另一個正方形位圖 – Chris

+0

可以告訴我如何? – ericlee

0

您可以創建targetRectangle 20像素寬和20像素高

RectF targetRect = new RectF(left, top, left + scaledWidth + 20, top + scaledHeight + 20); 

,並繪製背景白色

+0

使用它的地圖itemoverlay需要位圖 – ericlee

1

你可以在繪製你的位圖的東西后繪製4個矩形。

point 0,0,3,sizey 
point 0,0,sizex,3 
point 0,sizey-3,sizex,sizey 
point sizex-3,0,sizex,sizey 
50

我寫了一個函數如下:

private Bitmap addWhiteBorder(Bitmap bmp, int borderSize) { 
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig()); 
    Canvas canvas = new Canvas(bmpWithBorder); 
    canvas.drawColor(Color.WHITE); 
    canvas.drawBitmap(bmp, borderSize, borderSize, null); 
    return bmpWithBorder; 
} 

基本上它創建一個新的位圖增加2 * bordersize每個尺寸,然後繪製原始位在它與bordersize抵消它。

+2

這適用於圖像視圖尺寸高於顯示圖像尺寸的情況,並且具有背景和填充的邊框的常規方法會失敗,因爲某些部分的填充已超過其他部分。這種方法是完美的。榮譽。 –

0

試試這也將增加邊框畫布

canvas.drawLine(0, 0, canvas.getWidth(), 0, paint2); 
     canvas.drawLine(0, 0, 0, canvas.getHeight(), paint2); 
     canvas.drawLine(0, canvas.getHeight(), canvas.getWidth(), 
       canvas.getHeight(), paint2); 
     canvas.drawLine(canvas.getWidth(), 0, canvas.getWidth(), 
       canvas.getHeight(), paint2); 
相關問題