【问题标题】:How to create white border around bitmap?如何在位图周围创建白色边框?
【发布时间】:2011-12-28 04:24:01
【问题描述】:

例如,我想要在位图的所有 4 边周围有一个 10 像素的白色边框。我没有将它用于图像视图 我目前正在使用此代码来裁剪图像。我可以知道如何在其中添加白色边框吗?

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

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    我为此写了一个函数:

    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 * 边框大小,然后在其上绘制原始位图,用边框大小偏移它。

    【讨论】:

    • 当图像视图大小高于显示的图像大小并且带有背景和填充的边框的常规方法失败时,此方法有效,因为某些部分比其他部分过满。这个方法很完美。荣誉。
    • 完美的代码。在 drawBitmap() 方法中使用borderSize 需要浮动
    【解决方案2】:

    至于这样做的方法。你让你的位图比你添加的那个大,然后用你想要的背景填充画布。如果您需要添加其他效果,可以查看用于剪切矩形和添加圆角等的画布选项。

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

    【讨论】:

    • 不工作。图像白色边框不会出现在 4 面。只有顶部和底部的边框
    • 你已经有了基本的想法,使用 targetRect 和位图大小来获得你想要的效果。
    【解决方案3】:

    您可以在绘制位图的内容后绘制 4 个矩形。

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

    【讨论】:

      【解决方案4】:

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

      如果这不起作用,请创建一个带有 wrap_content 的 FrameLayout,将其背景设置为白色,将 ImageView 放入其中,并将 ImageView 的边距设置为所需的边框宽度。

      【讨论】:

        【解决方案5】:

        它并不优雅,但你总是可以在它后面画一个矩形,你已经有代码可以做到这一点,任何性能影响都不会被察觉

        【讨论】:

        • 嗨,你能告诉我它是怎么做的吗?我想弄清楚如何画一个白色的矩形背景
        • 你想要一个方形位图周围的边框吗?如果是这样,只需在其后面绘制另一个方形位图
        • 如果您使用的是 Canvas API,它只是 canvas.drawRect。
        【解决方案6】:

        您可以创建宽 20 像素和高 20 像素的 targetRectangle

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

        把背景涂成白色

        【讨论】:

        • 将其用于地图项覆盖需要位图
        【解决方案7】:

        试试这个,它也会为你的画布添加边框

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

        【讨论】:

          【解决方案8】:

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

          实现此目标的更好方法是使用 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() 方法四次使用第二个位图周围每条线的端点坐标绘制四条线,然后绘制源位图在必须返回的第二个位图上。

          【讨论】:

          • 目标位图不一定是正方形,边框要适合位图的形状。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-21
          • 1970-01-01
          • 1970-01-01
          • 2015-06-06
          相关资源
          最近更新 更多