有了新的android支持更新,矢量绘图就可以向后兼容。我有一个有不同路径的矢量图像。我希望路径的颜色在单击按钮时更改,或者根据输入值以编程方式更改。是否可以访问向量路径的名称参数?然后改变颜色。
发布于 2016-04-04 10:44:24
使用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));注意:如果将向量绘图设置为imageView作为背景,则imageView会给出空例外。
发布于 2018-02-07 12:15:26
有几种方法可以做同样的事情,但是这对于Vector 和SVG (本地/网络)都是有效的。
imageView.setColorFilter(ContextCompat.getColor(context,
R.color.headPink), android.graphics.PorterDuff.Mode.SRC_IN);(用您选择的颜色更改R.color.headPink )
发布于 2017-11-07 16:10:38
可以在运行时更改单个路径的颜色,而无需使用反射。
VectorMaster引入了对矢量图形的动态控制。可以使用这个库动态地(通过Java实例)控制向量绘图的每个方面。
只需在应用程序的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,麻省理工学院授权。
您现在可以完全控制矢量绘图了。
https://stackoverflow.com/questions/35625099
复制相似问题