7

ConstraintLayout ConstraintSet

 2 years ago
source link: https://xuyisheng.top/constraintset/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

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+下面列出的那些+CustomAttribute

组合标签:Layout、PropertySet、Transform、Motion、CustomAttribute。使用这些标签的好处是,如果不存在这些属性,则从基本布局文件中获取。如果只需要一个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.) CustomAttribute call a set"name" method via reflection Layout Attributes for the ConstraintLayout e.g. layout_constraintTop_toTopOf PropertySet currently only visibility, alpha, motionProgress,layout_constraintTag. Transform All the view transform API such as android:rotation. Motion 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 )

向大家推荐下我的网站 https://xuyisheng.top/ 专注 Android-Kotlin-Flutter 欢迎大家访问


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK