Android Transition Framework: Getting Started
In this tutorial, you’ll learn how to animate your UI with Android Transition Framework. By Zahidur Rahman Faisal.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Android Transition Framework: Getting Started
15 mins
- Switching Activities with Shared Elements
- Tracking Animation States with Lifecycle Callbacks
- Animating Transitions Between Fragments
- Creating the Image Viewer
- Sharing Transition Elements Between Fragments
- Scene Transitions
- Creating Scenes
- Switching Scenes
- Creating Custom Transitions
- Applying Custom Transitions
- Where To Go From Here?
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:
- 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.
- Takes the progress value of the progress bar and save it to TransitionValues instance internal Map along with
TRANSITION_PROPERTY
. - Stores the starting state using
captureStartValues()
. - 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:
- Declare
uploadTime
for three seconds andstatusInterval
for 600 milliseconds. CountDownTimer will fire itsonTick()
function five times before going toonFinish()
. -
onTick()
checks whetheruploadProgress
reached 100%. If not, it increasesuploadProgress
by 20% and callsTransitionManager.beginDelayedTransition()
to apply your customProgressBarTransition
onsceneContainer
. -
onFinish()
fires after three seconds. ChangeuploadStatus
text to Upload complete! and then add a confirmation message by callingaddItemData()
andshowAddItemConfirmation()
.
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:
- Deep-diving Android Animations
- Mastering Property Animation
- Getting started with Android MotionLayout
- Getting started with Android Design Support Library
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!