目錄
Android之MotionLayout(一),MotionLayout的基本使用
Android之MotionLayout(二),MotionScene的標(biāo)簽屬性說明
Android之MotionLayout(三),用 MotionLayout 來做過渡動畫,如何使用ConstraintSet
Android之MotionLayout(四),用 MotionLayout實(shí)現(xiàn)向上拉的折疊效果
Android之MotionLayout(五),如何使用 MotionLayout的自定義屬性
Android之MotionLayout(六),如果使用Keyframes實(shí)現(xiàn)實(shí)現(xiàn)YouTube切換效果
這篇文章主要通過MotionLayout實(shí)現(xiàn)實(shí)現(xiàn)向上拉的折疊效果
核心拖拽使用了OnSwipe效果
先看下效果圖

aa5b15377a85b29ed63922ea9ba00757.gif
1.創(chuàng)建acticity_scene.xml 布局文件
<?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"
android:id="@+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_scene">
<ImageView
android:id="@+id/image"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/bg_circular"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<ImageView
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
圓形的背景文件 bg_circular
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false">
<solid android:color="#FF0000" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
2.創(chuàng)建MotionScene文件(文件名:activity_scene)
<?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/layout_end"
app:constraintSetStart="@id/layout_start"
app:duration="1000">
<!-- 關(guān)聯(lián)到 bottomBar 上-->
<OnSwipe
app:dragDirection="dragUp"
app:touchAnchorId="@id/image"
app:touchAnchorSide="top" />
<!-- <OnClick-->
<!-- app:clickAction="toggle"-->
<!-- app:targetId="@id/image" />-->
</Transition>
<ConstraintSet android:id="@+id/layout_start">
<Constraint android:id="@+id/bottomBar">
<Layout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
</Constraint>
<Constraint android:id="@+id/image">
<Layout
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/bg_circular"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/layout_end">
<Constraint android:id="@+id/bottomBar">
<Layout
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent" />
</Constraint>
<Constraint android:id="@+id/image">
<Layout
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/bg_circular"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</Constraint>
</ConstraintSet>
</MotionScene>
這樣就能實(shí)現(xiàn)一個簡單上拉折疊效果是不是很簡單