Android - 如何绘制描边位图?

发布于 2025-01-13 04:20:06 字数 172 浏览 6 评论 0原文

我有一个如图所示的位图。我想使用 Canvas 为其添加边框。谁能帮助我吗? 我的位图

I have a bitmap like in the picture. I want to add a border for it using Canvas. Can anyone help me?
My bitmap

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[浮城] 2025-01-20 04:20:06

您可以使用 Paint.STYLE.STROKE 绘制边框。您需要进行两次单独的绘图调用。

paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#BAB399")); // set fill color
canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
    sbmp.getWidth() / 2+0.1f, paint);

paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10); // set stroke width
paint.setColor(Color.parseColor("#ffffff")); // set stroke color
canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
    sbmp.getWidth() / 2+0.1f, paint);

You can draw a border using Paint.STYLE.STROKE. You need to do two separate calls to draw.

paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#BAB399")); // set fill color
canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
    sbmp.getWidth() / 2+0.1f, paint);

paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10); // set stroke width
paint.setColor(Color.parseColor("#ffffff")); // set stroke color
canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
    sbmp.getWidth() / 2+0.1f, paint);
玻璃人 2025-01-20 04:20:06

如果图像具有透明背景(png 文件)并且描边大小不太大,则可以仅绘制位图并对其应用 4 次颜色叠加 - 左、上、右和下。

final float stroke = 20F; //stroke size in pixels.
//Load target image as bitmap.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.shap, options);
//A new, clear bitmap with same size.
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//Overlay target bitmap with black color.
PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
paint.setColorFilter(filter);
//Draw target bitmap over new bitmap.
canvas.drawBitmap(bitmap, -stroke, 0F, paint); //left
canvas.drawBitmap(bitmap, 0F, -stroke, paint); //top
canvas.drawBitmap(bitmap, stroke, 0F, paint); //right
canvas.drawBitmap(bitmap, 0F, stroke, paint); //bottom
//Remove overlay.
paint.setColorFilter(null);
//Draw target bitmap.
canvas.drawBitmap(bitmap, 0F, 0F, paint);
//Load new bitmap into ImageView.
ImageView iv = findViewById(R.id.iv);
iv.setImageBitmap(newBitmap);

输入图片此处描述

If the image has a transparent background (png file) and the stroke size is not too large, you can just draw the bitmap with a color overlay applied to it 4 times -- left, top, right and bottom.

final float stroke = 20F; //stroke size in pixels.
//Load target image as bitmap.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.shap, options);
//A new, clear bitmap with same size.
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//Overlay target bitmap with black color.
PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
paint.setColorFilter(filter);
//Draw target bitmap over new bitmap.
canvas.drawBitmap(bitmap, -stroke, 0F, paint); //left
canvas.drawBitmap(bitmap, 0F, -stroke, paint); //top
canvas.drawBitmap(bitmap, stroke, 0F, paint); //right
canvas.drawBitmap(bitmap, 0F, stroke, paint); //bottom
//Remove overlay.
paint.setColorFilter(null);
//Draw target bitmap.
canvas.drawBitmap(bitmap, 0F, 0F, paint);
//Load new bitmap into ImageView.
ImageView iv = findViewById(R.id.iv);
iv.setImageBitmap(newBitmap);

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文