如何创建位图周围白色边框? [英] How to create white border around bitmap?

查看:128
本文介绍了如何创建位图周围白色边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我想10pixel周围的位图的全部4个侧面有白色边框。我不使用它的ImageView 我目前使用这种code裁剪图像。我想知道我怎么可以添加一个白色边框成吗?

 公共位图scaleCenterCrop(位图​​源,诠释newHeight,诠释newWidth){
    INT sourceWidth = source.getWidth();
    INT sourceHeight = source.getHeight();

    //计算缩放因子,以适应新的高度和宽度,分别。
    //为了覆盖最终图像,最终的缩放将是更大的
    //这两个。
    浮xScale等=(浮点)newWidth / sourceWidth;
    浮动yScale =(浮点)newHeight / sourceHeight;
    浮规模= Math.max(xScale等,yScale);

    //现在得到的源位图的大小缩放时
    浮scaledWidth =规模* sourceWidth;
    浮scaledHeight =规模* sourceHeight;

    //让我们看看左上角的坐标,如果缩放位图
    //应该由这些参数给新的大小为中心
    左浮动=(newWidth  -  scaledWidth)/ 2;
    浮顶=(newHeight  -  scaledHeight)/ 2;

    //为源位图新,图像的缩放目标矩形将现
    // 是
    RectF targetRect =新RectF(左,上,左+ scaledWidth,顶部+ scaledHeight);

    //最后,我们创建指定大小的新位图,并得出我们新的,
    //位图缩放到它。
    位图DEST = Bitmap.createBitmap(newWidth,newHeight,source.getConfig());
    帆布油画=新的Canvas(DEST);
    canvas.drawBitmap(源,空,targetRect,NULL);

    返回DEST;
}
 

解决方案

至于这样做的方式。你让你的位图比一个你增加它,然后填写你想要的背景画布大。如果您需要添加其他的效果,你可以看看到画布上选择剪裁的矩形,并添加圆角等。

  RectF targetRect =新RectF(左+ 10,前+ 10,左+ scaledWidth,顶部+ scaledHeight);
位图DEST = Bitmap.createBitmap(newWidth + 20,newHeight + 20,source.getConfig());
帆布油画=新的Canvas(DEST);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(源,空,targetRect,NULL);
 

For example I want a white border of 10pixel around all 4 side of the bitmap. I am not using it for imageview I am currently using this code to crop image. May I know how I could add a white border into it?

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

解决方案

As for a way of doing this. You make your bitmap bigger than the one your adding to it and then fill the canvas with the background you want. If you need to add other effects you can look into the canvas options for clipping the rect and adding rounded corners and such.

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

这篇关于如何创建位图周围白色边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