android实现3D效果翻页

 

android实现3D效果翻页

分类: android   879人阅读  评论(3)  收藏  举报

        最近做了一个简单的3D效果翻页特效,先说说我的思路吧,首先我这个翻页效果并不是两个Activity之间的跳转,而是在同一个activity类切换不同的view而已。我现在的做法是单击一个button然后Gone当前的布局,然后把需要呈现的布局visible,在隐藏当前布局的时候启动动画,然后给动画添加监听,在动画结束时开始另外一个view的入场动画就行了。

       下面来看下我的主页面的布局文件:

[html]  view plain copy
 
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/container"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <include  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         layout="@layout/layout2" />  
  10.   
  11.     <include  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="fill_parent"  
  14.         layout="@layout/layout1" />  
  15.   
  16. </FrameLayout>  

我这个布局文件使用<include>标签包含了另外2个布局文件,这些布局文件才是呈现数据的,下面是另外2个布局文件:

layout1:

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/test1"  
  6.     android:orientation="vertical"   
  7.     android:id="@+id/container1"  
  8.     >  
  9.   
  10.     <Button  
  11.         android:id="@+id/bt_towhile"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_gravity="center"  
  15.         android:text="白色" />  
  16.   
  17. </LinearLayout>  

layout2:

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/container2"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="@drawable/test2"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <Button  
  10.         android:id="@+id/bt_toblack"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_gravity="center"  
  14.         android:text="黑色" />  
  15.   
  16. </LinearLayout>  


我这里只是举个例子并没有放什么实际的类容,只是放了2个button,当我点击其中一个跳转到另外一个layout。

       有了布局文件那我们就开始要实现功能了,我们的想法是点击按钮的时候开始一个动画等动画结束时再开启另外一个动画并隐藏和展示layout1和layout2。

下面是我写的一个动画工具类源码:

[java]  view plain copy
 
  1. package com.test.view;  
  2.   
  3. import android.view.View;  
  4. import android.view.ViewGroup;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.DecelerateInterpolator;  
  7.   
  8. public class RotateAnimationUtil {  
  9.   
  10.     private ViewGroup context;  
  11.   
  12.     private View[] views;  
  13.   
  14.     public RotateAnimationUtil(ViewGroup context, View... views) {  
  15.         super();  
  16.         this.context = context;  
  17.         this.views = views;  
  18.     }  
  19.   
  20.     /** 
  21.      * 应用自定义的Rotate3DAnimation动画 
  22.      *  
  23.      * @param flag 
  24.      *            当前控件的顺序坐标 
  25.      * @param startDegress 
  26.      *            开始的角度 
  27.      * @param endDegress 
  28.      *            结束的角度 
  29.      */  
  30.     public void applyRotateAnimation(int flag, float startDegress,  
  31.             float endDegress) {  
  32.         final float centerX = context.getWidth() / 2.0f;  
  33.         final float centerY = context.getHeight() / 2.0f;  
  34.   
  35.         Rotate3DAnimation rotate = new Rotate3DAnimation(startDegress,  
  36.                 endDegress, centerX, centerY, 310.0f, true);  
  37.         rotate.setDuration(1000);  
  38.         rotate.setFillAfter(false);  
  39.         rotate.setInterpolator(new DecelerateInterpolator());  
  40.   
  41.         rotate.setAnimationListener(new DisplayNextView(flag));  
  42.         context.startAnimation(rotate);  
  43.     }  
  44.   
  45.     private final class DisplayNextView implements Animation.AnimationListener {  
  46.   
  47.         private final int mFlag;  
  48.   
  49.         private DisplayNextView(int flag) {  
  50.             mFlag = flag;  
  51.         }  
  52.   
  53.         public void onAnimationStart(Animation animation) {  
  54.   
  55.         }  
  56.   
  57.         // 动画结束  
  58.   
  59.         public void onAnimationEnd(Animation animation) {  
  60.             context.post(new SwapViews(mFlag));  
  61.         }  
  62.   
  63.         public void onAnimationRepeat(Animation animation) {  
  64.   
  65.         }  
  66.     }  
  67.   
  68.     /** 
  69.      * 新开一个线程动画结束后再开始一次动画效果实现翻屏特效 
  70.      *  
  71.      * @author yangzhiqiang 
  72.      *  
  73.      */  
  74.     private final class SwapViews implements Runnable {  
  75.   
  76.         private final int mFlag;  
  77.   
  78.         public SwapViews(int mFlag) {  
  79.             this.mFlag = mFlag;  
  80.         }  
  81.   
  82.         public void run() {  
  83.             final float centerX = context.getWidth() / 2.0f;  
  84.             final float centerY = context.getHeight() / 2.0f;  
  85.             Rotate3DAnimation rotation;  
  86.             if (mFlag > -1) {  
  87.                 views[0].setVisibility(View.GONE);  
  88.                 views[1].setVisibility(View.VISIBLE);  
  89.                 views[1].requestFocus();  
  90.                 rotation = new Rotate3DAnimation(270360, centerX, centerY,  
  91.                         310.0f, false);  
  92.             } else {  
  93.                 views[1].setVisibility(View.GONE);  
  94.                 views[0].setVisibility(View.VISIBLE);  
  95.                 views[0].requestFocus();  
  96.                 rotation = new Rotate3DAnimation(900, centerX, centerY,  
  97.                         310.0f, false);  
  98.             }  
  99.             rotation.setDuration(1000);  
  100.             rotation.setFillAfter(false);  
  101.             rotation.setInterpolator(new DecelerateInterpolator());  
  102.             // 开始动画  
  103.             context.startAnimation(rotation);  
  104.   
  105.         }  
  106.   
  107.     }  
  108.   
  109. }  


