Monday, November 8, 2010

How to apply animations when activity enters & exits in framework.??

Every time when an activity is started / entered first time, or when an activity is exited, a simple animation is performed by the framework.Currently framework performs simple fade-in and fade-out animations.

What if we like to apply our own animations when activity screen is first launched.?

It is possible. I will point out the places where you need to modify to apply you animations.

1 - Through xml

If you like to apply animations through xml then write your animation logics in these files.


These animations are given style item names in styles.xml

<style name="Animation.Activity">
<item name="activityOpenEnterAnimation">@anim/activity_open_enter</item>
<item name="activityOpenExitAnimation">@anim/activity_open_exit</item>
<item name="activityCloseEnterAnimation">@anim/activity_close_enter</item>
<item name="activityCloseExitAnimation">@anim/activity_close_exit</item>

These names are referred in frameworks while picking the resource id for animations.

2 - Custom animation class.


This file in services of framework is where animation is set and performs the animations for activity enter & exit.
There are two places animations are set one for activities and one for windows, where activity view is attached. Parent of all views.

private boolean applyAnimationLocked(AppWindowToken wtoken,
WindowManager.LayoutParams lp, int transit, boolean enter) --> Activity

private boolean applyAnimationLocked(WindowState win,
int transit, boolean isEntrance) ----> Windows

I added logs in both places and found that in applyAnimationLocked for activity

// Only apply an animation if the display isn't frozen. If it is
// frozen, there is no reason to animate and it can cause strange
// artifacts when we unfreeze the display if some different animation
// is running.

It dint enter into the main 'if' to set the animations. I think it is because of the above said reason.!!

So i set the animation for main window itself in the other 'applyAnimationLocked' for windows.

For testing, i took the class provided in android-sdk samples under API-Demos folder.

Create a instant of this class and set the animation.

Added the below code at line no 2739

Display display = ((WindowManager)mContext.getSystemService(mContext.WINDOW_SERVICE)).getDefaultDisplay();

int width = display.getWidth();
int height = display.getHeight();

final float centerX = width.getWidth() / 2.0f;
final float centerY = height.getHeight() / 2.0f;

// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
final Rotate3dAnimation rotation =
new Rotate3dAnimation(0, 0, centerX, centerY, 310.0f, true);
a = rotation;

win.mAnimationIsEntrance = isEntrance;

This animation will be applied to all switch cases check,,which is window enter, window exit.. etc..

switch (transit) {
case WindowManagerPolicy.TRANSIT_ENTER:
attr =;
case WindowManagerPolicy.TRANSIT_EXIT:
attr =;
case WindowManagerPolicy.TRANSIT_SHOW:
attr =;
case WindowManagerPolicy.TRANSIT_HIDE:
attr =;

Now you can observe animations are applied to all types of dialog, error notes, all activitie..!!

By this you can define your own animation extending 'Animation', it can be even 3D animation and can be applied to activity enter and exit... !!

Hope it helps someone who works in framework and trying to apply animation when activity enters....!!


  1. Hi, Nice post. I'm looking for a place to disable the animation and I think i found it in your blog. thanks for sharing.

  2. Hello Mani! Great Post! :p