Search code examples
androidandroid-animationandroid-motionlayout

Orchestrating Android Motion Layout animations


I have an xml layout where I would like to implement some animation when a user click a button, so i thought of using the new MotionLayout with a MotionScene. In the layout I have a background FrameLayout, which has to slide up, and a ImageView which has to appear (alpha property animation) when te user click; the problem is that first the layout should slide up and then the ImageView appear, but in a MotionScene's Transition I can only specify a starting ConstraintSet and an ending ConstraintSet but not an intermediate one; I thought of using KeyAttributes but they does not let me set the background layout's height, and if i use them with the translationY attribute at (for example) 50% percent of the animation, then will it stay as it is or not? In addition, how do you start a MotionLayout animation from java code? thanks in advance.


Solution

  • As to the question about changing the height of a view using KeyAttribute use

    <KeyPosition
      motion:keyPositionType="parentRelative"
      motion:percentHeight="1"
      motion:percentY="0"
      motion:percentX="0"
      motion:framePosition="50"
      motion:motionTarget="@id/sliding_view"/>
    

    This will pin the view to the top left corner (with percentX and percentY) and increase the height to full at 50% of the animation.

    Then to animate the image view:

    <KeyAttribute 
      motion:motionTarget="@id/image_view"
      motion:framePosition="50"
      android:alpha="0"/>
    

    To start motion scene transition from java code:

    MotionLayout motionLayout = findViewById(R.id.motion_layout)   
    motionLayout.transitionToState(R.id.state)