How To Apply 3d Transition Between Two Activities In Android?
I have an android application and while switching between two activities I want to apply 3D transition... I know the method overridePendingTransition() but it does not have any ani
Solution 1:
I had used 3D Cubic Transition between activities.Credit goes to Robert Heim who is developer of this program.
Below is snippet
Activity1.java
package org.vipul;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
publicclassActivity1extendsActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
ButtonswitchActivityBtn= (Button) findViewById(R.id.bSwitchActivity);
switchActivityBtn.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
animatedStartActivity();
}
});
}
@OverrideprotectedvoidonResume() {
// animateIn this activity
ActivitySwitcher.animationIn(findViewById(R.id.container),
getWindowManager());
super.onResume();
}
privatevoidanimatedStartActivity() {
// we only animateOut this activity here.// The new activity will animateIn from its onResume() - be sure to// implement it.finalIntentintent=newIntent(getApplicationContext(),
Activity2.class);
// disable default animation for new intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
ActivitySwitcher.animationOut(findViewById(R.id.container),
getWindowManager(),
newActivitySwitcher.AnimationFinishedListener() {
@OverridepublicvoidonAnimationFinished() {
startActivity(intent);
}
});
}
}
Activity2.java
package org.vipul;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
publicclassActivity2extendsActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
ButtonswitchActivityBtn= (Button) findViewById(R.id.bSwitchActivity);
switchActivityBtn.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
animatedStartActivity();
}
});
}
@OverrideprotectedvoidonResume() {
// animateIn this activity
ActivitySwitcher.animationIn(findViewById(R.id.container),
getWindowManager());
super.onResume();
}
privatevoidanimatedStartActivity() {
// we only animateOut this activity here.// The new activity will animateIn from its onResume() - be sure to// implement it.finalIntentintent=newIntent(getApplicationContext(),
Activity1.class);
// disable default animation for new intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
ActivitySwitcher.animationOut(findViewById(R.id.container),
getWindowManager(),
newActivitySwitcher.AnimationFinishedListener() {
@OverridepublicvoidonAnimationFinished() {
startActivity(intent);
}
});
}
}
ActivitySwitcher.java
package org.vipul;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
/**
* This ActivitySwitcher uses a 3D rotation to animate an activity during its
* start or finish.
*
* see: http://blog.robert-heim.de/karriere/android-startactivity-rotate-3d-
* animation-activityswitcher/
*
* @author Robert Heim
*
*/publicclassActivitySwitcher {
privatefinalstaticintDURATION=300;
privatefinalstaticfloatDEPTH=400.0f;
/* ----------------------------------------------- */publicinterfaceAnimationFinishedListener {
/**
* Called when the animation is finished.
*/publicvoidonAnimationFinished();
}
/* ----------------------------------------------- */publicstaticvoidanimationIn(View container, WindowManager windowManager) {
animationIn(container, windowManager, null);
}
publicstaticvoidanimationIn(View container, WindowManager windowManager,
AnimationFinishedListener listener) {
apply3DRotation(90, 0, false, container, windowManager, listener);
}
publicstaticvoidanimationOut(View container, WindowManager windowManager) {
animationOut(container, windowManager, null);
}
publicstaticvoidanimationOut(View container,
WindowManager windowManager, AnimationFinishedListener listener) {
apply3DRotation(0, -90, true, container, windowManager, listener);
}
/* ----------------------------------------------- */privatestaticvoidapply3DRotation(float fromDegree, float toDegree,
boolean reverse, View container, WindowManager windowManager,
final AnimationFinishedListener listener) {
Displaydisplay= windowManager.getDefaultDisplay();
finalfloatcenterX= display.getWidth() / 2.0f;
finalfloatcenterY= display.getHeight() / 2.0f;
finalRotate3dAnimationa=newRotate3dAnimation(fromDegree, toDegree,
centerX, centerY, DEPTH, reverse);
a.reset();
a.setDuration(DURATION);
a.setFillAfter(true);
a.setInterpolator(newAccelerateInterpolator());
if (listener != null) {
a.setAnimationListener(newAnimation.AnimationListener() {
@OverridepublicvoidonAnimationStart(Animation animation) {
}
@OverridepublicvoidonAnimationRepeat(Animation animation) {
}
@OverridepublicvoidonAnimationEnd(Animation animation) {
listener.onAnimationFinished();
}
});
}
container.clearAnimation();
container.startAnimation(a);
}
}
Rotate3dAnimation.java
package org.vipul;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
/**
* An animation that 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.
*/publicclassRotate3dAnimationextendsAnimation {
privatefinalfloat mFromDegrees;
privatefinalfloat mToDegrees;
privatefinalfloat mCenterX;
privatefinalfloat mCenterY;
privatefinalfloat mDepthZ;
privatefinalboolean mReverse;
private Camera mCamera;
/**
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
* start angle and its end angle. Both angles are in degrees. The rotation
* is performed around a center point on the 2D space, definied by a pair of
* X and Y coordinates, called centerX and centerY. When the animation
* starts, a translation on the Z axis (depth) is performed. The length of
* the translation can be specified, as well as whether the translation
* should be reversed in time.
*
* @param fromDegrees
* the start angle of the 3D rotation
* @param toDegrees
* the end angle of the 3D rotation
* @param centerX
* the X center of the 3D rotation
* @param centerY
* the Y center of the 3D rotation
* @param reverse
* true if the translation should be reversed, false otherwise
*/publicRotate3dAnimation(float fromDegrees, float toDegrees, float centerX,
float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
@Overridepublicvoidinitialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = newCamera();
}
@OverrideprotectedvoidapplyTransformation(float interpolatedTime, Transformation t) {
finalfloatfromDegrees= mFromDegrees;
floatdegrees= fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);
finalfloatcenterX= mCenterX;
finalfloatcenterY= mCenterY;
finalCameracamera= mCamera;
finalMatrixmatrix= 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);
}
}
Activity1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/container"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#003300"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" /><Buttonandroid:id="@+id/bSwitchActivity"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="switch activity" /></LinearLayout>
Activity2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/container"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/bSwitchActivity"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="back" /></LinearLayout>
Manifest entries
<activityandroid:name=".Activity1"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".Activity2"android:label="Activity 2" ></activity>
Solution 2:
Here is extension of @Vipul Shah code with reverse animation functionality
add following method in to "ActivitySwitcher"
publicstaticvoidanimationInReverse(View container, WindowManager windowManager) {
animationInReverse(container, windowManager, null);
}
publicstaticvoidanimationInReverse(View container, WindowManager windowManager, AnimationFinishedListener listener) {
apply3DRotation(-90, 0, false, container, windowManager, listener);
}
publicstaticvoidanimationOutReverse(View container, WindowManager windowManager) {
animationOut(container, windowManager, null);
}
publicstaticvoidanimationOutReverse(View container, WindowManager windowManager, AnimationFinishedListener listener) {
apply3DRotation(0, 90, true, container, windowManager, listener);
}
and add this to first activity
@OverrideprotectedvoidonResume() {
if (isFromOncreate) {
isFromOncreate = false;
ActivitySwitcher.animationIn(findViewById(R.id.container), getWindowManager());
} else {
ActivitySwitcher.animationInReverse(findViewById(R.id.container), getWindowManager());
}
super.onResume();
}
Post a Comment for "How To Apply 3d Transition Between Two Activities In Android?"