点击上方蓝字关注我,知识会给你力量
熟读约束三百遍,不会使用也会吹。
https://developer.android.com/reference/androidx/constraintlayout/motion/widget/MotionLayout
MotionLayout的约束有几种写法,这个问题跟茴香豆的茴有几种写法还真不太一样。
在MotionLayout中,约束可以写在三个地方。
❝默认情况下,所有的约束都来自于Layout,这也是IDE中,默认显示的Source是Layout的原因。 ❞
这几种写法各有利弊,首先,写在MotionLayout中,会被State中的布局约束覆盖,但是写在Scene中,每个State都要重复写大量的约束,写在单独的CL布局中,则无法预览,所以,成年人的世界,就是要你全都会,结合不同的使用场景,需要用不同的约束策略。
前面说了,在Scene的ConstraintSet中,你需要对每个发生变化的元素创建Constraint,而且一旦你创建了这个元素的Constraint,那么Layout中的这个元素的所有约束都将失效,例如你修改了width,即使height没有修改,也需要重写。
Constraint位于ConstraintSet标签内部,用于描述当前的约束行为,你可以把它当作是一个简化的ConstraintLayout,可以在其内部创建约束,但更好的做法是通过layout、motion、transform等标签来对约束进行分类,从而可以更好的理清约束和动画的关系。
所以,当你不想重写所有属性的修改时,可以使用Sectioned Constraints,它有五个实现:
借助它们,可以少写无关的重复属性,简化代码。
ConstraintSet可以从另一个ConstraintSet中派生,这样可以优化约束的复用,利用deriveConstraintsFrom属性,你可以指定另一个ConstraintSet的id进行引用,然后在其中修改需要覆盖的属性。
这个标签是2.1新增的一个属性,也存在于ConstraintSet标签下,于Constraint同级。它包含了Constraint所有的属性,除了layout_constraintXX_toXXOf之类提供锚点信息的属性。利用这个属性,可以很方便的针对某些非锚点信息的变化的属性进行修改。
Constraint的标签支持两种形式。
属性列表:
属性 | 含义 |
---|---|
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 |
属性 | 含义 |
---|---|
[ConstraintLayout attributes] | see for attribute |
属性 | 含义 |
---|---|
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 |
属性 | 含义 |
---|---|
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 |
属性 | 含义 |
---|---|
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 |
属性 | 含义 |
---|---|
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 欢迎大家访问