首页 文章详情

ConstraintLayout2.0一篇写不完之约束到底写在哪

Android群英传 | 365 2021-06-03 20:29 0 0 0
UniSMS (合一短信)

点击上方蓝字关注我,知识会给你力量

熟读约束三百遍,不会使用也会吹。

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布局中,则无法预览,所以,成年人的世界,就是要你全都会,结合不同的使用场景,需要用不同的约束策略。

Sectioned Constraints

前面说了,在Scene的ConstraintSet中,你需要对每个发生变化的元素创建Constraint,而且一旦你创建了这个元素的Constraint,那么Layout中的这个元素的所有约束都将失效,例如你修改了width,即使height没有修改,也需要重写。

Constraint位于ConstraintSet标签内部,用于描述当前的约束行为,你可以把它当作是一个简化的ConstraintLayout,可以在其内部创建约束,但更好的做法是通过layout、motion、transform等标签来对约束进行分类,从而可以更好的理清约束和动画的关系。

所以,当你不想重写所有属性的修改时,可以使用Sectioned Constraints,它有五个实现:

  • Layout:这里面你可以修改和布局相关的状态变更,例如布局约束和尺寸
  • Transform:这里你可以修改Transform相关的状态变更,例如rotation、translationX等
  • PropertySet:这里你可以修改View的属性,例如Visibility
  • Motion:这里你可以修改跟MotionLayout相关的属性,例如pathMotionArc、TransitionEasing等
  • CustomAttribute:这里你可以修改你设置的CustomAttribute

借助它们,可以少写无关的重复属性,简化代码。

Derived Constraints

ConstraintSet可以从另一个ConstraintSet中派生,这样可以优化约束的复用,利用deriveConstraintsFrom属性,你可以指定另一个ConstraintSet的id进行引用,然后在其中修改需要覆盖的属性。

ConstraintOverride

这个标签是2.1新增的一个属性,也存在于ConstraintSet标签下,于Constraint同级。它包含了Constraint所有的属性,除了layout_constraintXX_toXXOf之类提供锚点信息的属性。利用这个属性,可以很方便的针对某些非锚点信息的变化的属性进行修改。

Constraint Tags

Constraint的标签支持两种形式。

  • 所有的ConstraintLayout+下面列出的那些+

  • 组合标签:。使用这些标签的好处是,如果不存在这些属性,则从基本布局文件中获取。如果只需要一个Motion标签,这就可以省去复制所有的布局标签。如果使用了这些标签,那么基础文件中的所有布局属性都会被覆盖。

属性列表:

属性含义
android:idId 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)
transitionEasingdefine 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 }
pathMotionArcthe path will move in arc (quarter eclipses) or key words {startVertical | startHorizontal | none }
transitionPathRotate(float) rotate object relative to path taken
drawPathdraw the path the layout will animate animate
progresscall 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

属性含义
visibilityset the Visibility of the view. One of Visible, invisible or gone
alphasetAlpha value
motionProgressusing reflection call setProgress
layout_constraintTaga tagging string to identify the type of object

Transform

属性含义
android:elevationbase z depth of the view.
android:rotationrotation of the view, in degrees.
android:rotationXrotation of the view around the x axis, in degrees.
android:rotationYrotation of the view around the y axis, in degrees.
android:scaleXscale of the view in the x direction
android:scaleYscale of the view in the y direction.
android:translationXtranslation 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:translationYtranslation 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:translationZtranslation in z of the view. This value is added to its elevation.
app:transformPivotTargettransform target
android:transformPivotXtransform pivot x
android:transformPivotYtransform pivot y

Motion

属性含义
transitionEasingDefines an acceleration curve.
pathMotionArcSays the object should move in a quarter ellipse unless the motion is vertical or horizontal
motionPathRotateset the rotation to the path of the object + this angle.
drawPathDebugging utility to draw the motion of the path
animateCircleAngleToAnimate with circle
animateRelativeToAnimate relative to target
quantizeMotionStepsquantize animation with steps

CustomAttribute

属性含义
attributeNameThe name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...)
customColorValueThe value is a color looking setMyAttr(int )
customIntegerValueThe value is an integer looking setMyAttr(int )
customFloatValueThe value is a float looking setMyAttr(float )
customStringValueThe value is a String looking setMyAttr(String )
customDimensionThe value is a dimension looking setMyAttr(float )
customBooleanThe value is true or false looking setMyAttr(boolean )

向大家推荐下我的网站 https://xuyisheng.top/  点击原文一键直达

专注 Android-Kotlin-Flutter 欢迎大家访问



往期推荐


本文原创公众号:群英传,授权转载请联系微信(Tomcat_xu),授权后,请在原创发表24小时后转载。
< END >
作者:徐宜生

更文不易,点个“三连”支持一下👇

good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter