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);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
ActivitySwitcher.animationOut(findViewById(R.id.container),getWindowManager(),new ActivitySwitcher.AnimationFinishedListener() {
@Override
public void onAnimationFinished() {
startActivity(intent);
}
});
}
Above R.id.container 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
ActivitySwitcher.animationIn(findViewById(R.id.container),getWindowManager());
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.reset();
a.setDuration(DURATION);
a.setFillAfter(true);
a.setInterpolator(new AccelerateInterpolator());
if (listener != null) {
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
listener.onAnimationFinished();
}
});
}
container.clearAnimation();
container.startAnimation(a);
}
}
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;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
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();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f – interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
456 total views, 2 views today
Share this On