Welcome 微信登录

首页 / 操作系统 / Linux / Android开发:button在底部的多个view切换<实例二3D切换>

代码精简,结构清晰,使用了include标签,希望对大家有帮助;另外实现了3D旋转动画。建议在看实例二之前参看一下实例1。见 http://www.linuxidc.com/Linux/2011-09/43555.htm

1. 镶嵌View的主ActivityGroup

  1. package com.isomobile.widgets;  
  2.   
  3. import Android.app.ActivityGroup;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.animation.Animation;  
  8. import android.widget.Button;  
  9. import android.widget.RelativeLayout;  
  10.   
  11. public class MainActivity extends ActivityGroup implements View.OnClickListener {  
  12.     private final static Class<?>[] sActivityClasses = {  
  13.             Activity1.class, Activity2.class, Activity3.class, Activity4.class, Activity5.class  
  14.     };  
  15.   
  16.     private final static int[] sResIds = {  
  17.             R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5  
  18.     };  
  19.   
  20.     private final static String[] sActivityIds = {  
  21.             "Activity1""Activity2""Activity3""Activity4""Activity5"  
  22.     };  
  23.   
  24.     private RelativeLayout mViewContainer;  
  25.   
  26.     private Button[] mBtns = new Button[sResIds.length];  
  27.   
  28.     private View mPreView;  
  29.   
  30.     private View[] mCurView = new View[sResIds.length];  
  31.   
  32.     private int mCurId = 0;  
  33.   
  34.     private int mPreBtnPos, mCurBtnPos = 0;  
  35.   
  36.     /** Called when the activity is first created. */  
  37.     @Override  
  38.     public void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.main);  
  41.         setupViews();  
  42.     }  
  43.   
  44.     private void setupViews() {  
  45.         mViewContainer = (RelativeLayout) findViewById(R.id.container);  
  46.         final Button[] btns = mBtns;  
  47.         for (int i = 0; i < btns.length; i++) {  
  48.             btns[i] = (Button) findViewById(sResIds[i]);  
  49.             btns[i].setOnClickListener(this);  
  50.         }  
  51.   
  52.         //第一次启动时,默认跳转到第一个activity   
  53.         mCurView[0] = getLocalActivityManager().startActivity(  
  54.                 sActivityIds[0],  
  55.                 new Intent(MainActivity.this, sActivityClasses[0])  
  56.                         .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();  
  57.         mViewContainer.addView(mCurView[0]);  
  58.         mPreView = mCurView[0];  
  59.         mPreBtnPos = 0;  
  60.     }  
  61.   
  62.     @Override  
  63.     public void onClick(View v) {  
  64.         final int id = v.getId();  
  65.         if (mCurId == id) {  
  66.             return;  
  67.         }  
  68.         mCurId = id;  
  69.         processViews(id);  
  70.         onRotateAnimation(getCurButtonIndex(id));  
  71.     }  
  72.   
  73.     private void processViews(int rid) {  
  74.         mViewContainer.removeAllViews();  
  75.         mCurBtnPos = getCurButtonIndex(rid);  
  76.         mCurView[mCurBtnPos] = getLocalActivityManager().startActivity(  
  77.                 sActivityIds[mCurBtnPos],  
  78.                 new Intent(this, sActivityClasses[mCurBtnPos])  
  79.                         .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();  
  80.     }  
  81.   
  82.     private int getCurButtonIndex(int rid) {  
  83.         final int length = sResIds.length;  
  84.         for (int i = 0; i < length; i++) {  
  85.             if (rid == sResIds[i]) {  
  86.                 return i;  
  87.             }  
  88.         }  
  89.         return 0;  
  90.     }  
  91.   
  92.     public void onRotateAnimation(int index) {  
  93.         if (mPreBtnPos > mCurBtnPos) {  
  94.             Rotate3d.rightRotate(mPreView, mCurView[index], 2400300new AnimListener());  
  95.         } else {  
  96.             Rotate3d.leftRotate(mPreView, mCurView[index], 2400300new AnimListener());  
  97.         }  
  98.   
  99.         mPreView = mCurView[index];  
  100.         mViewContainer.removeAllViews();  
  101.         mViewContainer.addView(mCurView[index]);  
  102.         mPreBtnPos = mCurBtnPos;  
  103.     }  
  104.   
  105.     private class AnimListener implements Animation.AnimationListener {  
  106.   
  107.         public void onAnimationEnd(Animation animation) {  
  108.           //可以设置buttons的背景或者状态(是否可点击等)   
  109.         }  
  110.   
  111.         public void onAnimationRepeat(Animation animation) {  
  112.   
  113.         }  
  114.   
  115.         public void onAnimationStart(Animation animation) {  
  116.           //可以设置buttons的背景或者状态(是否可点击等)   
  117.         }  
  118.     }  
  119. }  

2. Rotate3d.class:

  1. package com.isomobile.widgets;  
  2.   
  3. import android.view.View;  
  4.   
  5. public class Rotate3d {  
  6.     private Rotate3d() {  
  7.     }  
  8.   
  9.     public static void leftRotate(View layoutFrom, View layoutTo, int centerX, int centerY,  
  10.             int duration, android.view.animation.Animation.AnimationListener animationListener) {  
  11.         final Animation3d fromAnimation = new Animation3d(0.0F, -90F, 0.0F, 0.0F, centerX, centerY);  
  12.         final Animation3d toAnimation = new Animation3d(90F, 0.0F, 0.0F, 0.0F, centerX, centerY);  
  13.         fromAnimation.setDuration(duration);  
  14.         toAnimation.setDuration(duration);  
  15.         toAnimation.setAnimationListener(animationListener);  
  16.         layoutFrom.startAnimation(fromAnimation);  
  17.         layoutTo.startAnimation(toAnimation);  
  18.         layoutTo.setVisibility(0);  
  19.         layoutFrom.setVisibility(8);  
  20.     }  
  21.   
  22.     public static void rightRotate(View layoutFrom, View layoutTo, int centerX, int centerY,  
  23.             int duration, android.view.animation.Animation.AnimationListener animationListener) {  
  24.         final Animation3d fromAnimation = new Animation3d(0.0F, 90F, 0.0F, 0.0F, centerX, centerY);  
  25.         final Animation3d toAnimation = new Animation3d(-90F, 0.0F, 0.0F, 0.0F, centerX, centerY);  
  26.         fromAnimation.setDuration(duration);  
  27.         toAnimation.setDuration(duration);  
  28.         toAnimation.setAnimationListener(animationListener);  
  29.         layoutFrom.startAnimation(fromAnimation);  
  30.         layoutTo.startAnimation(toAnimation);  
  31.         layoutTo.setVisibility(0);  
  32.         layoutFrom.setVisibility(8);  
  33.     }  
  34. }  

3. Animation3d.class:

  1. package com.isomobile.widgets;  
  2.   
  3. import android.graphics.Camera;  
  4. import android.graphics.Matrix;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.Transformation;  
  7.   
  8. public class Animation3d extends Animation {  
  9.     private final float mFromDegree;  
  10.   
  11.     private final float mToDegree;  
  12.   
  13.     private final float mCenterX;  
  14.   
  15.     private final float mCenterY;  
  16.   
  17.     private final float mLeft;  
  18.   
  19.     private final float mTop;  
  20.   
  21.     private Camera mCamera;  
  22.   
  23.     public Animation3d(float fromDegree, float toDegree, float left, float top, float centerX,  
  24.             float centerY) {  
  25.         mFromDegree = fromDegree;  
  26.         mToDegree = toDegree;  
  27.         mLeft = left;  
  28.         mTop = top;  
  29.         mCenterX = centerX;  
  30.         mCenterY = centerY;  
  31.     }  
  32.   
  33.     public void initialize(int width, int height, int parentWidth, int parentHeight) {  
  34.         super.initialize(width, height, parentWidth, parentHeight);  
  35.         mCamera = new Camera();  
  36.     }  
  37.   
  38.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  39.         final float FromDegree = mFromDegree;  
  40.         float degrees = FromDegree + (mToDegree - mFromDegree) * interpolatedTime;  
  41.         final float centerX = mCenterX;  
  42.         final float centerY = mCenterY;  
  43.         Matrix matrix = t.getMatrix();  
  44.         if (degrees <= -76F) {  
  45.             degrees = -90F;  
  46.             mCamera.save();  
  47.             mCamera.rotateY(degrees);  
  48.             mCamera.getMatrix(matrix);  
  49.             mCamera.restore();  
  50.         } else if (degrees >= 76F) {  
  51.             degrees = 90F;  
  52.             mCamera.save();  
  53.             mCamera.rotateY(degrees);  
  54.             mCamera.getMatrix(matrix);  
  55.             mCamera.restore();  
  56.         } else {  
  57.             mCamera.save();  
  58.             mCamera.translate(0.0F, 0.0F, centerX);  
  59.             mCamera.rotateY(degrees);  
  60.             mCamera.translate(0.0F, 0.0F, -centerX);  
  61.             mCamera.getMatrix(matrix);  
  62.             mCamera.restore();  
  63.         }  
  64.         matrix.preTranslate(-centerX, -centerY);  
  65.         matrix.postTranslate(centerX, centerY);  
  66.     }  
  67. }  
整个工程源代码下载地址见: 免费下载地址在 http://linux.linuxidc.com/用户名与密码都是www.linuxidc.com具体下载目录在 /pub/Android源码集锦/2011年/9月/Android开发:button在底部的多个view切换 实例二3D切换/ (注意将processViews里面的addView方法去掉,不小心add了2次,???此说明。)