Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android滑動效果進階篇(五) 3D旋轉

Android滑動效果進階篇(五) 3D旋轉

編輯:Android開發教程

前面介紹了利用Android自帶的控件,進行滑動翻頁制作效果,現在我們通過代碼實現一些滑動翻頁的動畫效果。

Animation實現動畫有兩個方式:幀動畫(frame-by-frame animation)和補間動畫(tweened animation)

本示 例通過繼承Animation自定義Rotate3D,實現3D翻頁效果。效果圖如下:

1、Rotate3D(Animation)

首先,自定義Animation的3D動畫類Rotate3D

public class Rotate3D extends 

Animation {     
    private float fromDegree;   // 旋轉起始角度     
    private float toDegree;     // 旋轉終止角度     
    private float mCenterX;     // 旋轉中心x     
    private float mCenterY;     // 旋轉中心y     
    private Camera mCamera;     

    public Rotate3D(float fromDegree, float toDegree, float centerX, float centerY) {     
        this.fromDegree = fromDegree;     
        this.toDegree = toDegree;     
        this.mCenterX = centerX;     
        this.mCenterY = centerY;     
         
    }     

    @Override 
    public void initialize(int width, int height, int parentWidth, int parentHeight) {     
        super.initialize(width, height, parentWidth, parentHeight);     
        mCamera = new Camera();     
    }     

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) {     
        final float FromDegree = fromDegree;     
        float degrees = FromDegree + (toDegree - fromDegree) * interpolatedTime;    // 旋轉角度(angle)     
        final float centerX = mCenterX;     
        final float centerY = mCenterY;     
        final Matrix matrix = t.getMatrix();     

        if (degrees <= -76.0f) {     
            degrees = -90.0f;     
            mCamera.save();     
            mCamera.rotateY(degrees);       // 旋轉     
            mCamera.getMatrix(matrix);     
            mCamera.restore();     
        } else if (degrees >= 76.0f) {     
            degrees = 90.0f;     
            mCamera.save();     
            mCamera.rotateY(degrees);     
            mCamera.getMatrix(matrix);     
            mCamera.restore();     
        } else {     
            mCamera.save();     
            mCamera.translate(0, 0, centerX);       // 位移x     
            mCamera.rotateY(degrees);     
            mCamera.translate(0, 0, -centerX);     
            mCamera.getMatrix(matrix);     
            mCamera.restore();     
        }     

        matrix.preTranslate(-centerX, -centerY);     
        matrix.postTranslate(centerX, centerY);     
    }     
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved