建议在看实例二之前参看一下实例1。
package com.isomobile.widgets; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.widget.Button; import android.widget.RelativeLayout; public class MainActivity extends ActivityGroup implements View.OnClickListener { private final static Class<?>[] sActivityClasses = { Activity1.class, Activity2.class, Activity3.class, Activity4.class, Activity5.class }; private final static int[] sResIds = { R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5 }; private final static String[] sActivityIds = { "Activity1", "Activity2", "Activity3", "Activity4", "Activity5" }; private RelativeLayout mViewContainer; private Button[] mBtns = new Button[sResIds.length]; private View mPreView; private View[] mCurView = new View[sResIds.length]; private int mCurId = 0; private int mPreBtnPos, mCurBtnPos = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } private void setupViews() { mViewContainer = (RelativeLayout) findViewById(R.id.container); final Button[] btns = mBtns; for (int i = 0; i < btns.length; i++) { btns[i] = (Button) findViewById(sResIds[i]); btns[i].setOnClickListener(this); } //第一次启动时,默认跳转到第一个activity mCurView[0] = getLocalActivityManager().startActivity( sActivityIds[0], new Intent(MainActivity.this, sActivityClasses[0]) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); mViewContainer.addView(mCurView[0]); mPreView = mCurView[0]; mPreBtnPos = 0; } @Override public void onClick(View v) { final int id = v.getId(); if (mCurId == id) { return; } mCurId = id; processViews(id); onRotateAnimation(getCurButtonIndex(id)); } private void processViews(int rid) { mViewContainer.removeAllViews(); mCurBtnPos = getCurButtonIndex(rid); mCurView[mCurBtnPos] = getLocalActivityManager().startActivity( sActivityIds[mCurBtnPos], new Intent(this, sActivityClasses[mCurBtnPos]) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); } private int getCurButtonIndex(int rid) { final int length = sResIds.length; for (int i = 0; i < length; i++) { if (rid == sResIds[i]) { return i; } } return 0; } public void onRotateAnimation(int index) { if (mPreBtnPos > mCurBtnPos) { Rotate3d.rightRotate(mPreView, mCurView[index], 240, 0, 300, new AnimListener()); } else { Rotate3d.leftRotate(mPreView, mCurView[index], 240, 0, 300, new AnimListener()); } mPreView = mCurView[index]; mViewContainer.removeAllViews(); mViewContainer.addView(mCurView[index]); mPreBtnPos = mCurBtnPos; } private class AnimListener implements Animation.AnimationListener { public void onAnimationEnd(Animation animation) { //可以设置buttons的背景或者状态(是否可点击等) } public void onAnimationRepeat(Animation animation) { } public void onAnimationStart(Animation animation) { //可以设置buttons的背景或者状态(是否可点击等) } } }
package com.isomobile.widgets; import android.view.View; public class Rotate3d { private Rotate3d() { } public static void leftRotate(View layoutFrom, View layoutTo, int centerX, int centerY, int duration, android.view.animation.Animation.AnimationListener animationListener) { final Animation3d fromAnimation = new Animation3d(0.0F, -90F, 0.0F, 0.0F, centerX, centerY); final Animation3d toAnimation = new Animation3d(90F, 0.0F, 0.0F, 0.0F, centerX, centerY); fromAnimation.setDuration(duration); toAnimation.setDuration(duration); toAnimation.setAnimationListener(animationListener); layoutFrom.startAnimation(fromAnimation); layoutTo.startAnimation(toAnimation); layoutTo.setVisibility(0); layoutFrom.setVisibility(8); } public static void rightRotate(View layoutFrom, View layoutTo, int centerX, int centerY, int duration, android.view.animation.Animation.AnimationListener animationListener) { final Animation3d fromAnimation = new Animation3d(0.0F, 90F, 0.0F, 0.0F, centerX, centerY); final Animation3d toAnimation = new Animation3d(-90F, 0.0F, 0.0F, 0.0F, centerX, centerY); fromAnimation.setDuration(duration); toAnimation.setDuration(duration); toAnimation.setAnimationListener(animationListener); layoutFrom.startAnimation(fromAnimation); layoutTo.startAnimation(toAnimation); layoutTo.setVisibility(0); layoutFrom.setVisibility(8); } }
package com.isomobile.widgets; import android.graphics.Camera; import android.graphics.Matrix; import android.view.animation.Animation; import android.view.animation.Transformation; public class Animation3d extends Animation { private final float mFromDegree; private final float mToDegree; private final float mCenterX; private final float mCenterY; private final float mLeft; private final float mTop; private Camera mCamera; public Animation3d(float fromDegree, float toDegree, float left, float top, float centerX, float centerY) { mFromDegree = fromDegree; mToDegree = toDegree; mLeft = left; mTop = top; mCenterX = centerX; mCenterY = centerY; } 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 FromDegree = mFromDegree; float degrees = FromDegree + (mToDegree - mFromDegree) * interpolatedTime; final float centerX = mCenterX; final float centerY = mCenterY; Matrix matrix = t.getMatrix(); if (degrees <= -76F) { degrees = -90F; mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); } else if (degrees >= 76F) { degrees = 90F; mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); } else { mCamera.save(); mCamera.translate(0.0F, 0.0F, centerX); mCamera.rotateY(degrees); mCamera.translate(0.0F, 0.0F, -centerX); mCamera.getMatrix(matrix); mCamera.restore(); } matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }