MotionLayout 过渡动画

78 阅读1分钟

@layout/compass_main

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/material_dynamic_neutral0"
        app:layoutDescription="@xml/compass_main_2_scene"
        >

        <com.example.wifidemo1.customview.Compass
            android:id="@+id/compass"
            android:background="@drawable/compass"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintWidth_percent="0.7"
            app:layout_constraintDimensionRatio="1:1"
            />

        <androidx.constraintlayout.helper.widget.Flow
            android:id="@+id/flow"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:flow_wrapMode="chain"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:constraint_referenced_ids="compass"
            >

        </androidx.constraintlayout.helper.widget.Flow>

    </androidx.constraintlayout.motion.widget.MotionLayout>

</layout>

@layout/compass_main_2

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layoutDescription="@xml/compass_main_2_scene">

    <com.example.wifidemo1.customview.Compass
        android:id="@+id/compass"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/compass"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintDimensionRatio="1:1"
        />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:flow_wrapMode="chain"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:constraint_referenced_ids="compass"
        >

    </androidx.constraintlayout.helper.widget.Flow>

</androidx.constraintlayout.motion.widget.MotionLayout>

@xml/compass_main_2_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="@layout/compass_main_2"
        app:constraintSetStart="@layout/compass_main"
        app:duration="2000"
        >

        <OnClick app:clickAction="toggle" app:targetId="@id/compass"/>

    </Transition>

</MotionScene>

constraintSetEnd除了可以设置id还可以设置xml,第二个文件可以不用MotionLayout。 因为MotionScene只会确定两个xml对view的位置信息的更改,不会新增view。

若需要新增view,你可能需要的是acivity跳转的过渡动画,详见:juejin.cn/post/684490… juejin.cn/post/714010…