安卓:如何在程序中设置矢量图形的描边颜色

13

我在Android中遇到了VectorDrawable的问题。

我有一个矢量可绘制文件(.xml),我想将其绘制在位图上。我已经成功加载此文件并将其绘制在位图上。我可以更改其填充颜色,但问题是我无法更改其描边和颜色。

任何帮助都将不胜感激!!!

谢谢!

这是可绘制文件:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="312dp"
    android:height="312dp"
    android:viewportWidth="312.7"
    android:viewportHeight="312.699">
<path
    android:pathData="M306.35,266.34c0,22.09 -17.91,40.01 -40,40.01L46.35,306.35c-22.09,0 -40,-17.92 -40,-40.01v-219.99c0,-22.11 17.92,-40 40,-40h220c22.09,0 40,17.9 40,40L306.35,266.34z"
    android:strokeWidth="5"
    android:fillColor="@color/transparent"
    android:strokeColor="#231F20"/></vector>

这是我加载并用蓝色填充形状的方法:

Drawable drawable = getResources().getDrawable(R.drawable.graph_rounded_rectangle);
        drawable.setBounds(0, 0, width, height);
        drawable.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY));
        drawable.draw(canvas);

我刚刚尝试了一下,结果出现了一个错误:java.lang.ClassCastException: android.graphics.drawable.VectorDrawable无法强制转换为android.graphics.drawable.GradientDrawable。 - Thang Pham
哦,好的,那是我的错。删除评论以避免误导他人。如果我得到你问题的其他答案,我会告诉你的。谢谢。 - rahul
你弄清楚怎么做了吗? - Red M
你找到解决方案了吗?如果是的话,请分享一下。如何在<clip-path>周围绘制边框? - K Pradeep Kumar Reddy
4个回答

8

正如Chris Banes他的博客中所说,您可以使用支持库来着色您的可绘制对象,以下是代码:

Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_asset);

// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

6
尝试像这样在您的XML中添加“group”:

尝试像这样在您的XML中添加“group”:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="312dp"
    android:height="312dp"
    android:viewportWidth="312.7"
    android:viewportHeight="312.699">
    <group android:scaleX="1.0" android:scaleY="1.0">
        <path
            android:pathData="M306.35,266.34c0,22.09 -17.91,40.01 -40,40.01L46.35,306.35c-22.09,0 -40,-17.92 -40,-40.01v-219.99c0,-22.11 17.92,-40 40,-40h220c22.09,0 40,17.9 40,40L306.35,266.34z"
            android:strokeWidth="5"
            android:fillColor="@color/transparent"
            android:strokeColor="#231F20"/>
    </group>
</vector>

Reference here.


我该如何通过编程设置形状的描边? - Thang Pham
1
啊,抱歉,如果您想要编程方面的帮助,请在这里查看,或许可以帮到您:https://dev59.com/Im445IYBdhLWcg3wnroi#29204632 - Context
在我的情况下,我有一个VectorDrawable,它无法转换为GradientDrawable。无论如何,感谢您的帮助! - Thang Pham

1

您只能在Java API中访问这些向量属性。

Vector : name
Vector : width
Vector : height
Vector : viewportWidth
Vector : viewportHeight
Vector : tint
Vector : tintMode
Vector : autoMirrored
Vector : alpha

使用Java代码设置矢量图形的描边(颜色、大小等)是不可行的。您需要在drawable中处理它。


1
使用setTint( newColor )方法。
VectorDrawable vd = (VectorDrawable)context.getDrawable( R.drawable.ic_hint );
vd.setTint( 0xffff0000 ); //for red color

这个可以工作,但是它被应用到整个VectorDrawable上。如何将其应用于其中的一部分(通过路径/组名)?我尝试在VectorDrawableCompat.getTargetByName上使用反射,但它只返回null。 - android developer

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