我解释一下这个类的构造方法:

[java]  view plain copy
 
  1. public RotateAnimationUtil(ViewGroup context, View... views) {  
  2.         super();  
  3.         this.context = context;  
  4.         this.views = views;  
  5.     }  

有2个参数,第一个参数就是我们的主布局页面的FrameLayout,第2个参数就是我们要进行动画切换的2个子layout,我这使用的是一个可变长参数只是为了方便而已。

public void applyRotateAnimation(int flag, float startDegress,float endDegress)方法第一个参数是标记当前是第是从哪个个layout跳转,因外我们必须知道当前开始跳转的layout才能推算角度。

       下面是我自定义动画的源码:

[java]  view plain copy
 
  1. package com.test.view;  
  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 Rotate3DAnimation extends Animation {  
  9.   
  10.     private final float mFormDegress;  
  11.   
  12.     private final float mToDegress;  
  13.   
  14.     private final float mCenterX;  
  15.   
  16.     private final float mCenterY;  
  17.   
  18.     /** 
  19.      * 控制z轴的一个常量值,主要是控制动画的升降距离 
  20.      */  
  21.     private final float mDepthz;  
  22.   
  23.     /** 
  24.      * 控制z轴是像上移动还是向下移动,从而实现升降效果 
  25.      */  
  26.     private final boolean mReverse;  
  27.   
  28.     private Camera mCamera;  
  29.   
  30.     public Rotate3DAnimation(float formDegress, float toDegress, float centerX,  
  31.             float centerY, float depthz, boolean reverse) {  
  32.         super();  
  33.         this.mFormDegress = formDegress;  
  34.         this.mToDegress = toDegress;  
  35.         this.mCenterX = centerX;  
  36.         this.mCenterY = centerY;  
  37.         this.mDepthz = depthz;  
  38.         this.mReverse = reverse;  
  39.     }  
  40.   
  41.     @Override  
  42.     public void initialize(int width, int height, int parentWidth,  
  43.             int parentHeight) {  
  44.         super.initialize(width, height, parentWidth, parentHeight);  
  45.         mCamera = new Camera();  
  46.     }  
  47.   
  48.     /** 
  49.      * interpolatedTime 取值范围是0-1之间当每次,当动画启动后会系统会不停的调用applyTransformation方法, 
  50.      * 并改变interpolatedTime的值 
  51.      */  
  52.     @Override  
  53.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  54.         final float formDegress = mFormDegress;  
  55.         // 通过差点值计算出转变的角度  
  56.         float degress = formDegress  
  57.                 + ((mToDegress - formDegress) * interpolatedTime);  
  58.         final float centerX = mCenterX;  
  59.         final float centerY = mCenterY;  
  60.         final Camera camera = mCamera;  
  61.   
  62.         // 得到当前矩阵  
  63.         Matrix matrix = t.getMatrix();  
  64.         // 报错当前屏幕的状态  
  65.         camera.save();  
  66.         // 判断是降还是升  
  67.         if (mReverse) {  
  68.             // 正向改变Z轴角度  
  69.             camera.translate(0.0f, 0.0f, mDepthz * interpolatedTime);  
  70.         } else {  
  71.             // 反向改变Z轴角度  
  72.             camera.translate(0.0f, 0.0f, mDepthz * (1.0f - interpolatedTime));  
  73.         }  
  74.         // 旋转Y轴角度  
  75.         camera.rotateY(degress);  
  76.         // 把当前改变后的矩阵信息复制给Transformation的Matrix  
  77.         camera.getMatrix(matrix);  
  78.         // 根据改变后的矩阵信息从新恢复屏幕  
  79.         camera.restore();  
  80.   
  81.         // 让动画在屏幕中间运行  
  82.         matrix.preTranslate(-centerX, -centerY);  
  83.         matrix.postTranslate(centerX, centerY);  
  84.     }  
  85. }  

