プログラムでストロークまたは塗りつぶしの色を変更する。ベクトルの塗りつぶし色をプログラムではなくストロークカラーではなく変更することは可能ですか?私は、ベクトル描画可能に使用ベクトルレイアウトの塗りつぶし色またはstrokeColorをプログラムで変更する
vector_icon.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:strokeColor="#FF000000"
android:strokeWidth="0.8"
android:strokeAlpha="0.5"
android:strokeLineJoin="round"
android:strokeLineCap="round"
android:strokeMiterLimit="1"
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/>
</vector>
イメージ図。
<ImageView
android:id="@+id/ivNotification"
android:layout_width="@dimen/navigation_icon_size"
android:layout_height="@dimen/navigation_icon_size"
android:scaleType="fitXY"
android:src="@drawable/vector_icon_notification"/>
VectorDrawableをGradientDrawableにキャストしようとしましたが、クラスキャストの実行をスローしました。
GradientDrawable gdFavourite = (GradientDrawable) ivFavourite.getDrawable();
gdFavourite.setStroke(1, colorPrimary);
gdFavourite.setColor(colorPrimary);
tvFavourite.setTextColor(colorPrimary);
とjava.lang.ClassCastException:android.graphics.drawable.VectorDrawableはandroid.graphics.drawable.LayerDrawable
をされていません。
あなたはそれarroundの仕事にhere
一つの可能な解決策このクラスの完全なドキュメントを参照することができ、他のXMLからVectorDrawableを取得し、呼び出すことによってexisten 1を上書きすることですまさに[アニメーション化されたベクトル描画可能ファイル](https://developer.android.com/training/material/animations.html#AnimVector)は何ですか?なぜあなたはそれらの1つを使っていませんか? – ianhanniballake