您的位置:首页 > 其它

实现3D旋转效果的方法

2014-05-21 15:40 141 查看
Android中有一种旋转效果,是将一个图片进行360度的旋转。

Matrix的作用是对平面上的View进行缩放、平移、旋转,每一种操作都配了setXXX、preXXX、postXXX三个函数。

Camera不是物理摄像头,是android.graphic下的一个类,相当于手机的屏幕,他的坐标系是带有Z坐标的。



可以完成对指定View的X,Y,Z轴的变化,所以可以用来完成3D效果。但是变化之后并不是直接作用于View,而是修改View的Matrix的值,最终View再根据Matrix的值来变换。

Android APIDemos中已经提供了一种例子

/**
* An animation that rotates the view on the Y axis between two specified angles.
* This animation also adds a translation on the Z axis (depth) to improve the effect.
*/
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;

/**
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
* start angle and its end angle. Both angles are in degrees. The rotation
* is performed around a center point on the 2D space, definied by a pair
* of X and Y coordinates, called centerX and centerY. When the animation
* starts, a translation on the Z axis (depth) is performed. The length
* of the translation can be specified, as well as whether the translation
* should be reversed in time.
*
* @param fromDegrees the start angle of the 3D rotation
* @param toDegrees the end angle of the 3D rotation
* @param centerX the X center of the 3D rotation
* @param centerY the Y center of the 3D rotation
* @param reverse true if the translation should be reversed, false otherwise
*/
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();
}

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


如果不需要做特别的效果的话,可以直接照搬

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
* @author Administrator
*
*/
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;
// 是否需要扭曲 ,也就是是否有Z轴方向深度的变化
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,整个动画的过程中会不停的被调用
* @param interpolatedTime 会逐渐从0增大到1
* @param Transformation 变换对象
*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
/**
* 实际上整个过程就是通过Camera计算出要旋转所要修改的Matrix的值
* 整个函数结束之后,会根据这个Matrix的值进行变化
* 也就是说实际最终变换依据的依然是Matrix的值
*/
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);
}
//旋转到指定的角度
camera.rotateY(degrees);
//取得变换后的矩阵,修改Matrix的值
camera.getMatrix(matrix);
camera.restore();//恢复摄像机的状态 ,每次摄像机变换完后,下次又从摄像机初始位置开始变化
//将变换的中心点设成中心点
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}


Activity中的代码

import org.cxjchen.animation.Rotate3DAnimation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.*;
import android.widget.ImageView;
import android.widget.TextView;

/**
* @author Administrator
*
*/
public class Logo_Activity extends Activity {

private TextView logo_title = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.logo_activity);

logo_title = (TextView)findViewById(R.id.logo_title);
logo_title.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
applyRotation(0,360);
}
});
}

private void applyRotation(float start, float end) {
// 计算中心点
final float centerX = logo_title.getWidth() / 2.0f;
final float centerY = logo_title.getHeight() / 2.0f;
final Rotate3DAnimation rotation = new Rotate3DAnimation(start, end,
centerX, centerY, 360.0f, false);
rotation.setDuration(1000L);
//设置变换后是否维持变换状态
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
// 设置监听
rotation.setAnimationListener(new Animation.AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {

}
});
logo_title.startAnimation(rotation);
}

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