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; /** * 初始化方法 * @param fromDegrees 开始的旋转角度 * @param toDegrees 结束的旋转角度 * @param centerX 中心X坐标 * @param centerY 中心Y坐标 * @param depthZ Z轴 * @param reverse 翻转状态的标识 为true时表示从0—90度翻转,false表示从90-0度翻转 */ 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; }
public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); }
发布评论
评论(1)
使用Android中的camera类,我把代码贴上来,你一运行就明白了。
1.封装好的camera类:
package com.rotate3d;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;
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;
/**
* 初始化方法
* @param fromDegrees 开始的旋转角度
* @param toDegrees 结束的旋转角度
* @param centerX 中心X坐标
* @param centerY 中心Y坐标
* @param depthZ Z轴
* @param reverse 翻转状态的标识 为true时表示从0—90度翻转,false表示从90-0度翻转
*/
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;
}
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
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));
}
//绕Y轴旋转
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
2.如何使用:
package com.rotate3d;
import com.rotate3d.cjy.R;
import android.app.Activity;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class Rotate3dActivity extends Activity {
private FrameLayout mContainer;
private ImageView imageView1;
private ImageView imageView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 测试用的view
*/
mContainer = new FrameLayout(this);
mContainer.setBackgroundColor(Color.BLACK);
imageView1=new ImageView(this);
imageView1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView1.setId(20);
imageView1.setImageResource(R.drawable.back);
//第1个图片的按键监听
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView1,mContainer);
}
});
mContainer.addView(imageView1);
imageView2 =new ImageView(this);
imageView2.setId(30);
imageView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView2.setImageResource(R.drawable.front);
//第2个图片的按键监听
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView2,mContainer);
}
});
mContainer.addView(imageView2);
setContentView(mContainer);
}
public void startRotation3D(float start,float end,final View rotateView,final View containerView){
//获得中心点
final float centerX=containerView.getWidth()/2.0f;
final float centerY =containerView.getHeight() / 2.0f;
//开始翻转,将当前界面从0°翻转到90°
Rotate3dAnimation rotation=new Rotate3dAnimation(start, end, centerX, centerY, 200.0f, true);
rotation.setDuration(500);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
containerView.post(new Runnable() {
public void run() {
//判断哪个图片需要隐藏
Log.e("sC", String.valueOf(rotateView.getId()));
if(rotateView.getId() == 20){
imageView1.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
}else if (rotateView.getId() == 30) {
imageView2.setVisibility(View.GONE);
imageView1.setVisibility(View.VISIBLE);
}
//第二次翻转,将新页面从90°翻转到0°
Rotate3dAnimation rotatiomAnimation = new Rotate3dAnimation(-90, 0, centerX, centerY, 200.0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation.setInterpolator(new DecelerateInterpolator());
containerView.startAnimation(rotatiomAnimation);
}
});
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationStart(Animation arg0) {
}
});
containerView.startAnimation(rotation);
}
}