Android开发:Layout之间3D切换效果Demo


先上个效果图(跟自己Blog中的浏览图片的代码类似,不过是layout之间切换)

相信这个效果很多人都需要,现在共享在这里供大家学习:

1.Layout3D.java

  1. package cn.com;  
  2.   
  3. import Android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.Button;  
  8.   
  9. public class Layout3D extends Activity {  
  10.   
  11.     private int mCenterX = 160;  
  12.     private int mCenterY = 0;  
  13.     private ViewGroup layout1;  
  14.     private ViewGroup layout2;  
  15.   
  16.     private Rotate3d leftAnimation;  
  17.     private Rotate3d rightAnimation;  
  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.main);  
  24.   
  25.         initFirst();  
  26.   
  27.         layout1 = (ViewGroup) findViewById(R.id.layout1);  
  28.         Button b1 = (Button) findViewById(R.id.button1);  
  29.         b1.setEnabled(true);  
  30.         b1.setOnClickListener(new Button.OnClickListener() {  
  31.             public void onClick(View v) {  
  32.                 leftMoveHandle();  
  33.                 v.setEnabled(false);  
  34.             }  
  35.         });  
  36.     }  
  37.       
  38.     public void initFirst(){  
  39.         leftAnimation = new Rotate3d(0, -900.0f, 0.0f, mCenterX, mCenterY);  
  40.         rightAnimation = new Rotate3d(9000.0f, 0.0f, mCenterX, mCenterY);  
  41.         leftAnimation.setFillAfter(true);  
  42.         leftAnimation.setDuration(1000);  
  43.         rightAnimation.setFillAfter(true);  
  44.         rightAnimation.setDuration(1000);  
  45.     }  
  46.       
  47.     public void initSecond(){  
  48.         leftAnimation = new Rotate3d(-9000.0f, 0.0f, mCenterX, mCenterY);  
  49.         rightAnimation = new Rotate3d(0900.0f, 0.0f, mCenterX, mCenterY);  
  50.         leftAnimation.setFillAfter(true);  
  51.         leftAnimation.setDuration(1000);  
  52.         rightAnimation.setFillAfter(true);  
  53.         rightAnimation.setDuration(1000);  
  54.     }  
  55.   
  56.     public void jumpToLayout1(Rotate3d leftAnimation) {  
  57.         setContentView(R.layout.main);  
  58.   
  59.         layout1 = (ViewGroup) findViewById(R.id.layout1);  
  60.         layout1.startAnimation(leftAnimation);  
  61.   
  62.         Button b1 = (Button) findViewById(R.id.button1);  
  63.         b1.setEnabled(true);  
  64.         b1.setOnClickListener(new Button.OnClickListener() {  
  65.             public void onClick(View v) {  
  66.                 leftMoveHandle();  
  67.             }  
  68.         });  
  69.     }  
  70.   
  71.     public void jumpToLayout2(Rotate3d rightAnimation) {  
  72.         setContentView(R.layout.mylayout);  
  73.         layout2 = (ViewGroup) findViewById(R.id.layout2);  
  74.         layout2.startAnimation(rightAnimation);  
  75.   
  76.         Button b2 = (Button) findViewById(R.id.button2);  
  77.         b2.setEnabled(true);  
  78.         b2.setOnClickListener(new Button.OnClickListener() {  
  79.             public void onClick(View v) {  
  80.                 rightMoveHandle();  
  81.             }  
  82.         });  
  83.     }  
  84.   
  85.     public void leftMoveHandle() {  
  86.         initFirst();  
  87.         layout1.startAnimation(leftAnimation);  
  88.         jumpToLayout2(rightAnimation);  
  89.     }  
  90.   
  91.     public void rightMoveHandle() {  
  92.         initSecond();  
  93.         layout2.startAnimation(rightAnimation);  
  94.         jumpToLayout1(leftAnimation);  
  95.     }  
  96. }  

Rotate3d.java

  1. package cn.com;  
  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 Rotate3d extends Animation {  
  9.     private float mFromDegree;  
  10.     private float mToDegree;  
  11.     private float mCenterX;  
  12.     private float mCenterY;  
  13.     private float mLeft;  
  14.     private float mTop;  
  15.     private Camera mCamera;  
  16.     private static final String TAG = "Rotate3d";  
  17.   
  18.     public Rotate3d(float fromDegree, float toDegree, float left, float top,  
  19.             float centerX, float centerY) {  
  20.         this.mFromDegree = fromDegree;  
  21.         this.mToDegree = toDegree;  
  22.         this.mLeft = left;  
  23.         this.mTop = top;  
  24.         this.mCenterX = centerX;  
  25.         this.mCenterY = centerY;  
  26.   
  27.     }  
  28.   
  29.     @Override  
  30.     public void initialize(int width, int height, int parentWidth,  
  31.             int parentHeight) {  
  32.         super.initialize(width, height, parentWidth, parentHeight);  
  33.         mCamera = new Camera();  
  34.     }  
  35.   
  36.     @Override  
  37.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  38.         final float FromDegree = mFromDegree;  
  39.         float degrees = FromDegree + (mToDegree - mFromDegree)  
  40.                 * interpolatedTime;  
  41.         final float centerX = mCenterX;  
  42.         final float centerY = mCenterY;  
  43.         final Matrix matrix = t.getMatrix();  
  44.   
  45.         if (degrees <= -76.0f) {  
  46.             degrees = -90.0f;  
  47.             mCamera.save();  
  48.             mCamera.rotateY(degrees);  
  49.             mCamera.getMatrix(matrix);  
  50.             mCamera.restore();  
  51.         } else if (degrees >= 76.0f) {  
  52.             degrees = 90.0f;  
  53.             mCamera.save();  
  54.             mCamera.rotateY(degrees);  
  55.             mCamera.getMatrix(matrix);  
  56.             mCamera.restore();  
  57.         } else {  
  58.             mCamera.save();  
  59.             //   
  60.             mCamera.translate(00, centerX);  
  61.             mCamera.rotateY(degrees);  
  62.             mCamera.translate(00, -centerX);  
  63.             mCamera.getMatrix(matrix);  
  64.             mCamera.restore();  
  65.         }  
  66.   
  67.         matrix.preTranslate(-centerX, -centerY);  
  68.         matrix.postTranslate(centerX, centerY);  
  69.     }  
  70. }  

3.main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout android:layout_width="fill_parent"  
  3.     android:id="@+id/layout1" android:layout_height="fill_parent"  
  4.     android:background="@drawable/black" xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <Button android:id="@+id/button1" android:layout_width="118px"  
  6.         android:layout_height="wrap_content" android:text="Go to Layout2">  
  7.     </Button>  
  8.     <TextView android:id="@+id/text1" android:textSize="24sp"  
  9.         android:layout_width="186px" android:layout_height="29px"  
  10.         android:text="@string/layout1" android:layout_below="@+id/button1"></TextView>  
  11. </RelativeLayout>  

mylayout.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout android:layout_width="fill_parent"  
  3.     android:id="@+id/layout2" android:layout_height="fill_parent"  
  4.     android:background="@drawable/white" xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <Button android:id="@+id/button2" android:layout_width="118px"  
  6.         android:layout_height="wrap_content" android:text="Go to Layout1">  
  7.     </Button>  
  8.     <TextView android:id="@+id/text2" android:textSize="24sp"  
  9.         android:layout_width="186px" android:layout_height="29px"  
  10.         android:textColor="@drawable/black" android:text="@string/layout2"  
  11.         android:layout_below="@+id/button2">  
  12.     </TextView>  
  13. </RelativeLayout>  

color.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   <resources>  
  3.     <drawable name="black">#000000</drawable>  
  4.     <drawable name="white">#FFFFFFFF</drawable>  
  5.   </resources>  

相关内容