我正在使用MotionLayout完成一个项目,一切都运行得很顺利,除了一个小问题。当我打开应用并在屏幕上按下某处时,有时布局会跳转到未定义的状态。
我已定义的状态如下: - 应用程序从“破碎白色”背景开始 - 向右滑动时,将显示具有蓝色背景的框架布局 - 向上滑动时,将显示具有红色背景的框架布局,隐藏所有其他imageView
当在屏幕上按下某处时,应用程序采用的状态如下: - 出现红色背景,所有imageView可见。
我一直在寻找出现这种情况的原因,我猜这是个bug(因为它仍处于alpha版本),但想听听其他人的意见。
代码将附在此以便轻松复制。
主要布局:
动态布局:
我已定义的状态如下: - 应用程序从“破碎白色”背景开始 - 向右滑动时,将显示具有蓝色背景的框架布局 - 向上滑动时,将显示具有红色背景的框架布局,隐藏所有其他imageView
当在屏幕上按下某处时,应用程序采用的状态如下: - 出现红色背景,所有imageView可见。
我一直在寻找出现这种情况的原因,我猜这是个bug(因为它仍处于alpha版本),但想听听其他人的意见。
代码将附在此以便轻松复制。
主要布局:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl_forUnity"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"
android:alpha="1"
tools:ignore="UnusedAttribute"/>
<androidx.constraintlayout.motion.widget.MotionLayout
app:layoutDescription="@xml/scene_motion"
android:id="@+id/topMotionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<View
android:id="@+id/ivBottomAnchor"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<FrameLayout
android:id="@+id/flBottom"
app:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<View
android:id="@+id/ivLeftAnchor"
android:layout_width="1dp"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/flLeft"
app:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
android:layout_width="300dp"
android:layout_height="match_parent"/>
<View
android:id="@+id/ivRightAnchor"
android:layout_width="1dp"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/flRight"
app:layout_constraintStart_toEndOf="@id/ivRightAnchor"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<ImageView
android:layout_width="32dp"
android:src="@drawable/ic_message"
android:layout_height="32dp"
tools:tint="@color/colorGreen_200"
android:id="@+id/fabStartButton"/>
<ImageView
android:id="@+id/middleButton"
tools:tint="@color/colorGreen_200"
android:layout_width="64dp"
android:src="@drawable/ic_circle"
android:layout_height="64dp"/>
<ImageView
android:id="@+id/bottomButton"
tools:tint="@color/colorGreen_200"
android:layout_width="28dp"
android:src="@drawable/ic_message"
android:layout_height="28dp"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
tools:tint="@color/colorGreen_200"
android:src="@drawable/ic_adb"
android:id="@+id/fabEndButton"/>
<ImageView
android:src="@drawable/ic_orb"
android:id="@+id/ivOrb"
tools:tint="@color/colorGreen_200"
android:layout_width="6dp"
android:layout_height="6dp"/>
</androidx.constraintlayout.motion.widget.MotionLayout>
动态布局:
动态布局是一种新型的布局方式:
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
android:id="@+id/SwipeUp"
motion:constraintSetStart="@+id/beginning"
motion:constraintSetEnd="@+id/endAtTop">
<OnSwipe
motion:dragDirection="dragUp"
motion:touchAnchorId="@id/ivBottomAnchor"
motion:touchAnchorSide="top"/>
<KeyFrameSet>
<KeyAttribute
android:alpha="0"
motion:framePosition="15"
motion:target="@id/bottomButton"/>
<KeyAttribute
android:alpha="0"
motion:framePosition="15"
motion:target="@id/fabStartButton"/>
<KeyAttribute
android:alpha="0"
motion:framePosition="15"
motion:target="@id/middleButton"/>
<KeyAttribute
android:alpha="0"
motion:framePosition="15"
motion:target="@id/fabEndButton"/>
<KeyAttribute
motion:target="@id/backgroundView">
<CustomAttribute
motion:attributeName="backgroundColor"
motion:customColorValue="#B71C1C"/>
</KeyAttribute>
</KeyFrameSet>
</Transition>
<Transition
android:id="@+id/SwipeLeft"
motion:constraintSetStart="@+id/beginning"
motion:constraintSetEnd="@+id/endRighty">
<OnSwipe
motion:dragDirection="dragLeft"
motion:touchAnchorId="@id/ivRightAnchor"
motion:touchAnchorSide="top"/>
</Transition>
<Transition
android:id="@+id/SwipeRight"
motion:constraintSetStart="@+id/beginning"
motion:constraintSetEnd="@+id/endLefty">
<OnSwipe
motion:dragDirection="dragRight"
motion:touchAnchorId="@id/ivLeftAnchor"
motion:touchAnchorSide="top"/>
<KeyFrameSet>
<KeyAttribute
motion:target="@id/backgroundView">
<CustomAttribute
motion:attributeName="backgroundColor"
motion:customColorValue="#0D47A1"/>
</KeyAttribute>
</KeyFrameSet>
</Transition>
<!--TODO: pressing on the bottombutton, go to bottom screen-->
<ConstraintSet android:id="@+id/beginning">
<Constraint
android:id="@+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"/>
<Constraint
android:id="@+id/tbMain"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<Constraint
android:id="@+id/ivBottomAnchor"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_height="1dp"
android:layout_width="match_parent"/>
<Constraint
android:id="@+id/flBottom"
motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivLeftAnchor"
motion:layout_constraintStart_toStartOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flLeft"
motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
android:layout_width="300dp"
android:layout_marginEnd="10dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivRightAnchor"
motion:layout_constraintEnd_toEndOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flRight"
android:layout_width="0dp"
motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/fabStartButton"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginBottom="32dp"
android:layout_marginStart="32dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/middleButton"
android:layout_width="78dp"
android:layout_height="78dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="72dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/bottomButton"
android:layout_marginBottom="32dp"
android:layout_width="28dp"
android:layout_height="28dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/fabEndButton"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginBottom="32dp"
android:layout_marginEnd="32dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"/>
<Constraint
android:id="@+id/ivOrb"
android:layout_width="6dp"
android:layout_height="6dp"
android:visibility="gone"
android:alpha="0"
android:layout_marginTop="8dp"
motion:layout_constraintTop_toBottomOf="@id/fabStartButton"
motion:layout_constraintStart_toStartOf="@id/middleButton"
motion:layout_constraintEnd_toEndOf="@id/middleButton"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/endAtTop">
<Constraint
android:id="@+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="1"/>
<Constraint
android:id="@+id/tbMain"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<Constraint
android:id="@+id/ivBottomAnchor"
android:layout_height="1dp"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_width="match_parent"/>
<Constraint
android:id="@+id/flBottom"
motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivLeftAnchor"
motion:layout_constraintStart_toStartOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flLeft"
motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
android:layout_width="300dp"
android:layout_marginEnd="10dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivRightAnchor"
motion:layout_constraintEnd_toEndOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flRight"
android:layout_width="0dp"
motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/fabStartButton"
android:layout_width="26dp"
android:layout_height="26dp"
android:alpha="0"
android:layout_marginBottom="32dp"
android:layout_marginStart="80dp"
android:translationY="-100dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/middleButton"
android:layout_width="52dp"
android:translationY="-100dp"
android:alpha="0"
android:layout_height="52dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="38dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/bottomButton"
android:translationY="-100dp"
android:layout_width="26dp"
android:layout_height="26dp"
android:alpha="0"
motion:layout_constraintTop_toBottomOf="@id/middleButton"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/fabEndButton"
android:layout_width="26dp"
android:translationY="-100dp"
android:alpha="0"
android:layout_height="26dp"
android:layout_marginBottom="32dp"
android:layout_marginEnd="80dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"/>
<Constraint
android:id="@+id/ivOrb"
android:visibility="gone"
android:translationY="-100dp"
android:layout_width="6dp"
android:alpha="0"
android:layout_height="6dp"
android:layout_marginTop="8dp"
motion:layout_constraintTop_toBottomOf="@id/fabStartButton"
motion:layout_constraintStart_toStartOf="@id/middleButton"
motion:layout_constraintEnd_toEndOf="@id/middleButton"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/endRighty">
<Constraint
android:id="@+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"/>
<Constraint
android:id="@+id/tbMain"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<Constraint
android:id="@+id/ivBottomAnchor"
android:layout_height="1dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"/>
<Constraint
android:id="@+id/flBottom"
motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivLeftAnchor"
motion:layout_constraintStart_toStartOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flLeft"
motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
android:layout_width="300dp"
android:layout_marginEnd="10dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivRightAnchor"
motion:layout_constraintStart_toStartOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flRight"
motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/fabStartButton"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginBottom="32dp"
android:layout_marginStart="80dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/middleButton"
android:layout_width="52dp"
android:layout_height="52dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="38dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/bottomButton"
android:layout_width="28dp"
android:layout_height="28dp"
android:alpha="0"
motion:layout_constraintTop_toBottomOf="@id/middleButton"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/fabEndButton"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginBottom="32dp"
android:layout_marginEnd="80dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"/>
<Constraint
android:id="@+id/ivOrb"
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_marginTop="8dp"
motion:layout_constraintTop_toBottomOf="@id/fabEndButton"
motion:layout_constraintStart_toStartOf="@id/fabEndButton"
motion:layout_constraintEnd_toEndOf="@id/fabEndButton"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/endLefty">
<Constraint
android:id="@+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="1"/>
<Constraint
android:id="@+id/tbMain"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<Constraint
android:id="@+id/ivBottomAnchor"
android:layout_height="1dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"/>
<Constraint
android:id="@+id/flBottom"
motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivLeftAnchor"
motion:layout_constraintEnd_toEndOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flLeft"
motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/ivRightAnchor"
motion:layout_constraintEnd_toEndOf="parent"
android:layout_height="match_parent"
android:layout_width="1dp"/>
<Constraint
android:id="@+id/flRight"
android:layout_width="0dp"
motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintTop_toBottomOf="@id/tbMain"
android:layout_height="0dp"/>
<Constraint
android:id="@+id/fabStartButton"
android:layout_width="26dp"
android:layout_height="26dp"
android:alpha="0"
android:layout_marginBottom="32dp"
android:layout_marginStart="80dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/middleButton"
android:layout_width="52dp"
android:layout_height="52dp"
android:alpha="0"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="38dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/bottomButton"
android:layout_width="28dp"
android:layout_height="28dp"
android:alpha="0"
motion:layout_constraintTop_toBottomOf="@id/middleButton"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<Constraint
android:id="@+id/fabEndButton"
android:layout_width="26dp"
android:layout_height="26dp"
android:alpha="0"
android:layout_marginBottom="32dp"
android:layout_marginEnd="80dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"/>
<Constraint
android:id="@+id/ivOrb"
android:alpha="0"
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_marginTop="8dp"
motion:layout_constraintTop_toBottomOf="@id/fabStartButton"
motion:layout_constraintStart_toStartOf="@id/fabStartButton"
motion:layout_constraintEnd_toEndOf="@id/fabStartButton"/>
</ConstraintSet>