如何在位图周围创建白色边框?

时间:2011-11-13 19:34:02

标签: android bitmap

例如,我希望在位图的所有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;
}

8 个答案:

答案 0 :(得分:60)

我为此写了一个函数:

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

基本上它会创建一个新的Bitmap,为每个维度添加2 * bordersize,然后在其上绘制原始的Bitmap,用borderize抵消它。

答案 1 :(得分: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);

答案 2 :(得分:1)

绘制位图的东西后,可以绘制4个矩形。

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

答案 3 :(得分:0)

一种非常简单的方法是将ImageView背景设置为白色并添加填充值。

如果这不起作用,请使用w / h的wrap_content创建一个FrameLayout,将其背景设置为白色,将ImageView放在那里,并将ImageView的边距设置为所需的边框宽度。

答案 4 :(得分:0)

它不优雅,但你总是可以在它后面画一个矩形,你已经有了这样做的代码,任何性能影响都是不明显的

答案 5 :(得分:0)

您可以创建更宽和更高20px的targetRectangle 20px

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

并将背景涂成白色

答案 6 :(得分: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);

答案 7 :(得分:0)

可接受的答案很好,但是在位图包含透明背景的情况下,它会在源位图的整个背景上填充白色像素。因此它不适用于所有情况。

达到此目标的更好方法是使用Canvas#drawLine方法,例如以下代码:

    Bitmap drawBorder(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, source.getConfig());
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setStrokeWidth(50);
    paint.setColor(Color.WHITE);

    canvas.drawLine(0, 0, width, 0, paint);
    canvas.drawLine(width, 0, width, height, paint);
    canvas.drawLine(width, height, 0, height, paint);
    canvas.drawLine(0, height, 0, 0, paint);
    canvas.drawBitmap(source, 0, 0, null);

    return bitmap;
        }

这样,我们首先使用源位图的宽度,高度和配置创建第二个位图,并使用drawline()方法四次使用第二位图周围每条线的端点坐标绘制四条线,然后绘制源位图在必须返回的第二个位图上。