Android Transition Framework: Getting Started

In this tutorial, you’ll learn how to animate your UI with Android Transition Framework. By Zahidur Rahman Faisal.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Creating Custom Transitions

You can create you own transition animation, simulating an upload process in AddItemActivity.

To create a transition, you need to extend Transition class from Android Transition Framework and manipulate your animation.

To do that, you’ll customize the default progress animation of ProgressBar.

Create a new class named ProgressBarTransition inside the util package and add following code:

package com.raywenderlich.isell.util

import android.widget.ProgressBar
import android.view.ViewGroup
import android.animation.Animator
import android.animation.ObjectAnimator
import android.view.animation.DecelerateInterpolator
import android.support.transition.Transition
import android.support.transition.TransitionValues

class ProgressBarTransition : Transition() {

  val PROGRESSBAR_PROPERTY = "progress"
  val TRANSITION_PROPERTY = "ProgressBarTransition:progress"

  // 1
  override fun createAnimator(sceneRoot: ViewGroup, startValues: TransitionValues?,
                              endValues: TransitionValues?): Animator? {
    if (startValues != null && endValues != null && endValues.view is ProgressBar) {
      val progressBar = endValues.view as ProgressBar
      val startValue = startValues.values[TRANSITION_PROPERTY] as Int
      val endValue = endValues.values[TRANSITION_PROPERTY] as Int
      if (startValue != endValue) {
        // 2
        val objectAnimator = ObjectAnimator
                .ofInt(progressBar, PROGRESSBAR_PROPERTY, startValue, endValue)
        objectAnimator.interpolator = DecelerateInterpolator()

        return objectAnimator
      }
    }

    return null
  }
  
  // 3
  private fun captureValues(transitionValues: TransitionValues) {
    if (transitionValues.view is ProgressBar) {
      // Save current progress in the transitionValues Map
      val progressBar = transitionValues.view as ProgressBar
      transitionValues.values[TRANSITION_PROPERTY] = progressBar.progress
    }
  }

  // 4
  override fun captureStartValues(transitionValues: TransitionValues) {
    captureValues(transitionValues)
  }

  // 5
  override fun captureEndValues(transitionValues: TransitionValues) {
    captureValues(transitionValues)
  }

}

In the first part of the code above, the important arguments are startValues and endValues as TransitionValues. A TransitionValues instance holds information about the View and a Map with properties and current values from the view.

The arguments startValues and endValues contain the value for a property name modified in this transition. If there are startValues and endValues and the view type is ProgressBar, then the code extracts the values and puts them into startValue and endValue variables, respectively.

Here’s what the remaining code does:

  1. Creates an ObjectAnimator to animate changes of the progress property from ProgressBar. Set a DecelerateInterpolator to reflect the hustle of uploading data during the progress animation.
  2. Takes the progress value of the progress bar and save it to TransitionValues instance internal Map along with TRANSITION_PROPERTY.
  3. Stores the starting state using captureStartValues().
  4. Stores the end state of the transition using captureEndValues().

Applying Custom Transitions

Now, you’ll apply your custom transition while simulating the upload process. Open AddItemActivity and import ProgressBarTransition:

import com.raywenderlich.isell.util.ProgressBarTransition

Then, replace everything inside onClickAddItem():

fun onClickAddItem(view: View) {
  if (hasValidInput()) {
    // Scene Transition
    val uploadScene: Scene = Scene
            .getSceneForLayout(sceneContainer, R.layout.scene_upload, this)
    TransitionManager.go(uploadScene, Fade())

    // 1
    val uploadTime: Long = 3000
    val statusInterval: Long = 600

    object : CountDownTimer(uploadTime, statusInterval) {
      val maxProgress = 100
      var uploadProgress = 0

      // 2
      override fun onTick(millisUntilFinished: Long) {
        if (uploadProgress < maxProgress) {
          uploadProgress += 20
          TransitionManager
              .beginDelayedTransition(sceneContainer, ProgressBarTransition())
          progressBar.progress = uploadProgress
        }
      }

      // 3
      override fun onFinish() {
        uploadStatus.text = getString(R.string.text_uploaded)
        addItemData()
        showAddItemConfirmation()
      }
    }.start()
  }
}

Here’s what’s happening in the code above:

  1. Declare uploadTime for three seconds and statusInterval for 600 milliseconds. CountDownTimer will fire its onTick() function five times before going to onFinish().
  2. onTick() checks whether uploadProgress reached 100%. If not, it increases uploadProgress by 20% and calls TransitionManager.beginDelayedTransition() to apply your custom ProgressBarTransition on sceneContainer.
  3. onFinish() fires after three seconds. Change uploadStatus text to Upload complete! and then add a confirmation message by calling addItemData() and showAddItemConfirmation().

Build and run now, and try adding another item:

Congratulations! You've mastered the art of transitions and turned a simple app into an awesome one!

Where To Go From Here?

Get the final project by clicking the Download Materials button at the top or bottom of this tutorial.

Keep your users excited with the magic of Android animations. Here are some additional resources to explore:

I hope learning Android Transition Framework helps you create amazing user experiences for your next app. If you have questions or comments, please join the forum discussion and comment below!