Animations in upday

By Ahmed Khalil

github.com/r4md4c

What are we trying to achieve?

ConstraintLayout to the rescue!

<!-- collapsed_layout.xml -->

<android.support.constraint.ConstraintLayout
       android:layout_height="match_parent"
       android:id="@+id/body_layout"
       android:layout_width="match_parent">
 <ImageView
           android:id="@+id/hero_image"
           android:scaleType="centerCrop"
           android:layout_height="192dp"/>
      
 <FrameLayout 
   android:layout_width="0dp"
   android:layout_height="0dp"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"            
   app:layout_constraintBottom_toBottomOf="@+id/hero_image">

       <ViewStub
           android:id="@+id/viewStub"            
           android:layout="@layout/explore_stories_content"
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>

 </FrameLayout>
   <include
           android:layout_width="0dp"
           layout="@layout/article_content"
           android:layout_height="0dp" />
<android.support.design.widget.FloatingActionButton
           android:id="@+id/deepdive_fab"
           app:layout_constraintTop_toBottomOf="@+id/hero_image"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintBottom_toBottomOf="@+id/hero_image" />
<!-- expanded_layout.xml -->

<android.support.constraint.ConstraintLayout
       android:layout_height="match_parent"
       android:id="@+id/body_layout"
       android:layout_width="match_parent">
 <ImageView
           android:id="@+id/hero_image"
           android:layout_width="match_parent"
           android:scaleType="centerCrop"
           android:layout_height="372dp"/>
  <FrameLayout android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="@+id/hero_image">

       <include
               android:id="@+id/viewStub"
               layout="@layout/explore_stories_content"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />

  </FrameLayout>
   <include
           android:layout_width="0dp"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintRight_toRightOf="parent"
           layout="@layout/article_content_expanded"
           android:layout_height="0dp"/>
    <android.support.design.widget.FloatingActionButton
           android:id="@+id/deepdive_fab"
           app:layout_constraintTop_toBottomOf="@+id/hero_image"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintBottom_toBottomOf="@+id/hero_image"/>
... />
<!-- expanded_layout.xml -->

<android.support.constraint.ConstraintLayout
       android:layout_height="match_parent"
       android:id="@+id/body_layout"
       android:layout_width="match_parent">

   <ImageView
           android:id="@+id/hero_image"
           android:scaleType="centerCrop"
           android:layout_height="372dp"/>

   <FrameLayout android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="@+id/hero_image">

       <include
               android:id="@+id/viewStub"
               layout="@layout/explore_stories_content"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />

   </FrameLayout>

   <include
           android:layout_width="0dp"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintRight_toRightOf="parent"
           layout="@layout/article_content_expanded"
           android:layout_height="0dp"/>

   <android.support.design.widget.FloatingActionButton
           android:id="@+id/deepdive_fab"
           app:layout_constraintTop_toBottomOf="@+id/hero_image"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintBottom_toBottomOf="@+id/hero_image"/>
... />

            
            
            
<!-- collapsed_layout.xml -->

<android.support.constraint.ConstraintLayout
       android:layout_height="match_parent"
       android:id="@+id/body_layout"
       android:layout_width="match_parent">
    
       <ImageView
           android:id="@+id/hero_image"
           android:scaleType="centerCrop"
           android:layout_height="192dp" />
        
       <FrameLayout 
           android:layout_width="0dp"
           android:layout_height="0dp"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintRight_toRightOf="parent"            
           app:layout_constraintBottom_toBottomOf="@+id/hero_image">
    
           <include
               android:id="@+id/viewStub"
               layout="@layout/explore_stories_content"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />
    
       </FrameLayout>
    
       <include
           android:layout_width="0dp"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintRight_toRightOf="parent"
           layout="@layout/article_content"
           android:layout_height="0dp" />
    
       <android.support.design.widget.FloatingActionButton
           android:id="@+id/deepdive_fab"
           app:layout_constraintTop_toBottomOf="@+id/hero_image"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintBottom_toBottomOf="@+id/hero_image" />
.../>

ConstraintSet

  • Applies
    • LayoutParams (including constraints)
    • Transformations
    • Visibility
  • Most important APIs
    • Cloning
      • void clone (ConstraintLayout constraintLayout)
      • void clone (Context context, @LayoutResId int constraintLayoutId)
    • Applying Constraints
      • void applyTo (ConstraintLayout constraintLayout)
