当使用MotionLayout时,如何将色调指定为ImageView的自定义属性?

9

在使用MotionLayout时,如何将ImageView指定为自定义属性的色彩。目前我只能在我的MotionScene xml文件中指定自定义背景颜色:

<ConstraintSet android:id="@+id/end">
    <Constraint
        android:id="@+id/imageView"
        android:layout_width="180dp"
        android:layout_height="180dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintVertical_bias="0.75"
        motion:srcCompat="@drawable/ic_android_black_24dp" >
        <CustomAttribute
            motion:attributeName="backgroundColor"
            motion:customColorValue="#9999FF" />
    </Constraint>
</ConstraintSet>
3个回答

16
请注意,您正在使用backgroundColor,但属性应为android:background。对于tint,您应该编写:

请注意,您正在使用backgroundColor,但属性应为android:background。对于tint,您应该编写:

            <CustomAttribute
                motion:attributeName="ColorFilter"
                motion:customColorValue="#9999FF" />
据我所知,CustomAttribute 特性使用反射而不是来自xml等的属性。 对于所有自定义特性,请记住这一点。

如何在自定义属性中设置可见性。 - Jithish P N
@JithishPN 你可以在 <Constraint ...> 中设置可见性,就像其他 android:layout_width 等行一样。将其输入为:android:visibility="visible"。 - Nick Dev
3
有没有一个好的地方可以找到所有的attributeName?例如,我想找到不同的attributeName,比如CardView的"ColorFilter"。你提到了反射,这是否意味着Java代码中可用的getter和setter? - Nick Dev

9
你可以使用 ColorFilter
<CustomAttribute
    motion:attributeName="colorFilter"
    motion:customColorValue="@color/your_tint_color" />

如何更改ImageView的'src'?(在状态之间更改可绘制对象) - Niroshan

-4

由于tint采用颜色值,因此它应该与backgroundColor几乎相同。

<CustomAttribute motion:attributeName="tint" motion:customColorValue="@color/your_tint_color" />


2
为使自定义属性起作用,必须存在与属性名称完全相同的setter/getter函数(getRadius()/setRadius(...)-> radius)。在这种情况下,ImageView类没有像“setTint(int color)”这样的函数。该类的setTint函数仅接受ColorStateList,而不是简单的颜色值。因此,这将无法工作。 - Palm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接