開局三連,MotionLayout 是什么?,MotionLayout 是做什么的呢????,MotionLayout 怎么用呢???
1. MotionLayout 是什么呢???
MotionLayout 是ConstraintLayout 2.0 版本以后新增的一個繼承于ConstraintLayout的一個新布局
2. MotionLayout 是做什么的呢???
MotionLayout
是一種布局類型,可幫助您管理應用中的運動和微件動畫。MotionLayout
是 ConstraintLayout
的子類,在其豐富的布局功能基礎之上構建而成。作為 ConstraintLayout
庫的一部分,MotionLayout
可用作支持庫,并可向后兼容 API 級別 14。
3. MotionLayout 怎么用呢???
MotionLayout 在ConstraintLayout 2.0 包下,所以直接引用ConstraintLayout 2.0的包就可以直接因引用了
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
具體什么時候用到MotionLayout呢 ?
MotionLayout提供了對其直屬的子View提供各種變換功能,所以當我們需要控制頁面多個子控件伴隨動效的時候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="1000">
<!-- 觸摸屬性 -->
<OnSwipe
motion:dragDirection="dragDown"
motion:touchAnchorId="@+id/button"
motion:touchAnchorSide="bottom" />
</Transition>
<!-- 是定義描述您的運動的各種限制條件的位置 -->
<!-- 開始的View限制 -->
<ConstraintSet android:id="@+id/start">
<!-- 條件限制 -->
<Constraint
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="64dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent">
<!-- 自定義條件限制,backgroundColor:方法名,customColorValue:參數類型 并且必須與具有
getter 和 setter 方法的對象匹配-->
<!-- 設置背景色 -->
<CustomAttribute
motion:attributeName="backgroundColor"
motion:customColorValue="#D81B60" />
<!-- 設置文字-->
<CustomAttribute
motion:attributeName="text"
motion:customStringValue="開始" />
</Constraint>
</ConstraintSet>
<!-- 結束的View限制 -->
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="64dp"
android:layout_marginBottom="8dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent">
<CustomAttribute
motion:attributeName="backgroundColor"
motion:customColorValue="#9999FF" />
<CustomAttribute
motion:attributeName="text"
motion:customStringValue="結束" />
</Constraint>
</ConstraintSet>
</MotionScene>
下邊介紹下MotionLayout用到的重點方法以及類
-
MotionLayout
:翻譯而來叫運動布局,繼承于constraintlayout- layoutDescription: MotionLayout 的屬性,引用一個 MotionScene資源文件來描述這個運動的狀態
- MotionScene: 是一個 XML 資源文件,其中包含相應布局的所有運動描述
-
ConstraintSet: 是定義描述您的運動的各種限制條件的位置
- Constraint :添加各種數據行對View進行限制
- CustomAttribute: 自定義屬性 必須與getter() 和 setter ()方法的對象匹配
- attributeName: 屬性名字
- customStringValue: 參數數據
包含方法:
從以下支持的類型中選擇:
motion:customColorValue 適用于顏色
motion:customIntegerValue 適用于整數
motion:customFloatValue 適用于浮點值
motion:customStringValue 適用于字符串
motion:customDimension 適用于尺寸
motion:customBoolean 適用于布爾值
- CustomAttribute: 自定義屬性 必須與getter() 和 setter ()方法的對象匹配
- Constraint :添加各種數據行對View進行限制
-
Transition: 包含運動的基本定義
- motion:constraintSetStart:指的是運動開始的端點
- motion:constraintSetEnd:指的是運動結束的端點
- OnSwipe:可讓您通過輕觸控制運動
motion:touchAnchorId 指的是您可以滑動并拖動的視圖。
motion:touchAnchorSide 表示我們從右側拖動視圖
motion:dragDirection 表示拖動的進度方向
motion:dragDirection="dragRight" 表示當您向右拖動時,進度會增加
-
總結
MotionLayout 整合了Android動畫操作,讓開發者可以處理一些相對復雜的動畫頁面,效果很棒,且最低兼容到API 14 .所以以后的時間,我會一點點的學習這個新控件,爭取寫成個系列