Camera类实现3d动画(android)

2016-03-30  本文已影响988人  逐悦

Rotate3DAnimation.java
<code>
package com.example.tdview.animation;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Rotate3DAnimation extends Animation {

private float fromDegree;
private float toDegree;
private Camera mCamera;
//动画执行中心
private float mCenterX;
private float mCenterY;
//z轴上的深度
private float mDepthZ;
//由远及近   还是  由近及远
private boolean mReverse;

public Rotate3DAnimation(float fromDegree, float toDegree, float depthZ,
        float mCenterX, float mCenterY,boolean reverse) {
    super();
    this.fromDegree = fromDegree;
    this.toDegree = toDegree;
    this.mDepthZ = depthZ;
    this.mCenterX = mCenterX;
    this.mCenterY = mCenterY;
    this.mReverse = reverse;
    System.out.println("centerX :" + mCenterX);
    System.out.println("centerY :" + mCenterY);
}

@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) {
    super.applyTransformation(interpolatedTime, t);
    
    float degrees = fromDegree + (fromDegree - toDegree)*interpolatedTime;
    mCamera.save();
    
    if(mReverse){
         //由近到远的效果
        mCamera.translate(0, 0, mDepthZ*interpolatedTime);
    }else{
        //由远及近的效果
        mCamera.translate(0, 0, mDepthZ * (1-interpolatedTime));
    }
    Matrix matrix = t.getMatrix();
    mCamera.rotateY(degrees);
    mCamera.getMatrix(matrix);
    mCamera.restore();
    
    
    //总体效果是   以mCenterX,mCenterY为中心执行
        //执行前平移
    matrix.preTranslate(-mCenterX, -mCenterY);
        //执行后平移
    matrix.postTranslate(mCenterX, mCenterY);
}}

Mainactivity.java
public class MainActivity extends ActionBarActivity {
float centerX;
float centerY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TDVIEW tdView = (TDVIEW) findViewById(R.id.tdView);
    tdView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        
        @Override
        public void onGlobalLayout() {
            tdView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            centerX = tdView.getMeasuredWidth() / 2.0f;
            centerY = tdView.getMeasuredHeight() / 2.0f;
            
            //创建动画对象
            Rotate3DAnimation r3a = new Rotate3DAnimation(0, 3600, 500, centerX, centerY,false);
            r3a.setDuration(500*60);
            r3a.setFillAfter(true);
            //设置加速插补器
            r3a.setInterpolator(new AccelerateInterpolator());
            tdView.startAnimation(r3a);
            
            //动画监听
            r3a.setAnimationListener(new AnimationListener() {
                //动画开始执行时的动作
                @Override
                public void onAnimationStart(Animation animation) {
                    
                }
                //动画重复执行
                @Override
                public void onAnimationRepeat(Animation animation) {
                    
                }
                //动画结束时的动作
                @Override
                public void onAnimationEnd(Animation animation) {
                    //再次开启一个动画
                    Rotate3DAnimation again = new Rotate3DAnimation(3600, 0, 500, centerX, centerY, true);
                    again.setDuration(500*30);
                    again.setFillAfter(true);
                    //设置减速插补器
                    again.setInterpolator(new DecelerateInterpolator());
                    tdView.startAnimation(again);
                }
            });
        }
    });
    


}

}
</code>

上一篇下一篇

猜你喜欢

热点阅读