2016-02-25 21 views
14

随着新的android支持更新,矢量绘图获得向后兼容性。我有一个具有各种路径的矢量图像。我希望路径的颜色可以通过单击按钮或基于输入值以编程方式进行更改。是否有可能访问矢量路径的名称参数?然后改变颜色。如何更改按钮点击矢量绘制路径的颜色

+1

您可以调整整个矢量(与您选择的颜色改变R.color.headPink),而不是单一的路径组件 – Blackbelt

回答

1

使用此功能在您的矢量绘制

VectorChildFinder vector = new VectorChildFinder(this, R.drawable.my_vector, imageView); 

VectorDrawableCompat.VFullPath path1 = vector.findPathByName("path1"); 
path1.setFillColor(Color.RED); 

库更改路径的颜色是在这里:https://github.com/devsideal/VectorChildFinder

31

整个矢量的颜色可以使用setTint来改变。

你必须设置你的ImageView在你的布局文件,因为这:

<ImageView 
    android:id="@+id/myImageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:tint="@color/my_nice_color" 
    android:src="@drawable/ic_my_drawable" 
    android:scaleType="fitCenter" /> 

然后更改图像的色彩:

DrawableCompat.setTint(myImageView.getDrawable(), ContextCompat.getColor(context, R.color.another_nice_color)); 

注:myImageView.getDrawable()给出NullPointerException - 如果矢量绘制被设置为imageView作为背景。

+2

这台色调整个绘制,但不是的特定路径矢量绘制。 – suku

+1

设置'android:src'后必须设置'android:tint'# – rupinderjeet

+1

令人惊叹!谢谢。 – tinyCoder

0

由于在这个岗位 https://stackoverflow.com/a/32007436/4969047

表示通过@Eyal不能更改单个通道在运行时的颜色。 查看VectorDrawableCompat的源代码,按名称公开内部元素的唯一方法是getTargetByName,它存在于内部私有状态类VectorDrawableCompatStateVectorDrawableCompat中。

由于它是私人包装(默认) - 您不能使用它(除非您使用reflection)。

+1

@Shubral,能否请您详细说明如何使用反射来实现这一点?这可能需要时间,但会帮助大量的人。 – suku

4

你可以使用这个方法来改变颜色下的API,从而改变矢颜色片段

int myVectorColor = ContextCompat.getColor(getActivity(), R.color.colorBlack); 
        myButton.getIcon().setColorFilter(myVectorColor, PorterDuff.Mode.SRC_IN); 

到位getActivity的你应该使用MainActivity.this在活动改变矢量颜色

1

可以在运行时更改单个路径的颜色,而不使用反射。

VectorMaster引入了对矢量绘图的动态控制。使用这个库可以动态地(通过Java实例)控制vector drawable的每个方面。

只需添加以下依赖于你的应用程序的的build.gradle

dependencies { 
    compile 'com.sdsmdg.harjot:vectormaster:1.0.9' 
} 

在你的情况,你需要一个简单的颜色变化:

向量例如:your_vector。XML

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportWidth="24.0" 
    android:viewportHeight="24.0"> 
<path 
    android:name="outline" 
    android:pathData="M20.84,4..." 
    android:strokeColor="#5D5D5D" 
    android:fillColor="#00000000" 
    android:strokeWidth="2"/> 

XML

<com.sdsmdg.harjot.vectormaster.VectorMasterView 
    android:id="@+id/your_vector" 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    app:vector_src="@drawable/your_drawable" /> 

的Java

VectorMasterView heartVector = (VectorMasterView) 
findViewById(R.id.your_drawable); 

// find the correct path using name 
PathModel outline = heartVector.getPathModelByName("outline"); 

// set the stroke color 
outline.setStrokeColor(Color.parseColor("#ED4337")); 

// set the fill color (if fill color is not set or is TRANSPARENT, then no fill is drawn) 
outline.setFillColor(Color.parseColor("#ED4337")); 

来源:https://github.com/harjot-oberai/VectorMaster,MIT授权。

您现在可以完全控制矢量绘图。

0

有做同样的东西的几种方法,但是这同时适用于矢量绘图资源以及SVG(本地/网络)。

imageView.setColorFilter(ContextCompat.getColor(context, 
R.color.headPink), android.graphics.PorterDuff.Mode.SRC_IN); 

相关问题