constraintayout新功能montionLayout寫動畫小試牛刀

湯米粥發表於2020-12-31

首先看效果圖
在這裡插入圖片描述
在這裡插入圖片描述
程式碼如下

佈局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_motionlayout_scene"
    tools:context=".motionLayout.MotionLayoutMainActivity">

    <ImageView
        android:id="@+id/iv_avator"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:layout_marginTop="20dip"
        android:scaleType="fitXY"
        android:src="@drawable/pic_3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:flow_horizontalAlign="end"
        android:layout_marginTop="10dip"
        app:constraint_referenced_ids="tv1,tv2,tv3"
        app:flow_verticalGap="20dip"
        app:flow_wrapMode="aligned"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/iv_avator" />



    <TextView
        android:id="@+id/tv1"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:background="#33988f"
        android:gravity="center"
        android:text="第一個"
        android:textColor="#ffffff" />


    <TextView
        android:id="@+id/tv2"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:background="#33988f"
        android:gravity="center"
        android:text="第二個"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:background="#33988f"
        android:gravity="center"
        android:text="第三個"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/motionAnimation"
        android:layout_width="150dip"
        android:layout_height="80dip"
        android:layout_marginBottom="20dip"
        android:background="#33988f"
        android:gravity="center"
        android:text="開始Montion動畫"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

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

動畫定義程式碼

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

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="500">
        <KeyFrameSet></KeyFrameSet>
        <OnSwipe motion:clickAction="toggle" />
        <OnClick
            motion:clickAction="toggle"
            motion:targetId="@+id/motionAnimation" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/iv_avator"
            android:layout_width="120dip"
            android:layout_height="120dip"
            android:layout_marginTop="20dip"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/iv_avator"
            android:layout_width="80dip"
            android:layout_height="80dip"
            android:layout_marginStart="10dip"
            android:layout_marginTop="20dip"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />


        <Constraint
            android:id="@+id/tv1"
            android:layout_width="80dip"
            android:layout_height="80dip" />
        <Constraint
            android:id="@+id/tv2"
            android:layout_width="80dip"
            android:layout_height="80dip" />
        <Constraint
            android:id="@+id/tv3"
            android:layout_width="80dip"
            android:layout_height="80dip" />


        <Constraint
            android:id="@+id/flow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="100dip"
            android:layout_marginTop="20dip"
            motion:layout_constraintStart_toEndOf="@+id/iv_avator"
            motion:layout_constraintTop_toTopOf="parent" />

    </ConstraintSet>
</MotionScene>

相關文章