如果你不需要沉降效果那么你把下面的代码删除掉即可:

[java]  view plain copy
 
  1. if (mReverse) {  
  2.             // 正向改变Z轴角度  
  3.             camera.translate(0.0f, 0.0f, mDepthz * interpolatedTime);  
  4.         } else {  
  5.             // 反向改变Z轴角度  
  6.             camera.translate(0.0f, 0.0f, mDepthz * (1.0f - interpolatedTime));  
  7.         }  

好了核心代码已经上完,下面是主界面代码:

[java]  view plain copy
 
  1. package com.test.rotateanimation;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.Button;  
  9. import android.widget.FrameLayout;  
  10. import android.widget.LinearLayout;  
  11.   
  12. import com.test.view.RotateAnimationUtil;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private FrameLayout container;  
  17.   
  18.     private LinearLayout container1;  
  19.   
  20.     private LinearLayout container2;  
  21.   
  22.     private RotateAnimationUtil rotateAnimationUtil;  
  23.   
  24.     private Button bt_towhile;  
  25.   
  26.     private Button bt_toblack;  
  27.   
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.         initView();  
  33.   
  34.         bt_towhile.setOnClickListener(new View.OnClickListener() {  
  35.   
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 rotateAnimationUtil.applyRotateAnimation(1090);  
  39.             }  
  40.         });  
  41.         bt_toblack.setOnClickListener(new View.OnClickListener() {  
  42.   
  43.             @Override  
  44.             public void onClick(View v) {  
  45.                 rotateAnimationUtil.applyRotateAnimation(-10, -90);  
  46.             }  
  47.         });  
  48.   
  49.         // 设置当前View控件的缓存  
  50.         container  
  51.                 .setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);  
  52.     }  
  53.   
  54.     private void initView() {  
  55.         container = (FrameLayout) findViewById(R.id.container);  
  56.         bt_toblack = (Button) findViewById(R.id.bt_toblack);  
  57.         bt_towhile = (Button) findViewById(R.id.bt_towhile);  
  58.   
  59.         container1 = (LinearLayout) findViewById(R.id.container1);  
  60.         container2 = (LinearLayout) findViewById(R.id.container2);  
  61.   
  62.         rotateAnimationUtil = new RotateAnimationUtil(container, container1,  
  63.                 container2);  
  64.     }  
  65.   
  66.     @Override  
  67.     public boolean onCreateOptionsMenu(Menu menu) {  
  68.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  69.         return true;  
  70.     }  
  71.   
  72. }  

下面是运行效果,剪切效果不好,呵呵.

 

猜你喜欢

转载自huaonline.iteye.com/blog/1902219