08 MotionLayout-上拉的折叠效果

作者: 凤邪摩羯 | 来源:发表于2021-08-12 08:58 被阅读0次

先看下效果图

image

1.创建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.创建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">

        <!--  关联到 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>

这样就能实现一个简单上拉折叠效果是不是很简单

相关文章

网友评论

    本文标题:08 MotionLayout-上拉的折叠效果

    本文链接:https://www.haomeiwen.com/subject/umgkzktx.html