Adding rotation animation when switch from one activity to other | Techbirds

Here I am going to show that how on a click of a button my page changes after a complete rotation.

Steps :

1. on click of button add this method :

private void animatedStartActivity() {
             final Intent intent = new Intent(getApplicationContext(),Activity2.class);
             ActivitySwitcher.animationOut(findViewById(,getWindowManager(),new                ActivitySwitcher.AnimationFinishedListener() {
                                          public void onAnimationFinished() {

Above is the id of Layout and ActivitySwitcher is a class which is implemented in step3.

2. In onResume() be sure to add following line because the new activity will animateIn from its Resume


3. ActivitySwitcher :

public class ActivitySwitcher {

    private final static int DURATION = 300;
    private final static float DEPTH = 400.0f;

    public interface AnimationFinishedListener {
        public void onAnimationFinished();

    public static void animationIn(View container, WindowManager windowManager) {
        animationIn(container, windowManager, null);

    public static void animationIn(View container, WindowManager windowManager,
            AnimationFinishedListener listener) {
                                 apply3DRotation(90, 0, false, container, windowManager, listener);

    public static void animationOut(View container, WindowManager windowManager) {
        animationOut(container, windowManager, null);

    public static void animationOut(View container,
            WindowManager windowManager, AnimationFinishedListener listener) {
                            apply3DRotation(0, -90, true, container, windowManager, listener);

    private static void apply3DRotation(float fromDegree, float toDegree,
            boolean reverse, View container, WindowManager windowManager,
            final AnimationFinishedListener listener) {

        Display display = windowManager.getDefaultDisplay();
        final float centerX = display.getWidth() / 2.0f;
        final float centerY = display.getHeight() / 2.0f;

        final Rotate3dAnimation a = new Rotate3dAnimation(fromDegree, toDegree,
                centerX, centerY, DEPTH, reverse);
        a.setInterpolator(new AccelerateInterpolator());
        if (listener != null) {
            a.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {

                public void onAnimationRepeat(Animation animation) {

                public void onAnimationEnd(Animation animation) {

4. In above class we used a class Rotate3dAnimation which is implemented as :

public class Rotate3dAnimation extends Animation {
    private final float mFromDegrees;
    private final float mToDegrees;
    private final float mCenterX;
    private final float mCenterY;
    private final float mDepthZ;
    private final boolean mReverse;
    private Camera mCamera;

    public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX,
            float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;

    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();

    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final float fromDegrees = mFromDegrees;
        float degrees = fromDegrees
                + ((mToDegrees – fromDegrees) * interpolatedTime);

        final float centerX = mCenterX;
        final float centerY = mCenterY;
        final Camera camera = mCamera;

        final Matrix matrix = t.getMatrix();;
        if (mReverse) {
            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
        } else {
            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f – interpolatedTime));

        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);

456 total views, 2 views today

Share this Onfacebook-6465815twitter-8301722linkedin-3254106google-9098914