Android Rotation Animation























Android Animation List is here.And here i worked with Rotate3dAnimation.

 Android Rotate3dAnimation  rotates the view on the Y axis between two specified angles. This animation also adds a translation on the Z axis (depth) to improve the effect.here i rotate view to 0 to 90 degrees.then view is hide and next view is display.The secondview is visible after complete of rotation of firstview.and  secondview is load using -90 to 0.then when i go back  secondview is rotate again 0 to 90 and it disappear.and firstview is load using rotate it to 90 to 0.

So,using Y axis we can rotate view.And after rotation we can hide and show view.

We have to rotate mainview,that accomplish 3d rotation.and firstview and secondview is child of mainview.those hide and show meanwhile 3dRotation.


MainActivity.java

package com.samir;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.samir.anim.DisplayNextView;
import com.samir.anim.Rotate3dAnimation;

public class MainActivity extends Activity implements OnClickListener {

Button btnRotateit, btnRotateBack;
RelativeLayout mContainer, relChild1, relChild2;
private boolean isFirstView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mContainer = (RelativeLayout) findViewById(R.id.mainlayout);
relChild1 = (RelativeLayout) findViewById(R.id.child1);
relChild2 = (RelativeLayout) findViewById(R.id.child2);

btnRotateit = (Button) findViewById(R.id.btnrotate);
btnRotateit.setOnClickListener(this);
btnRotateBack = (Button) findViewById(R.id.btnback);
btnRotateBack.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnrotate:
isFirstView = true;
applyRotation(0, 90);
break;
case R.id.btnback:
isFirstView = false;
applyRotation(0, -90);
break;
default:
break;
}

}

private void applyRotation(float start, float end) {
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;

// The animation listener is used to trigger the next animation
final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
centerX, centerY, 310.0f, true);
rotation.setDuration(1200);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstView,
mContainer, relChild1, relChild2));

mContainer.startAnimation(rotation);

}
}



DisplayNextView.java




import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;


public class DisplayNextView implements AnimationListener {
RelativeLayout mContainer, relChild1, relChild2;
boolean isFirstView;


public DisplayNextView(boolean isFirstView, RelativeLayout mContainer,
RelativeLayout relChild1, RelativeLayout relChild2) {
this.mContainer = mContainer;
this.relChild1 = relChild1;
this.relChild2 = relChild2;


this.isFirstView = isFirstView;
}


@Override
public void onAnimationEnd(Animation animation) {
mContainer.post(new SwapViews());
}


@Override
public void onAnimationRepeat(Animation animation) {
}


@Override
public void onAnimationStart(Animation animation) {
}


public class SwapViews implements Runnable {
@Override
public void run() {


final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
Rotate3dAnimation rotation;


if (isFirstView) {
rotation = new Rotate3dAnimation(-90, 0, centerX, centerY,
310.0f, false);
} else {
rotation = new Rotate3dAnimation(90, 0, centerX, centerY,
310.0f, false);
}
rotation.setDuration(1500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());


mContainer.startAnimation(rotation);


if (isFirstView) {
relChild1.setVisibility(View.GONE);
relChild2.setVisibility(View.VISIBLE);
} else {
relChild2.setVisibility(View.GONE);
relChild1.setVisibility(View.VISIBLE);
}
}
}
}




You can Download Full Source Code