此刻,我试图理解MotionLayout(在“测试应用程序”中)以将其应用于更大的应用程序。
我正在尝试模拟可折叠的工具栏。
我用几个图像和ScrollView进行了布局:
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/motion_l"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/motion_test"
tools:context=".MainActivity"
tools:showPaths="true">
<ImageView
android:id="@+id/background_image"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/img_back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent " />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Awsome title"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="@id/background_image"
app:layout_constraintEnd_toEndOf="parent "
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/back_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent " />
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/background_image">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@+id/text1"
app:layout_constraintTop_toTopOf="@+id/text1">
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/text_test"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/text_test"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<ImageView
android:id="@+id/logo"
android:layout_width="64dp"
android:layout_height="64dp"
android:elevation="4dp"
android:scaleType="centerCrop"
android:src="@drawable/img_logo"
app:layout_constraintBottom_toBottomOf="@id/background_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/background_image" />
</androidx.constraintlayout.motion.widget.MotionLayout>
然后我创建了MotionScene布局来处理过渡:
MotionScene xml
<?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
app:constraintSetEnd="@id/collapsed"
app:constraintSetStart="@id/expanded">
<OnSwipe
app:dragDirection="dragUp"
app:touchAnchorId="@id/scroll_view"
app:touchAnchorSide="top" />
</Transition>
<ConstraintSet android:id="@+id/collapsed">
<Constraint
android:id="@id/background_image"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CustomAttribute
app:attributeName="imageAlpha"
app:customIntegerValue="0" />
</Constraint>
<Constraint
android:id="@id/logo"
android:layout_width="64dp"
android:layout_height="64dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@id/background_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/background_image">
<CustomAttribute
app:attributeName="imageAlpha"
app:customIntegerValue="0" />
</Constraint>
<Constraint
android:id="@id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/background_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/background_image">
<CustomAttribute
app:attributeName="textColor"
app:customColorValue="#000000" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/expanded">
<Constraint
android:id="@id/background_image"
android:layout_height="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CustomAttribute
app:attributeName="imageAlpha"
app:customIntegerValue="255" />
</Constraint>
<Constraint
android:id="@id/logo"
android:layout_width="64dp"
android:layout_height="64dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@id/background_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/background_image">
<CustomAttribute
app:attributeName="imageAlpha"
app:customIntegerValue="255" />
</Constraint>
<Constraint
android:id="@id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:scaleX="1.0"
android:scaleY="1.0"
app:layout_constraintBottom_toBottomOf="@id/background_image"
app:layout_constraintEnd_toEndOf="parent "
app:layout_constraintStart_toStartOf="parent">
<CustomAttribute
app:attributeName="textColor"
app:customColorValue="#ffffff" />
</Constraint>
</ConstraintSet>
</MotionScene>
这是我遇到的问题。动画效果很好,但是只有在ImageView中执行手势时才可以。如果在ScrollView中执行手势,则忽略过渡。
如您在OnSwipe
中所见,touchAnchorID
已应用于scrollView。
也许无法应用于ScrollView?在那种情况下,可以采用哪种解决方案?
因为在“真实项目”中,视图是使用ScrollView完成的。