ConstraintLayout2.0一篇寫不完之約束到底寫在哪

點擊上方藍字關注我,知識會給你力量

熟讀約束三百遍,不會使用也會吹。
https://developer.android.com/reference/androidx/constraintlayout/motion/widget/MotionLayout
約束到底寫在哪
MotionLayout的約束有幾種寫法,這個問題跟茴香豆的茴有幾種寫法還真不太一樣。
在MotionLayout中,約束可以寫在三個地方。
直接寫在MotionLayout中:如果布局簡單,那么可以直接寫在MotionLayout中,這樣Scene中的約束會直接繼承Layout。 寫在Scene中:這是官方推薦的做法,不同的State對應不同的ConstraintSet。 寫在單獨的CL中:Transition的State不僅僅可以使用ConstraintSet,也可以使用單獨的ConstraintLayout布局文件。
?默認情況下,所有的約束都來自于Layout,這也是IDE中,默認顯示的Source是Layout的原因。
?
這幾種寫法各有利弊,首先,寫在MotionLayout中,會被State中的布局約束覆蓋,但是寫在Scene中,每個State都要重復寫大量的約束,寫在單獨的CL布局中,則無法預覽,所以,成年人的世界,就是要你全都會,結(jié)合不同的使用場景,需要用不同的約束策略。
Sectioned Constraints
前面說了,在Scene的ConstraintSet中,你需要對每個發(fā)生變化的元素創(chuàng)建Constraint,而且一旦你創(chuàng)建了這個元素的Constraint,那么Layout中的這個元素的所有約束都將失效,例如你修改了width,即使height沒有修改,也需要重寫。
Constraint位于ConstraintSet標簽內(nèi)部,用于描述當前的約束行為,你可以把它當作是一個簡化的ConstraintLayout,可以在其內(nèi)部創(chuàng)建約束,但更好的做法是通過layout、motion、transform等標簽來對約束進行分類,從而可以更好的理清約束和動畫的關系。
所以,當你不想重寫所有屬性的修改時,可以使用Sectioned Constraints,它有五個實現(xiàn):
Layout:這里面你可以修改和布局相關的狀態(tài)變更,例如布局約束和尺寸 Transform:這里你可以修改Transform相關的狀態(tài)變更,例如rotation、translationX等 PropertySet:這里你可以修改View的屬性,例如Visibility Motion:這里你可以修改跟MotionLayout相關的屬性,例如pathMotionArc、TransitionEasing等 CustomAttribute:這里你可以修改你設置的CustomAttribute
借助它們,可以少寫無關的重復屬性,簡化代碼。
Derived Constraints
ConstraintSet可以從另一個ConstraintSet中派生,這樣可以優(yōu)化約束的復用,利用deriveConstraintsFrom屬性,你可以指定另一個ConstraintSet的id進行引用,然后在其中修改需要覆蓋的屬性。
ConstraintOverride
這個標簽是2.1新增的一個屬性,也存在于ConstraintSet標簽下,于Constraint同級。它包含了Constraint所有的屬性,除了layout_constraintXX_toXXOf之類提供錨點信息的屬性。利用這個屬性,可以很方便的針對某些非錨點信息的變化的屬性進行修改。
Constraint Tags
Constraint的標簽支持兩種形式。
所有的ConstraintLayout+下面列出的那些+
組合標簽:
、 、 、 、 。使用這些標簽的好處是,如果不存在這些屬性,則從基本布局文件中獲取。如果只需要一個Motion標簽,這就可以省去復制所有的布局標簽。如果使用了這些標簽,那么基礎文件中的所有布局屬性都會被覆蓋。
屬性列表:
| 屬性 | 含義 |
|---|---|
| android:id | Id of the View |
| [ConstraintLayout attributes] | Any attribute that is part of ContraintLayout layout is allowed |
| [Standard View attributes] | A collection of view attributes supported by the system (see below) |
| transitionEasing | define an easing curve to be used when animating from this point (e.g. curve(1.0,0,0,1.0)) or key words {standard | accelerate | decelerate | linear } |
| pathMotionArc | the path will move in arc (quarter eclipses) or key words {startVertical | startHorizontal | none } |
| transitionPathRotate | (float) rotate object relative to path taken |
| drawPath | draw the path the layout will animate animate |
| progress | call method setProgress(float) on this view (used to talk to nested ConstraintLayouts etc.) |
| call a set"name" method via reflection | |
| Attributes for the ConstraintLayout e.g. layout_constraintTop_toTopOf | |
| currently only visibility, alpha, motionProgress,layout_constraintTag. | |
| All the view transform API such as android:rotation. | |
| Motion Layout control commands such as transitionEasing and pathMotionArc |
Layout
| 屬性 | 含義 |
|---|---|
| [ConstraintLayout attributes] | see for attribute |
PropertySet
| 屬性 | 含義 |
|---|---|
| visibility | set the Visibility of the view. One of Visible, invisible or gone |
| alpha | setAlpha value |
| motionProgress | using reflection call setProgress |
| layout_constraintTag | a tagging string to identify the type of object |
Transform
| 屬性 | 含義 |
|---|---|
| android:elevation | base z depth of the view. |
| android:rotation | rotation of the view, in degrees. |
| android:rotationX | rotation of the view around the x axis, in degrees. |
| android:rotationY | rotation of the view around the y axis, in degrees. |
| android:scaleX | scale of the view in the x direction |
| android:scaleY | scale of the view in the y direction. |
| android:translationX | translation in x of the view. This value is added post-layout to the left property of the view, which is set by its layout. |
| android:translationY | translation in y of the view. This value is added post-layout to the top property of the view, which is set by its layout |
| android:translationZ | translation in z of the view. This value is added to its elevation. |
| app:transformPivotTarget | transform target |
| android:transformPivotX | transform pivot x |
| android:transformPivotY | transform pivot y |
Motion
| 屬性 | 含義 |
|---|---|
| transitionEasing | Defines an acceleration curve. |
| pathMotionArc | Says the object should move in a quarter ellipse unless the motion is vertical or horizontal |
| motionPathRotate | set the rotation to the path of the object + this angle. |
| drawPath | Debugging utility to draw the motion of the path |
| animateCircleAngleTo | Animate with circle |
| animateRelativeTo | Animate relative to target |
| quantizeMotionSteps | quantize animation with steps |
CustomAttribute
| 屬性 | 含義 |
|---|---|
| attributeName | The name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...) |
| customColorValue | The value is a color looking setMyAttr(int ) |
| customIntegerValue | The value is an integer looking setMyAttr(int ) |
| customFloatValue | The value is a float looking setMyAttr(float ) |
| customStringValue | The value is a String looking setMyAttr(String ) |
| customDimension | The value is a dimension looking setMyAttr(float ) |
| customBoolean | The value is true or false looking setMyAttr(boolean ) |
向大家推薦下我的網(wǎng)站 https://xuyisheng.top/ 點擊原文一鍵直達
專注 Android-Kotlin-Flutter 歡迎大家訪問
往期推薦
更文不易,點個“三連”支持一下??
