Android实现3D翻转动画效果-创新互联

Android中并没有提供直接做3D翻转的动画,所以关于3D翻转的动画效果需要我们自己实现,那么我们首先来分析一下Animation 和 Transformation。

创新互联建站主营海城网站建设的网络公司,主营网站建设方案,APP应用开发,海城h5小程序设计搭建,海城网站营销推广欢迎海城等地区企业咨询

Animation动画的主要接口,其中主要定义了动画的一些属性比如开始时间,持续时间,是否重复播放等等。而Transformation中则包含一个矩阵和alpha值,矩阵是用来做平移,旋转和缩放动画的,而alpha值是用来做alpha动画的,要实现3D旋转动画我们需要继承自Animation类来实现,我们需要重载getTransformation和applyTransformation,在getTransformation中Animation会根据动画的属性来产生一系列的差值点,然后将这些差值点传给applyTransformation,这个函数将根据这些点来生成不同的Transformation。下面是

具体实现:


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); 
  } 
} 

本文标题:Android实现3D翻转动画效果-创新互联
文章来源:http://hbruida.cn/article/cohspe.html