以编程方式将边框/描边设置为矢量图形。

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

enter image description here

以前我使用了这种方法,但在我的情况下失败了。
我将矢量可绘制对象转换为位图,然后使用此函数应用边框,但它会全部填充为黑色,描边没有应用。
  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;
    }
2个回答

0

看看这个答案

https://dev59.com/sK_la4cB1Zd3GeqPq06k#52960168

您可以使用具有不同视口宽度和高度的矢量可绘制对象作为背景。

要以编程方式执行此操作,您可以根据需要将背景设置为透明或具有不同尺寸的可绘制对象。

希望这可以帮助到您。


0
假设您在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>

我认为这将在位图周围绘制一个矩形边框,而我想将描边/边框添加到矢量可绘制路径(或从中创建的位图)中。@Doris - waqas
我已经添加了有关图片,请参考 @Doris。 - waqas
是的,上面的答案会产生一个矩形边框。看到您添加的图像后,我假设这个VectorDrawable可以用一条路径表示,在这种情况下,您需要定义strokeWidth和strokeColor来实现那个黑色轮廓/边框。 - Doris Liu
好的,谢谢您详细的解释 :) 我需要知道如何应用它,我希望描边宽度能够应用于 seekbarvalue,也就是说,当我增加 seek 时,描边会增加 @Doris - waqas
如何在<clip-path>周围绘制边框? - K Pradeep Kumar Reddy
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接