suk*_*uku 37 android vector vector-graphics android-vectordrawable
随着新的Android支持更新,矢量drawables获得向后兼容性.我有一个带有各种路径的矢量图像.我希望通过单击按钮或基于输入值以编程方式更改路径的颜色.是否可以访问矢量路径的name参数?然后改变颜色.
Mar*_* HC 51
可以使用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" />
Run Code Online (Sandbox Code Playgroud)
然后更改图像的颜色:
DrawableCompat.setTint(myImageView.getDrawable(), ContextCompat.getColor(context, R.color.another_nice_color));
Run Code Online (Sandbox Code Playgroud)
注意:myImageView.getDrawable()如果向量drawable设置为imageView作为背景,则给出nullpointerexception.
Dev*_*ven 19
使用此选项可更改矢量drawable中的路径颜色
VectorChildFinder vector = new VectorChildFinder(this, R.drawable.my_vector, imageView);
VectorDrawableCompat.VFullPath path1 = vector.findPathByName("path1");
path1.setFillColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)
图书馆在这里:https://github.com/devsideal/VectorChildFinder
Moo*_*idi 10
有几种方法可以做同样的事情,但这对Vector Drawables和SVG(本地/网络)都有效。
imageView.setColorFilter(ContextCompat.getColor(context,
R.color.headPink), android.graphics.PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)
(使用您选择的颜色更改 R.color.headPink)
小智 5
您可以使用此方法更改较低API中的颜色以更改片段中的矢量颜色
int myVectorColor = ContextCompat.getColor(getActivity(), R.color.colorBlack);
myButton.getIcon().setColorFilter(myVectorColor, PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)
代替getActivity,你应该使用MainActivity.this来改变活动中的矢量颜色
您可以在运行时更改单个路径的颜色,而无需使用反射。
VectorMaster 引入了对矢量绘图的动态控制。使用此库可以动态控制矢量绘图的每个方面(通过 Java 实例)。
只需在应用程序的 build.gradle 中添加以下依赖项
dependencies {
compile 'com.sdsmdg.harjot:vectormaster:1.0.9'
}
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您需要简单的颜色更改:
矢量示例: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"/>
Run Code Online (Sandbox Code Playgroud)
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" />
Run Code Online (Sandbox Code Playgroud)
爪哇:
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"));
Run Code Online (Sandbox Code Playgroud)
来自:https: //github.com/harjot-oberai/VectorMaster,已获得 MIT 许可。
您现在可以完全控制矢量绘图。
| 归档时间: |
|
| 查看次数: |
40604 次 |
| 最近记录: |