iT邦幫忙

2021 iThome 鐵人賽

DAY 26
0
Mobile Development

android studio 30天學習筆記系列 第 26

30天學習筆記 -day 26-Motion Editor(上篇)

Motion Editor是自 Android Studio 4.0 版本開始為MotionLayout佈局型別所構建的視覺化編輯氣,用於建立動畫效果。
MotionLayout是ConstraintLayout 的子類別,可以將任何的ConstraintLayout轉換成MotionLayout。

將constraintlayout轉換於motionLayout

https://ithelp.ithome.com.tw/upload/images/20210909/20138966bblbOjSDos.png

轉換結束會發現在res目錄之下有xml資料夾
https://ithelp.ithome.com.tw/upload/images/20210909/20138966PfWZm3Wp6K.png

activity_motion_scene.xml

設定使用者與ui互動會顯示的動畫,
button(id為button)被我滑動的時候,會從開始的ConstraintSet至結束的ConstraintSet所進行的動畫。

<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="1000">
      
        <!-- 動畫實現的樣式 -->
       <OnSwipe motion:dragDirection="dragUp"
        motion:touchAnchorId="@id/button"
        motion:touchAnchorSide="top"/>
    </Transition>
    <!-- 動畫開始 -->
    <ConstraintSet android:id="@+id/start">
        <Constraint
            motion:layout_constraintVertical_bias="0.877"
            android:layout_height="wrap_content"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="wrap_content"
            android:id="@+id/button" />
    </ConstraintSet>
    <!-- 動畫結束 -->
    <ConstraintSet android:id="@+id/end">
        <Constraint
            motion:layout_constraintVertical_bias="0.109"
            android:layout_height="wrap_content"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="wrap_content"
            android:id="@+id/button" />
    </ConstraintSet>
</MotionScene>

Transition裡的:
motion:constraintSetEnd="@+id/end" :表示動畫結束的布局id
motion:constraintSetStart="@id/start":表示動畫開始的布局id
motion:duration="1000" //動畫執行的時間
OnSwipe:使用者與ui互動的方法

  • motion:dragDirection="" 控制元件拉的方向(有 dragUp (向上移動)/ dragDown(向下移動) / dragRight(向右移動) / dragLeft(向左移動) )這4種
  • motion:touchAnchorId=""設定可互動的元件
  • motion:touchAnchorSide=""設定滑動區域

成果
這樣就可以完成簡單的動畫了~


上一篇
30天學習筆記 介紹-day 25-View Animation
下一篇
30天學習筆記 -day 27-Motion Editor(下篇)
系列文
android studio 30天學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言