首页 > 解决方案 > android motionLayout 的 CustomAttribute - 如何从可绘制资源中指定背景颜色

问题描述

在 androids motionLayout 中,我需要在视图转换时更改颜色。但它似乎不采用诸如“@drawable/myshape”之类的资源链接。它想要像“#FFFFFF”这样的原始值。这是我到目前为止所做的:

    <?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        android:id="@+id/transition"
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@id/start"
        app:motionInterpolator="linear"
        app:moveWhenScrollAtTop="true">

    </Transition>

    <ConstraintSet android:id="@+id/start">

        <Constraint
            android:id="@+id/spaceShip"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:alpha="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" >

<!--            <CustomAttribute-->
<!--                app:attributeName="backgroundColor"-->
<!--               app:customColorValue="@drawable/space_spaceShip_bg" />-->  

//上面的代码不起作用。它需要一个原始值,如何从可绘制对象中指定它,因为我已经为我的背景创建了一个自定义背景形状。

        </Constraint>

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">

        <Constraint
            android:id="@+id/spaceShip"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:alpha="0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" >

            <CustomAttribute
                app:attributeName="backgroundColor"
                app:customColorValue="#FFFFFF" />    //also here how to specify @color/white instead of raw value
        </Constraint>

    </ConstraintSet>

</MotionScene>

标签: androidandroid-motionlayout

解决方案


似乎这样做的方法是找到某个视图的属性,该属性具有从 0 到 1 的设置器。 [imageFilterView][1] 类就是这样一个。它有一个名为 [setcrossfade][2] 的属性

所以我的motionlayout看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        android:id="@+id/transition"
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@id/start">

    </Transition>

    <ConstraintSet android:id="@+id/start">

        <Constraint
            android:id="@+id/myview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:alpha="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" >

            <CustomAttribute
                app:attributeName="crossfade"
                app:customFloatValue="0" />

        </Constraint>

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">

        <Constraint
            android:id="@+id/myview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:alpha="0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" >

            <CustomAttribute
                app:attributeName="crossfade"
                app:customFloatValue="1" />

        </Constraint>

    </ConstraintSet>

</MotionScene>

在我们的布局文件中,我们可以这样做:

 <androidx.constraintlayout.motion.widget.MotionLayout
    android:id="@+id/ml"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_gravity="bottom"
    app:layoutDescription="@xml/motion_scene">

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/myview"
        android:layout_width="0dp"
        android:layout_height="0dp"

        android:src="@drawable/bg1"
        app:altSrc="@drawable/bg2"

        android:background="@drawable/bg1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.motion.widget.MotionLayout>

以您想要的方式转换它..手动或使用运动布局文件中的滑动。无论如何,它会淡出一个drawable并淡入另一个drawable。正是我想要的。[1]:https ://developer.android.com/reference/androidx/constraintlayout/utils/widget/ImageFilterView [2]:https ://developer.android.com/reference/androidx/constraintlayout/utils/widget/ImageFilterView #setCrossfade(浮动)


推荐阅读