【问题标题】:How to add stroke/border to transparent png image in android?如何在android中为透明png图像添加描边/边框?
【发布时间】:2019-04-23 17:05:54
【问题描述】:

我有透明图像,如形状、字母,我从图库中获取,所以我需要给它们黑色的笔触/轮廓,我可以设置边框,但它设置为整个位图,如左、右、上和下。

我们可以用 Photoshop 做的同样的事情是给图像添加外部描边,但我想在 android 中实现。

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

原图

我想要这样 -->

这在android中可行吗?

【问题讨论】:

    标签: java android bitmap png android-canvas


    【解决方案1】:

    我有一个这样的临时解决方案

    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 使用描边颜色覆盖原始位图位置。

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

    【讨论】:

      猜你喜欢
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多