android 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>

似乎这样做的方法是找到某个视图的属性,该属性具有从 0 缩放到 1 的 setter。 [imageFilterView][1] 类就是这样一种。 它有一个名为[设置交叉淡入淡出]的属性[2]

所以我的运动布局可以看起来像这样:

<?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>

随心所欲地过渡它..手动或使用在 Motion 布局文件中轻扫。 无论如何,之后,它将淡出一个可绘制对象并淡入另一个可绘制对象。正是我想要的。 [1]: https://developer.android.com/reference/androidx/constraintlayout/utils/widget/ImageFilterView [2]: https://developer.android.com/reference/androidx/constraintlayout/utils/widget/ImageFilterView#setCrossfade(浮子(

最新更新