【问题标题】:ConstraintSets in MotionLayout without copy-pasting attributesMotionLayout 中的 ConstraintSets 没有复制粘贴属性
【发布时间】:2021-09-27 03:52:17
【问题描述】:

我想将 MotionLayout 用于动画,无需任何代码,只需 XML。

当我创建一个 ConstraintSets 时,它可以正常工作,如果我使用 所有约束属性(如 layout_widthlayout_constraintEnd_toEndOf 等),但我不会更改它们。我可以使用仅在动画中真正更改的属性吗?

与此相关的另一个问题:我可以从 any 状态进行转换吗?例如,我想用三个按钮管理三个案例。我可以在六个转换中做到这一点(每个状态到其他状态),但可能只做三个?

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    简短的回答是肯定的,而且大部分情况下。

    长答案...

    在 MotionScene 中,CostraitnSets 是从布局“派生的”。 您也可以从另一个 ConstraintSet 派生。 您可以覆盖不同粒度级别的属性。

    简单

    <ConstraintSet>
    <Constraint androdi:id="@+id/a 
      motion:top_margin="23dp"
      ...
      />
    </ConstraintSet>
    

    仅简单定义的约束标签覆盖布局

    分段:

    <ConstraintSet>
    <Constraint androdi:id="@+id/a">
      <Layout motion:top_margin="23dp" ...\>
      <PropertySet android:alpha="0.3" ...\>
      <Transform android:scaleX="1.3" ...\>
      <Motion motion:transitionEasing="linear ...\>
     
      </ConstraintSet>
    </ConstraintSet>
    

    如果你有一个部分,所有布局标签都被替换,那么只有部分被替换

    约束覆盖:

    <ConstraintSet>
    <ConstraintOVerride androdi:id="@+id/a" 
          motion:top_margin="23dp" 
          android:alpha="0.3"...\>
     
      </ConstraintSet>
    </ConstraintSet>
    

    个别属性已更改,但您不能使用“锚属性”,例如 layout_constraintTop_ToTopOf

    第二个问题 中等答案:您可以转换任何状态 通常情况下,过渡是往返的

     <Transition
            motion:constraintSetEnd="@+id/end"
            motion:constraintSetStart="@id/start"
            motion:duration="6000">
            <OnClick motion:targetId="@+id/button1" />
    

    您可以省略运动:constraintSetStart。

     <Transition
            motion:constraintSetEnd="@+id/end"
            motion:duration="6000">
            <OnClick motion:targetId="@+id/button1" />
    

    这意味着点击时可以从任何状态进入此状态。即使在状态之间的转换! 你可以看到这样的代码here

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多