private val nonExpandedStateConstraintSet by lazy {   
      ConstraintSet() 
}

private val expandedStateConstraintSet by lazy {         
       ConstraintSet() 
}

// Initialize both of the ConstraintSets

nonExpandedStateConstraintSet.clone(bodyLayout)

expandedStateConstraintSet.clone(context, R.layout.expanded_layout)
// When clicking the FAB

private fun applyConstraintSets(expand: Boolean) {
   if (expand) {
       expandedStateConstraintSet.applyTo(bodyLayout)
   } else {
       nonExpandedStateConstraintSet.applyTo(bodyLayout)
   }
}

TransitionManager

private val nonExpandedStateConstraintSet by lazy {   
      ConstraintSet() 
}

private val expandedStateConstraintSet by lazy {         
       ConstraintSet() 
}

// Initialize both of the ConstraintSets

nonExpandedStateConstraintSet.clone(bodyLayout)

expandedStateConstraintSet.clone(context, R.layout.expanded_layout)
// When clicking the Fab

private fun applyConstraintSets(expand: Boolean) {
   if (expand) {
       expandedStateConstraintSet.applyTo(bodyLayout)
   } else {
       nonExpandedStateConstraintSet.applyTo(bodyLayout)
   }
   // Magic Starts Here!
   TransitionManager.beginDelayedTransition(bodyLayout)
}

AutoTransition

  • Fade out
  • ChangeBounds
  • Fade In

Custom Transitions

Transitions Everywhwere

compile "com.andkulikov:transitionseverywhere:1.7.6"

ChangeBounds

Gifs taken from: https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50

ChangeImageTransform

Gif taken from: https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50

Recolor

Gifs taken from: https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50

private val nonExpandedStateConstraintSet by lazy {   
      ConstraintSet() 
}

private val expandedStateConstraintSet by lazy {         
       ConstraintSet() 
}

// Initialize both of the ConstraintSets

nonExpandedStateConstraintSet.clone(bodyLayout)

expandedStateConstraintSet.clone(context, R.layout.expanded_layout)
// When clicking the Fab

private fun applyConstraintSets(expand: Boolean) {
   if (expand) {
       expandedStateConstraintSet.applyTo(bodyLayout)
   } else {
       nonExpandedStateConstraintSet.applyTo(bodyLayout)
   }
   TransitionInflater.from(context)
      .inflateTransition(R.transition.custom_transition).apply {
           TransitionManager.beginDelayedTransition(bodyLayout, this)
   }
}
<!-- custom_transition.xml -->

<?xml version="1.0" encoding="utf-8"?>
<transitionSet
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:transitionOrdering="together"
        app:duration="600">
    <transitionSet>
            <changeBounds/>
            <changeImageTransform/>
            <targets>
                <target app:targetId="@+id/hero_image"/>
            </targets>
    </transitionSet
    <changeBounds>
        <targets>
            <target app:excludeId="@+id/hero_image"/>
        </targets>
    </changeBounds>
     <recolor>
            <targets>
                <target app:targetId="@+id/preview_text"/>
                <target app:targetId="@+id/hero_text"/>
            </targets>
     </recolor>
    <fade>
        <targets>
            <target app:targetId="@+id/category_text"/>
        </targets>
    </fade>
    <transitionSet>
        <fade/>
        <slide/>
        <targets>
            <target app:targetId="@+id/exploreStoriesContent"/>
        </targets>
    </transitionSet>

... />
<!-- custom_transition.xml -->

<?xml version="1.0" encoding="utf-8"?>
<transitionSet
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:transitionOrdering="together"
        app:duration="600">

    <transitionSet>
        <changeBounds/>
        <changeImageTransform/>
        <targets>
            <target app:targetId="@+id/hero_image"/>
        </targets>
    </transitionSet>

    <changeBounds>
        <targets>
            <target app:excludeId="@+id/hero_image"/>
        </targets>
    </changeBounds>

    <recolor>
        <targets>
            <target app:targetId="@+id/preview_text"/>
            <target app:targetId="@+id/hero_text"/>
        </targets>
    </recolor>

    <fade>
        <targets>
            <target app:targetId="@+id/category_text"/>
        </targets>
    </fade>

    <transitionSet>
        <fade/>
        <slide/>
        <targets>
            <target app:targetId="@+id/exploreStoriesContent"/>
        </targets>
    </transitionSet>

</transitionSet>

Questions?

Thank you

Made with Slides.com