【问题标题】:Android: How to set stroke color for vector drawable programmaticallyAndroid:如何以编程方式设置矢量可绘制的笔触颜色
【发布时间】:2015-12-22 09:43:07
【问题描述】:

我在 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 cannot be cast to android.graphics.drawable.GradientDrawable
  • 哦,好吧,我的错……删除评论以避免其他人的错误答案。如果我对您的问题有任何其他答案,会通知您。谢谢
  • 你知道怎么做吗?
  • 您找到解决方案了吗?如果是,请分享。如何在 周围绘制边框?

标签: android android-vectordrawable


【解决方案1】:

正如Chris Baneshis blog 中所说,您可以使用支持库使用以下代码为您的drawable 着色:

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);

【讨论】:

    【解决方案2】:

    尝试在您的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>
    

    参考here

    【讨论】:

    • 如何以编程方式设置形状的笔触?
    • 啊对不起,你想要它以编程方式请在这里检查它可能对你有帮助stackoverflow.com/a/29204632/3077569
    • 就我而言,我有一个 VectorDrawable,它不能转换为 GradientDrawable。无论如何,感谢您的帮助!
    【解决方案3】:

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

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

    无法使用 java 代码设置矢量的描边(颜色、大小等)。你必须在drawable中处理它。

    【讨论】:

      【解决方案4】:

      使用setTint( newColor ) 方法。

      VectorDrawable vd = (VectorDrawable)context.getDrawable( R.drawable.ic_hint );
      vd.setTint( 0xffff0000 ); //for red color
      

      【讨论】:

        猜你喜欢
        • 2023-03-02
        • 2015-07-01
        • 2015-07-29
        • 1970-01-01
        • 2022-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多