Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MotionLayout.pdf

 MotionLayout.pdf

Avatar for SandulOleksandr

SandulOleksandr

April 25, 2019

Other Decks in Programming

Transcript

  1. <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.motion.widget.MotionLayout

    android:id="@+id/motionLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/move_from_top_to_bottom_scene" tools:showPaths="true"> <ImageView android:id="@+id/testImage" android:layout_width="100dp" android:layout_height="100dp" android:background="@color/colorPrimaryDark" android:contentDescription="@null" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.motion.widget.MotionLayout> </androidx.constraintlayout.widget.ConstraintLayout> Our test image Motion Layout
  2. MotionScene - this is files in XML folder and they

    have root tag <MotionScene> .... </MotionScene> <androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/motionLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/move_from_top_to_bottom_scene" tools:showPaths="true">
  3. <MotionScene> <Transition> <OnClick /> <OnSwipe /> <KeyFrameSet> <KeyPosition /> <KeyAttribute

    /> <KeyCycle /> </KeyFrameSet> </Transition> <ConstraintSet> <Constraint> <CustomAttribute /> </Constraint> </ConstraintSet> <StateSet> <State> <Variant /> </State> </StateSet> </MotionScene> Typical MotionScene Any variants ?
  4. <?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition android:id="@+id/motionLayout" app:constraintSetEnd="@+id/end"

    app:constraintSetStart="@+id/start" app:duration="300" app:motionInterpolator="linear"> <OnSwipe app:dragDirection="dragDown" app:touchAnchorId="@+id/testImage" app:touchAnchorSide="top" /> </Transition> <ConstraintSet android:id="@+id/start"> <Constraint android:id="@+id/testImage"> <Layout app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" motion:layout_height="100dp" motion:layout_width="100dp" /> </Constraint> </ConstraintSet> <ConstraintSet android:id="@+id/end"> <Constraint android:id="@id/testImage"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" motion:layout_height="100dp" motion:layout_width="100dp" /> </Constraint> </ConstraintSet> </MotionScene>
  5. <?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition android:id="@+id/motionLayout" app:constraintSetEnd="@+id/end"

    app:constraintSetStart="@+id/start" app:duration="300" app:motionInterpolator="linear"> <OnSwipe app:dragDirection="dragRight" app:touchAnchorId="@+id/testImage" app:touchAnchorSide="top" /> </Transition> <ConstraintSet android:id="@+id/start"> <Constraint android:id="@+id/testImage"> <Layout app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" motion:layout_height="100dp" motion:layout_width="100dp" /> </Constraint> </ConstraintSet> <ConstraintSet android:id="@+id/end"> <Constraint android:id="@id/testImage"> <Layout app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" motion:layout_height="100dp" motion:layout_width="100dp" /> </Constraint> </ConstraintSet> </MotionScene>
  6. <androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/motionLayout" app:motionDebug="SHOW_ALL" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/move_all_scene" app:showPaths="true"> <ImageView android:id="@+id/testImage1" android:layout_width="50dp"

    android:layout_height="50dp" android:background="@color/colorPrimaryDark" android:contentDescription="@null" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnRun" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Run" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> btnRun.setOnClickListener { when (motionLayout.currentState) { R.id.start -> motionLayout.transitionToEnd() R.id.end -> motionLayout.transitionToStart() } }
  7. <Transition android:id="@+id/motionLayout" motion:constraintSetEnd="@+id/end" motion:constraintSetStart="@+id/start" motion:duration="3000" motion:motionInterpolator="linear"> <Transition android:id="@+id/motionLayout" motion:constraintSetEnd="@layout/end" motion:constraintSetStart="@layout/start"

    motion:duration="3000" motion:motionInterpolator="linear"> <Transition android:id="@+id/motionLayout" motion:constraintSetEnd="@+id/end" motion:constraintSetStart="@+id/start" motion:duration="3000" motion:motionInterpolator="linear">
  8. <KeyFrameSet> <KeyPosition motion:framePosition="25" motion:keyPositionType="pathRelative" motion:motionTarget="@id/testImage1" motion:percentX="0.75" motion:percentY="-0.3" /> <KeyPosition motion:framePosition="50"

    motion:keyPositionType="pathRelative" motion:motionTarget="@id/testImage1" motion:percentY="-0.4" /> <KeyPosition motion:framePosition="75" motion:keyPositionType="pathRelative" motion:motionTarget="@id/testImage1" motion:percentX="0.25" motion:percentY="-0.3" /> </KeyFrameSet>
  9. • target: the widget the keyframe apply to • framePosition:

    from 0 to 100, when does the keyframe applies • keyPositionType: the coordinate system used, parentRelative, deltaRelative, pathRelative • percentX / percentY : the (x,y) coordinate of the position <KeyPosition> ... </KeyPosition> <KeyFrameSet> <KeyPosition motion:framePosition="25" motion:keyPositionType="pathRelative" motion:motionTarget="@id/testImage1" motion:percentX="0.75" motion:percentY="-0.3" />
  10. deltaRelative This second coordinate system addresses this exact issue, by

    being defined using the start/end positions. The coordinates express a percentage of the distance between the start and end positions.
  11. pathRelative This last coordinate system is defined as relative to

    the straight path between the start and end states
  12. <ConstraintSet android:id="@+id/start"> <Constraint android:id="@+id/testImage1"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" motion:layout_height="50dp"

    motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorPrimaryDark" /> </Constraint> <Constraint android:id="@+id/testImage2"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" app:layout_marginStart="50dp" motion:layout_height="50dp" motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorPrimaryDark" /> </Constraint> </ConstraintSet> start
  13. end <ConstraintSet android:id="@+id/end"> <Constraint android:id="@id/testImage1"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"

    motion:layout_height="50dp" motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorAccent" /> <Transform android:rotation="-75" /> </Constraint> <Constraint android:id="@id/testImage2"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" motion:layout_height="50dp" motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorAccent" /> <Transform android:rotation="55" /> </Constraint> </ConstraintSet>
  14. start vc end <ConstraintSet android:id="@+id/start"> <Constraint android:id="@+id/testImage1"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" motion:layout_height="50dp" motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorPrimaryDark" /> </Constraint> <Constraint android:id="@+id/testImage2"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" app:layout_marginStart="50dp" motion:layout_height="50dp" motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorPrimaryDark" /> </Constraint> </ConstraintSet> <ConstraintSet android:id="@+id/end"> <Constraint android:id="@id/testImage1"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" motion:layout_height="50dp" motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorAccent" /> <Transform android:rotation="-75" /> </Constraint> <Constraint android:id="@id/testImage2"> <Layout app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" motion:layout_height="50dp" motion:layout_width="50dp" /> <CustomAttribute motion:attributeName="backgroundColor" motion:customColorValue="@color/colorAccent" /> <Transform android:rotation="55" /> </Constraint> </ConstraintSet>