Camera类实现3d动画(android)
16lz
2021-01-26
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); } }); } }); }
}
更多相关文章
- android.graphics.Canvas类详解
- android 图片拖动简单例子
- Android(安卓)围绕中心旋转一个ImageView动画
- 你追我赶进度条
- Android彩虹菜单
- Android(安卓)动画效果
- Android在SurfaceView绘图
- IP地址获取
- Android(安卓)模拟键值