通过
ConstraintSet
动态修改约束,实现动画效果。
创建
val constraintSet = ConstraintSet()
复制
//1
constraintSet.clone(context: Context, constraintLayoutId: Int)
//2
constraintSet.clone(set: ConstraintSet)
//3
constraintSet.clone(constraintLayout: ConstraintLayout)
//4
constraintSet.clone(constraints: Constraints)
注意:
ConstraintLayout
的子View
需要设置id
。
All children of ConstraintLayout must have ids to use ConstraintSet.
修改
属性
//相对自身 支持 ConstraintSet.WRAP_CONTENT、MATCH_CONSTRAINT
constrainWidth(int viewId, int width)
constrainHeight(int viewId, int height)
//相对父布局
constrainPercentWidth(int viewId, float percent)
constrainPercentHeight(int viewId, float percent)
constrainDefaultWidth/constrainDefaultHeight
constrainMaxWidth/constrainMaxHeight
constrainMinWidth/constrainMinHeight
setVisibility - 设置显隐
setAlpha - 设置透明度
setRotation/setRotationX/setRotationY - 设置旋转角度
setScaleX/setScaleY - 设置缩放比例
setTransformPivot/setTransformPivotX/setTransformPivotY - 设置变换中心点
setTranslation/setTranslationX/setTranslationY/setTranslationZ - 设置偏移量
setElevation - 设置阴影
约束
connect
center
centerHorizontally
centerVertically
constrainCircle - 设置圆周约束
createHorizontalChain/createHorizontalChainRtl/createVerticalChain - 设置水平/垂直链
addToHorizontalChain/addToHorizontalChainRtl/addToVerticalChain - 加入水平/垂直链
removeFromHorizontalChain/removeFromHorizontalChainRtl/removeFromoVerticalChain - 从水平/垂直链移除
setHorizontalChainStyle/setVerticalChainStyle - 设置水平/垂直链的类型
setHorizontalBias/setVerticalBias - 设置水平/垂直偏移
setHorizontalWeight/setVerticalWeight - 设置水平/垂直权重
setMargin/setGoneMargin - 设置间隔和约束对象消失后的间隔
setGuidelineBegin/setGuidelineEnd/setGuidelinePercent - 设置参考线的相对位置
setDimensionRatio - 设置宽高比
clear - 清理约束
//id
ConstraintSet.PARENT_ID
//side
ConstraintSet.TOP
ConstraintSet.BOTTOM
ConstraintSet.LEFT
ConstraintSet.RIGHT
ConstraintSet.START
ConstraintSet.END
ConstraintSet.BASELINE
应用
TransitionManager.beginDelayedTransition(constraintLayout: ConstraintLayout)
constraintSet.applyTo(constraintLayout: ConstraintLayout)