Rotate3DAnimation.java

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);}@Overridepublic void initialize(int width, int height, int parentWidth,        int parentHeight) {    super.initialize(width, height, parentWidth, parentHeight);    mCamera = new Camera();}@Overrideprotected 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);                }            });        }    });    }

}

更多相关文章

  1. android.graphics.Canvas类详解
  2. android 图片拖动简单例子
  3. Android(安卓)围绕中心旋转一个ImageView动画
  4. 你追我赶进度条
  5. Android彩虹菜单
  6. Android(安卓)动画效果
  7. Android在SurfaceView绘图
  8. IP地址获取
  9. Android(安卓)模拟键值

随机推荐

  1. Android兼容性优化-Android(安卓)8.0设置
  2. android 文件存储注意点
  3. Android(安卓)Bitmap与String互转
  4. 屏幕方向android:screenOrientation
  5. 介绍两个Android开源项目:Android显示GIF
  6. Android(安卓)O 版本(Android(安卓)8.0) 存
  7. Android:自定义view实现动画
  8. 在服务器上使用 gradle 打包 android 源
  9. Android(安卓)PopupWindow工具类 (已解决
  10. 对Android应用进行单元测试