android - 以编程方式将边框/描边设置为矢量可绘制对象

标签 android vector-graphics stroke android-vectordrawable

我可以通过编程方式更改矢量可绘制对象的颜色,但我想将描边应用于矢量可绘制对象。我需要一种在运行时更改矢量可绘制笔划的方法:

enter image description here

以前我使用过这种方法,但在我的案例中失败了。

我将 Vector drawable 转换为位图,然后使用此函数应用边框,但它全部填充为黑色,未应用描边。

  private static Bitmap getBitmap(VectorDrawable vectorDrawable)
    {
        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
        vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        vectorDrawable.draw(canvas);
        return bitmap;
    }
    private static Bitmap getBitmap(Context context, int drawableId)
    {

        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
        if (drawable instanceof BitmapDrawable)
        {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        else if (drawable instanceof VectorDrawable)
        {
            return getBitmap((VectorDrawable) drawable);
        }
        else
        {
            throw new IllegalArgumentException("unsupported drawable type");
        }
    }
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.BLACK);
        canvas.drawBitmap(bmp, borderSize, borderSize, null);
        return bmpWithBorder;
    }

最佳答案

假设您在 vectordrawable.xml 中定义了 VectorDrawable

<vector
    android:width="100dp"
    android:height="100dp"
    android:viewportWidth="100"
    android:viewportHeight="100">
    <path
        android:name="headset"
        android:strokeColor="#FF000000"
        android:strokeWidth="0"
        ...
        android:pathData="..." />
</vector>

然后你可以定义一个AnimatedVectorDrawable来改变strokeWidth

<?xml version="1.0" encoding="utf-8"?>
<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/vectordrawable">
    <target
        android:animation="@animator/change_stroke_width"
        android:name="headset" />

</animated-vector>

最后在change_stroke_width.xml中定义改变strokeWidth的动画:

<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <objectAnimator
        android:duration="100"
        android:propertyName="strokeWidth"
        android:valueFrom="0"
        android:valueTo="10" />
</set>

关于android - 以编程方式将边框/描边设置为矢量可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674459/

相关文章:

android - 进程 'command ' ./node_modules/expokit/detach-scripts/run-exp.sh'' 以非零退出值 127 完成

android - 您好,我正在尝试为Flutter设置我的Android设备。 Gradle 错误

android - 在牛轧糖上多次接收 FCM 通知

html5-canvas - HTML5 Canvas 笔触未消除锯齿

android - AChartEngine XYMultipleSeriesRenderer 参数?

svg - 从矩形反转 SVG 图像/剪辑路径

pdf - 如何调整 pdf 中位图图像的大小/重新采样并保持矢量叠加

javascript - Javascript 中的矢量图形?

javascript - 使用 JS 在 View 中绘制圆形描边动画

text - 是否可以在 ffmpeg 中为文本添加笔画?