我有透明图像,如形状、字母,我是从图库中获取的,所以我需要用黑色给它们描边/轮廓,我可以设置边框,但它被设置为整个位图,如左、右、上和下。
Same thing we can do with photoshop is giving outerstroke to image but I want to achieve that in android.
我试过了 this和 this它给边界,但我想做的是像下面 示例图片
这在安卓中可行吗?
最佳答案
我有这样的临时解决方案
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 = newStrokedBitmap.getWidth() / originalBitmap.getWidth();
float scaleY = newStrokedBitmap.getHeight() / 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);
首先创建一个新的位图,在左、右、下和上设置笔画大小。
其次,稍微缩放位图并在新创建的位图 Canvas 上绘制缩放位图。
使用 PorterDuff 模式绘制颜色(您的描边颜色)SRC_ATOP 用描边颜色覆盖原始位图位置。
最后在新创建的位图 Canvas 上绘制原始位图。
关于java - 如何在android中为透明png图像添加描边/边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53406813/