您的位置:首页 > 其它

使用Camera实现三维动画

2015-04-04 12:57 148 查看
package test.example.lxm.test;

import android.app.Activity;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//获取屏幕大小
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
int windowWidth = displayMetrics.widthPixels;
int windowHeight = displayMetrics.heightPixels;

//开始动画
int centerX = windowWidth /2;
int centerY = windowHeight/2;
MyAnimation myAnimation = new MyAnimation(centerX,centerY,4000);
ImageView iv = (ImageView)findViewById(R.id.iv_test);
iv.startAnimation(myAnimation);

}

class MyAnimation extends Animation{

//中心点
int centerX;
int centerY;

int duration;

//进行三维变动
Camera camera = new Camera();
//各方向的位移变化量
final float TRANS_X = 100f;
final float TRANS_Y = -150f;
final float TRANS_Z = 80f;
//旋转度数
final int ROTATE_X = 360;
final int ROTATE_Y = 360;
final int ROTATE_Z = 360;

public MyAnimation(int centerX,int centerY, int duration){
this.centerX = centerX;
this.centerY = centerY;
this.duration = duration;
}

/**
* @param width Width of the object being animated
* @param height Height of the object being animated
* @param parentWidth Width of the animated object's parent
* @param parentHeight Height of the animated object's parent
*/
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width,height,parentWidth,parentHeight);
setDuration(duration); //设置持续时间
setFillAfter(true); //设置保留动画结果
setInterpolator(new LinearInterpolator());
}

/**
* 设置动画在每一进度所做的事情
* @param interpolatedTime 动画行进时间比(如:动画进行到一半,interpolatedTime=0.5即1/2)
* @param t 变化
*/
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime,t);
camera.save();
//位移
camera.translate(TRANS_X - TRANS_X * interpolatedTime,TRANS_Y - TRANS_Y*interpolatedTime,
TRANS_Z-TRANS_Z*interpolatedTime);
//旋转
camera.rotateX(ROTATE_X *interpolatedTime );
camera.rotateY(ROTATE_Y * interpolatedTime);
camera.rotateZ(ROTATE_Z * interpolatedTime);
//应用
Matrix matrix = t.getMatrix();
camera.getMatrix(matrix);
//位移中心
matrix.preTranslate(-centerX,-centerY);
matrix.postTranslate(centerX,centerY);
camera.restore();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: