如何在android中为透明png图像添加笔画/边框?

问题描述 投票:2回答:1

我有透明图像,如形状,字母,我从画廊取,所以我需要给它们描边/轮廓与黑色,我可以设置边框,但它设置为整个位图,如左,右,上,下。

我们可以用photoshop做同样的事情是给图像outerstroke但我想在android中实现它。

我试过thisthis这是给边框,但我想做的是像下面的示例图像

原始图像without stroke

我想要这样 - > With stroke

在Android中这可能吗?

java android bitmap png android-canvas
1个回答
0
投票

我有这样的临时解决方案

int strokeWidth = 8;
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flower_icon);
Bitmap newStrokedBitmap = Bitmap.createBitmap(originalBitmap.getWidth() + 2 * strokeWidth, originalBitmap.getHeight() + 2 * strokeWidth, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newStrokedBitmap);
float scaleX = (originalBitmap.getWidth() + 2.0f * strokeWidth) / originalBitmap.getWidth();
float scaleY = (originalBitmap.getHeight() + 2.0f * strokeWidth) / originalBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY);
canvas.drawBitmap(originalBitmap, matrix, null);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_ATOP); //Color.WHITE is stroke color
canvas.drawBitmap(originalBitmap, strokeWidth, strokeWidth, null);

首先在左,右,底部和顶部创建一个具有笔触大小的新位图。

其次是一个小比例的位图,并在新创建的位图画布上绘制缩放的位图。

使用PorterDuff模式绘制颜色(笔触颜色)SRC_ATOP使用笔触颜色覆盖原始位图位置。

最后在新创建的位图画布上绘制原始位图。

© www.soinside.com 2019 - 2024. All rights reserved.