Android 如何在位图周围创建白色边框?
例如,我想要一个10像素的白色边框环绕位图的所有4个边。我没有将其用于imageview 我目前正在使用此代码裁剪图像。我可以知道如何在上面加上白色边框吗Android 如何在位图周围创建白色边框?,android,bitmap,Android,Bitmap,例如,我想要一个10像素的白色边框环绕位图的所有4个边。我没有将其用于imageview 我目前正在使用此代码裁剪图像。我可以知道如何在上面加上白色边框吗 public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { int sourceWidth = source.getWidth(); int sourceHeight = source.getHeight(); // Comp
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;
}
一种非常简单的方法是将ImageView背景设置为白色并添加一个填充值
如果这不起作用,请创建一个包含w/h wrap_内容的FrameLayout,将其背景设置为白色,将ImageView放入其中,并将ImageView的边距设置为所需的边框宽度。这并不优雅,但您可以始终在其后面绘制一个矩形,您已经有了执行此操作的代码,任何性能影响都将是不可察觉的您可以创建更宽20px、更高20px的targetRectangle
RectF targetRect = new RectF(left, top, left + scaledWidth + 20, top + scaledHeight + 20);
然后将背景漆成白色作为一种方法。使位图比添加到其中的位图大,然后用所需的背景填充画布。如果您需要添加其他效果,可以查看画布选项,以剪裁矩形和添加圆角等
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个矩形
point 0,0,3,sizey
point 0,0,sizex,3
point 0,sizey-3,sizex,sizey
point sizex-3,0,sizex,sizey
我为此编写了一个函数:
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进行偏移。尝试此操作,它还会将边框添加到画布中
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);
接受的答案很好,但在位图包含透明背景的情况下,它会用白色像素填充源位图的整个背景。因此,它并不适用于所有情况 实现此目标的更好方法是使用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()mathod四次,使用第二个位图周围每行端点的坐标绘制四条线,然后在必须返回的第二个位图上绘制源位图。将其用于map itemoverlay需要bitmaphi,你能告诉我怎么做吗?我在试着弄清楚如何画一个白色的图像,你是想在一个正方形位图周围画一个边框吗?如果是这样,只需在其后面绘制另一个方形位图。如果您使用的是canvas API,则只需canvas.drawRect。不工作。图像白色边框不显示在4侧。只有顶部和底部的边框您有基本的想法,可以使用targetRect和位图大小来获得所需的效果。当imageview大小高于显示的图像大小时,此方法有效,并且带有背景和填充的边框的常规方法失败,因为某些部分填充过多,而其他部分填充过多。这种方法是完美的。荣誉,完美的代码。在drawBitmap()方法中使用的borderSize需要是浮点值。目标位图不一定是正方形,边框应该适合位图的形状。