1.TranslateLayout.java文件(页面1) 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.ImageView;  
  7. import android.widget.RelativeLayout;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10.   
  11. public class TranslateLayout extends Activity implements OnClickListener {  
  12.   
  13.     RelativeLayout layout1;  
  14.   
  15.     String tag;  
  16.   
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.first);  
  22.         ImageView first_btn = (ImageView) findViewById(R.id.first_btn);  
  23.         first_btn.setOnClickListener(this);  
  24.   
  25.         layout1 = (RelativeLayout) findViewById(R.id.layout1);  
  26.         showView();  
  27.   
  28.     }  
  29.   
  30.     public void showView() {  
  31.         /* 取得Intent中的Bundle对象 */  
  32.         Bundle bundle = this.getIntent().getExtras();  
  33.   
  34.         if (bundle != null) {  
  35.             /* 取得Bundle对象中的数据 */  
  36.             tag = bundle.getString("second");  
  37.             System.out.println("tag =" + tag);  
  38.             if (tag.equals("Second")) {  
  39.                 rotateHelper = new RotationHelper(this,  
  40.                         Constants.KEY_FIRST_CLOCKWISE);  
  41.                 rotateHelper.applyLastRotation(layout1, -900);  
  42.             }  
  43.         }  
  44.     }  
  45.   
  46.     RotationHelper rotateHelper;  
  47.   
  48.     @Override  
  49.     public void onClick(View v) {  
  50.         // TODO Auto-generated method stub  
  51.         rotateHelper = new RotationHelper(this, Constants.KEY_FIRST_INVERSE);  
  52.         rotateHelper.applyFirstRotation(layout1, 0, -90);  
  53.     }  
  54.   
  55.     public void jumpToSecond() {  
  56.         Intent in = new Intent();  
  57.         in.setClass(this, Second.class);  
  58.         // new一个Bundle对象,并将要传递的数据传入  
  59.         Bundle bundle = new Bundle();  
  60.         bundle.putString("front""First");  
  61.         /* 将Bundle对象assign给Intent */  
  62.         in.putExtras(bundle);  
  63.         // 如果已经打开过的实例,将不会重新打开新的Activity  
  64.         // in.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  
  65.         startActivity(in);  
  66.         finish();  
  67.     }  
  68.   
  69. }  


2. Second.java文件(页面2) 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.ImageView;  
  9. import android.widget.RelativeLayout;  
  10.   
  11. public class Second extends Activity implements OnClickListener {  
  12.   
  13.     String tag = "";  
  14.   
  15.     RotationHelper rotateHelper;  
  16.   
  17.     RelativeLayout layout2;  
  18.   
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.second);  
  24.   
  25.         layout2 = (RelativeLayout) findViewById(R.id.layout2);  
  26.         showView();  
  27.         setListener();  
  28.     }  
  29.   
  30.     public void setListener() {  
  31.         ImageView second_btn = (ImageView) findViewById(R.id.second_btn);  
  32.         second_btn.setOnClickListener(this);  
  33.     }  
  34.   
  35.     @Override  
  36.     public void onClick(View v) {  
  37.         // TODO Auto-generated method stub  
  38.         rotateHelper = new RotationHelper(this,  
  39.                 Constants.KEY_SECOND_CLOCKWISE);  
  40.         rotateHelper.applyFirstRotation(layout2, 090);  
  41.     }  
  42.   
  43.     public void showView() {  
  44.         /* 取得Intent中的Bundle对象 */  
  45.         Bundle bundle = this.getIntent().getExtras();  
  46.   
  47.         if (bundle != null) {  
  48.             /* 取得Bundle对象中的数据 */  
  49.             tag = bundle.getString("front");  
  50.         }  
  51.   
  52.         System.out.println("bundle =" + tag);  
  53.   
  54.         if (tag.equals("First")) {  
  55.             rotateHelper = new RotationHelper(this, Constants.KEY_SECOND_INVERSE);  
  56.             rotateHelper.applyLastRotation(layout2, 900);  
  57.         }  
  58.     }  
  59.   
  60.     public void jumpToFirst() {  
  61.         Intent in = new Intent();  
  62.         in.setClass(this, TranslateLayout.class);  
  63.         Bundle bundle = new Bundle();  
  64.         bundle.putString("second""Second");  
  65.         in.putExtras(bundle);  
  66.         startActivity(in);  
  67.         finish();  
  68.     }  
  69.   
  70. }  

3.RotationHelper.java文件 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.util.Log;  
  5. import android.view.ViewGroup;  
  6. import android.view.animation.AccelerateInterpolator;  
  7.   
  8. public class RotationHelper {  
  9.       
  10.     DisplayNextView displayNext;  
  11.       
  12.     public RotationHelper(Activity con,int order){  
  13.         displayNext = new DisplayNextView(con, order);  
  14.     }  
  15.       
  16.     // 逆时针旋转90  
  17.     public void applyFirstRotation(ViewGroup layout,float start, float end) {  
  18.         // Find the center of the container  
  19.         final float centerX = layout.getWidth() / 2.0f;  
  20.         final float centerY = layout.getHeight() / 2.0f;  
  21.         Log.i("centerX =" + centerX, "centerX");  
  22.         Log.i("centerY =" + centerY, "centerY");  
  23.   
  24.         // Create a new 3D rotation with the supplied parameter  
  25.         // The animation listener is used to trigger the next animation  
  26.         final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,  
  27.                 centerX, centerY, 310.0f, true);  
  28.         rotation.setDuration(700);  
  29.         rotation.setFillAfter(true);  
  30.         rotation.setInterpolator(new AccelerateInterpolator());  
  31.         rotation.setAnimationListener(displayNext);  
  32.         layout.startAnimation(rotation);  
  33.     }  
  34.       
  35.     public void applyLastRotation(ViewGroup layout,float start, float end) {  
  36.         // Find the center of the container  
  37.         final float centerX = layout.getWidth() / 2.0f;  
  38.         final float centerY = layout.getHeight() / 2.0f;  
  39.         Log.i("centerX =" + centerX, "centerX");  
  40.         Log.i("centerY =" + centerY, "centerY");  
  41.   
  42.         // Create a new 3D rotation with the supplied parameter  
  43.         // The animation listener is used to trigger the next animation  
  44.         final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,  
  45.                  160192310.0f, false);  
  46.         rotation.setDuration(700);  
  47.         rotation.setFillAfter(true);  
  48.         rotation.setInterpolator(new AccelerateInterpolator());  
  49.         layout.startAnimation(rotation);  
  50.     }     
  51. }  

4. Rotate3dAnimation.java文件(动画辅助类) 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.view.animation.Animation;  
  4. import android.view.animation.Transformation;  
  5. import android.graphics.Camera;  
  6. import android.graphics.Matrix;  
  7.   
  8. /** 
  9.  * An animation that rotates the view on the Y axis between two specified angles. 
  10.  * This animation also adds a translation on the Z axis (depth) to improve the effect. 
  11.  */  
  12. public class Rotate3dAnimation extends Animation {  
  13.     private final float mFromDegrees;  
  14.     private final float mToDegrees;  
  15.     private final float mCenterX;  
  16.     private final float mCenterY;  
  17.     private final float mDepthZ;  
  18.     private final boolean mReverse;  
  19.     private Camera mCamera;  
  20.   
  21.     /** 
  22.      * Creates a new 3D rotation on the Y axis. The rotation is defined by its 
  23.      * start angle and its end angle. Both angles are in degrees. The rotation 
  24.      * is performed around a center point on the 2D space, definied by a pair 
  25.      * of X and Y coordinates, called centerX and centerY. When the animation 
  26.      * starts, a translation on the Z axis (depth) is performed. The length 
  27.      * of the translation can be specified, as well as whether the translation 
  28.      * should be reversed in time. 
  29.      * 
  30.      * @param fromDegrees the start angle of the 3D rotation 
  31.      * @param toDegrees the end angle of the 3D rotation 
  32.      * @param centerX the X center of the 3D rotation 
  33.      * @param centerY the Y center of the 3D rotation 
  34.      * @param reverse true if the translation should be reversed, false otherwise 
  35.      */  
  36.     public Rotate3dAnimation(float fromDegrees, float toDegrees,  
  37.             float centerX, float centerY, float depthZ, boolean reverse) {  
  38.         mFromDegrees = fromDegrees;  
  39.         mToDegrees = toDegrees;  
  40.         mCenterX = centerX;  
  41.         mCenterY = centerY;  
  42.         mDepthZ = depthZ;  
  43.         mReverse = reverse;  
  44.     }  
  45.   
  46.     @Override  
  47.     public void initialize(int width, int height, int parentWidth, int parentHeight) {  
  48.         super.initialize(width, height, parentWidth, parentHeight);  
  49.         mCamera = new Camera();  
  50.     }  
  51.   
  52.     @Override  
  53.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  54.         final float fromDegrees = mFromDegrees;  
  55.         float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);  
  56.   
  57.         final float centerX = mCenterX;  
  58.         final float centerY = mCenterY;  
  59.         final Camera camera = mCamera;  
  60.   
  61.         final Matrix matrix = t.getMatrix();  
  62.   
  63.         camera.save();  
  64.         if (mReverse) {  
  65.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  
  66.         } else {  
  67.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  
  68.         }  
  69.         camera.rotateY(degrees);  
  70.         camera.getMatrix(matrix);  
  71.         camera.restore();  
  72.   
  73.         matrix.preTranslate(-centerX, -centerY);  
  74.         matrix.postTranslate(centerX, centerY);  
  75.     }  
  76. }  

5.DisplayNextView.java监听动画的类 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.view.animation.Animation;  
  5.   
  6. public class DisplayNextView implements Animation.AnimationListener {  
  7.   
  8.     Object obj;  
  9.   
  10.     // 动画监听器的构造函数  
  11.     Activity ac;  
  12.     int order;  
  13.   
  14.     public DisplayNextView(Activity ac, int order) {  
  15.         this.ac = ac;  
  16.         this.order = order;  
  17.     }  
  18.   
  19.     public void onAnimationStart(Animation animation) {  
  20.     }  
  21.   
  22.     public void onAnimationEnd(Animation animation) {  
  23.         doSomethingOnEnd(order);  
  24.     }  
  25.   
  26.     public void onAnimationRepeat(Animation animation) {  
  27.     }  
  28.   
  29.     private final class SwapViews implements Runnable {  
  30.         public void run() {  
  31.             switch (order) {  
  32.             case Constants.KEY_FIRST_INVERSE:  
  33.                 ((TranslateLayout) ac).jumpToSecond();  
  34.                 break;  
  35.             case Constants.KEY_SECOND_CLOCKWISE:  
  36.                 ((Second) ac).jumpToFirst();  
  37.                 break;  
  38.             }  
  39.         }  
  40.     }  
  41.   
  42.     public void doSomethingOnEnd(int _order) {  
  43.         switch (_order) {  
  44.         case Constants.KEY_FIRST_INVERSE:  
  45.             ((TranslateLayout) ac).layout1.post(new SwapViews());  
  46.             break;  
  47.   
  48.         case Constants.KEY_SECOND_CLOCKWISE:  
  49.             ((Second) ac).layout2.post(new SwapViews());  
  50.             break;  
  51.         }  
  52.     }  
  53. }  

6. Constants标识Layout的id 
Java代码  
  1. package cn.com;  
  2.   
  3. public class Constants {  
  4.   
  5.     public final static int KEY_FIRST_INVERSE = 1;  
  6.   
  7.     public final static int KEY_FIRST_CLOCKWISE = 2;  
  8.   
  9.     public final static int KEY_SECOND_INVERSE = 3;  
  10.   
  11.     public final static int KEY_SECOND_CLOCKWISE = 4;  
  12. }  

7.first.xml文件 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:id="@+id/layout1">  
  5.     <ImageView android:layout_width="fill_parent" android:id="@+id/first_btn"  
  6.         android:layout_height="fill_parent" android:background="@drawable/first_bg" />  
  7. </RelativeLayout>  

8.second.xml文件 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:id="@+id/layout2">  
  5.   
  6.     <ImageView android:layout_width="fill_parent" android:id="@+id/second_btn"  
  7.         android:layout_height="fill_parent" android:background="@drawable/second_bg" />  
  8. </RelativeLayout>  

9.AndroidMenifest.xml文件 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.com" android:versionCode="1" android:versionName="1.0">  
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  5.         <activity android:name=".TranslateLayout" android:label="@string/app_name">  
  6.             <intent-filter>  
  7.                 <action android:name="android.intent.action.MAIN" />  
  8.                 <category android:name="android.intent.category.LAUNCHER" />  
  9.             </intent-filter>  
  10.         </activity>  
  11.         <activity android:name=".Second" />  
  12.     </application>  
  13. </manifest>   
posted on 2011-04-15 15:49  kitea  阅读(1833)  评论(0)    收藏  举报