使用 MotionLayout 时如何将 tint 指定为 ImageView 上的自定义属性

Eme*_*kpo 5 android android-constraintlayout

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

<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>
Run Code Online (Sandbox Code Playgroud)

ylu*_*lus 11

请注意,您正在使用backgroundColor但属性是android:background. 因为tint你应该写:

            <CustomAttribute
                motion:attributeName="ColorFilter"
                motion:customColorValue="#9999FF" />
Run Code Online (Sandbox Code Playgroud)

据我所知,CustomAttribute使用反射的功能而不是来自 xml 等的属性。请记住所有自定义属性。

  • 有没有找到所有 attributeName 的好地方?例如,我希望能够找到 CardView 的不同属性名称,例如“ColorFilter”。您提到了反射,这是否仅仅意味着 Java 代码中可用的 getter 和 setter? (3认同)

Lin*_*Lin 8

您可以使用ColorFilter

<CustomAttribute
    motion:attributeName="colorFilter"
    motion:customColorValue="@color/your_tint_color" />
Run Code Online (Sandbox Code Playgroud)


小智 -4

由于色调采用颜色值,因此它应该几乎与backgroundColor

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

  • 为了使自定义属性起作用,必须存在一个与属性名称完全相同的 setter/getter 函数(getRadius()/setRadius(...) -&gt; radius)。在这种情况下,ImageView 类没有像“setTint(int color)”这样的函数。该类中的 setTint 函数仅接受 ColorStateList,但不接受简单的颜色值。所以这行不通 (2认同)