如何使用 Kotlin 或 Java 更改可绘制对象的描边颜色
How to change stroke color of a drawable with Kotlin or Java
我有一个可绘制对象,我用它来设置线性布局的背景,将线性与圆形和半透明的橙色线分开。但在代码中的某个时刻,我需要将此背景(可绘制对象)的颜色更改为我仅作为参数使用的颜色,而我的颜色文件中没有它。我需要将此可绘制对象的描边颜色更改为我在运行时变量之一中拥有的颜色
bg_static_show_password.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- center circle -->
<stroke android:color="@color/accent_color_alpha"
android:width="3dp" />
<solid android:color="@android:color/transparent" />
<size
android:width="28dp"
android:height="28dp"/>
</shape>
线性布局
<android.support.constraint.ConstraintLayout
android:id="@+id/card_show_password"
android:layout_width="@dimen/anim_layout_size"
android:layout_height="@dimen/anim_layout_size"
android:layout_marginTop="@dimen/anim_margin_top"
android:background="@drawable/bg_static_show_password"
android:layout_gravity="center"
app:layout_constraintTop_toBottomOf="@id/view_group_item"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
我尝试用来改变颜色的方法
fun showAlternativeForAnimation(view: LinearLayout) {
val drawable = view.background as GradientDrawable
val theme = PasswordRecoveryTheme(ApplicationSession.instance?.themeId)
drawable.setStroke(1, theme.getAccentColor(ApplicationFactory.context!!))
}
方法的参数是LinearLayout
当我尝试时,我得到了这个异常:
kotlin.TypeCastException: null 不能转换为非 null 类型 android.graphics.drawable.GradientDrawable
进行安全转换 (as?
),确保您传递的视图具有可绘制形状设置作为背景并将参数更改为 view: View
以允许使用任何视图(LinearLayout 、ConstraintLayout 等)。
fun showAlternativeForAnimation(view: View) {
val drawable = view.background as? GradientDrawable
val theme = PasswordRecoveryTheme(ApplicationSession.instance?.themeId)
drawable?.setStroke(1, theme.getAccentColor(ApplicationFactory.context!!))
}
我有一个可绘制对象,我用它来设置线性布局的背景,将线性与圆形和半透明的橙色线分开。但在代码中的某个时刻,我需要将此背景(可绘制对象)的颜色更改为我仅作为参数使用的颜色,而我的颜色文件中没有它。我需要将此可绘制对象的描边颜色更改为我在运行时变量之一中拥有的颜色
bg_static_show_password.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- center circle -->
<stroke android:color="@color/accent_color_alpha"
android:width="3dp" />
<solid android:color="@android:color/transparent" />
<size
android:width="28dp"
android:height="28dp"/>
</shape>
线性布局
<android.support.constraint.ConstraintLayout
android:id="@+id/card_show_password"
android:layout_width="@dimen/anim_layout_size"
android:layout_height="@dimen/anim_layout_size"
android:layout_marginTop="@dimen/anim_margin_top"
android:background="@drawable/bg_static_show_password"
android:layout_gravity="center"
app:layout_constraintTop_toBottomOf="@id/view_group_item"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
我尝试用来改变颜色的方法
fun showAlternativeForAnimation(view: LinearLayout) {
val drawable = view.background as GradientDrawable
val theme = PasswordRecoveryTheme(ApplicationSession.instance?.themeId)
drawable.setStroke(1, theme.getAccentColor(ApplicationFactory.context!!))
}
方法的参数是LinearLayout
当我尝试时,我得到了这个异常: kotlin.TypeCastException: null 不能转换为非 null 类型 android.graphics.drawable.GradientDrawable
进行安全转换 (as?
),确保您传递的视图具有可绘制形状设置作为背景并将参数更改为 view: View
以允许使用任何视图(LinearLayout 、ConstraintLayout 等)。
fun showAlternativeForAnimation(view: View) {
val drawable = view.background as? GradientDrawable
val theme = PasswordRecoveryTheme(ApplicationSession.instance?.themeId)
drawable?.setStroke(1, theme.getAccentColor(ApplicationFactory.context!!))
}