Android之MotionLayout(四),用 MotionLayout實(shí)現(xiàn)向上拉的折疊效果

目錄

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)一個簡單上拉折疊效果是不是很簡單

(每天學(xué)習(xí)一點(diǎn)點(diǎn).每天進(jìn)步一點(diǎn)點(diǎn),分享不宜路過點(diǎn)個贊呀,喜歡的點(diǎn)個關(guān)注后續(xù)更新不斷)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容