3D翻轉效果介紹,Android動畫分析

Android動畫分析 - 3D翻轉效果Android動畫分析 - 3D翻轉效果

android中的翻轉動畫效果的實現,首先看一下運行效果如上圖所示。,更多android面試題.. html

Android中並無提供直接作3D翻轉的動畫,因此關於3D翻轉的動畫效果須要咱們本身實現,那麼咱們首先來分析一下Animation 和 Transformation。 java

Animation動畫的主要接口,其中主要定義了動畫的一些屬性好比開始時間,持續時間,是否重複播放等等。而Transformation中則包含一個矩陣和alpha值,矩陣是用來作平移,旋轉和縮放動畫的,而alpha值是用來作alpha動畫的,要實現3D旋轉動畫咱們須要繼承自Animation類來實現,咱們須要重載getTransformation和applyTransformation,在getTransformation中Animation會根據動畫的屬性來產生一系列的差值點,而後將這些差值點傳給applyTransformation,這個函數將根據這些點來生成不一樣的Transformation。下面是具體實現: android

package com.example.textviewtest;
 
 import android.graphics.Camera;
 import android.graphics.Matrix;
 import android.view.animation.Animation;
 import android.view.animation.Transformation;
 
 public class Rotate3dAnimation extends Animation {
     // 開始角度
     private final float mFromDegrees;
     // 結束角度
     private final float mToDegrees;
     // 中心點
     private final float mCenterX;
     private final float mCenterY;
     private final float mDepthZ;
     // 是否須要扭曲
     private final boolean mReverse;
     // 攝像頭
     private Camera mCamera;
 
     public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX,
             float centerY, float depthZ, boolean reverse) {
         mFromDegrees = fromDegrees;
         mToDegrees = toDegrees;
         mCenterX = centerX;
         mCenterY = centerY;
         mDepthZ = depthZ;
         mReverse = reverse;
     }
 
     @Override
     public void initialize(int width, int height, int parentWidth,
             int parentHeight) {
         super.initialize(width, height, parentWidth, parentHeight);
         mCamera = new Camera();
     }
 
     // 生成Transformation
     @Override
     protected void applyTransformation(float interpolatedTime, Transformation t) {
         final float fromDegrees = mFromDegrees;
         // 生成中間角度
         float degrees = fromDegrees
                 + ((mToDegrees - fromDegrees) * interpolatedTime);
 
         final float centerX = mCenterX;
         final float centerY = mCenterY;
         final Camera camera = mCamera;
 
         final Matrix matrix = t.getMatrix();
 
         camera.save();
         if (mReverse) {
             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
         } else {
             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
         }
         camera.rotateY(degrees);
         // 取得變換後的矩陣
         camera.getMatrix(matrix);
         camera.restore();
 
         matrix.preTranslate(-centerX, -centerY);
         matrix.postTranslate(centerX, centerY);
     }
 }

其中包括了旋轉的開始和結束角度,中心點、是否扭曲、和一個Camera,這裏咱們主要分析applyTransformation函數,其中第一個參數就是經過getTransformation函數傳遞的差指點,而後咱們根據這個差值經過線性差值算法計算出一箇中間角度degrees,Camera類是用來實現繞Y軸旋轉後透視投影的,所以咱們首先經過t.getMatrix()取得當前的矩陣,而後經過camera.translate來對矩陣進行平移變換操做,camera.rotateY進行旋轉。這樣咱們就能夠很輕鬆的實現3D旋轉效果了。 面試

下面是佈局文件main.xml: 算法

 

MainActivity的代碼以下: app

package com.example.textviewtest;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.View;
 import android.view.animation.AccelerateInterpolator;
 import android.view.animation.Animation;
 import android.view.animation.DecelerateInterpolator;
 import android.widget.Button;
 import android.widget.TextView;
 
 public class MainActivity extends Activity {
     private TextView tv;
     private Button btn;
     private int count = 1;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         tv = (TextView) findViewById(R.id.tv);
         tv.setText(String.valueOf(count));
         btn = (Button) findViewById(R.id.next_btn);
         applyRotation(0, 90);
 
         btn.setOnClickListener(new View.OnClickListener() {
 
             @Override
             public void onClick(View v) {
                 applyRotation(0, 90);
             }
         });
 
     }
 
     private void applyRotation(float start, float end) {
         // 計算中心點
         final float centerX = tv.getWidth() / 2.0f;
         final float centerY = tv.getHeight() / 2.0f;
 
         final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
                 centerX, centerY, 310.0f, true);
         rotation.setDuration(500);
         rotation.setFillAfter(true);
         rotation.setInterpolator(new AccelerateInterpolator());
         // 設置監聽
         rotation.setAnimationListener(new DisplayNextView());
 
         tv.startAnimation(rotation);
     }
 
     private final class DisplayNextView implements Animation.AnimationListener {
 
         public void onAnimationStart(Animation animation) {
         }
 
         // 動畫結束
         public void onAnimationEnd(Animation animation) {
             tv.post(new SwapViews());
         }
 
         public void onAnimationRepeat(Animation animation) {
         }
     }
 
     private final class SwapViews implements Runnable {
 
         public void run() {
             final float centerX = tv.getWidth() / 2.0f;
             final float centerY = tv.getHeight() / 2.0f;
             Rotate3dAnimation rotation = null;
 
             tv.requestFocus();
 
             rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f,
                     false);
             rotation.setDuration(500);
             rotation.setFillAfter(true);
             rotation.setInterpolator(new DecelerateInterpolator());
             // 開始動畫
             tv.startAnimation(rotation);
             tv.setText(String.valueOf(count++));
         }
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return true;
     }
 
 }
相關文章
相關標籤/搜索