android 实现3D动画旋转效果


一个LinearLayout有一张背景图片,里面有一个ImageButton子组件,子组件完成360度顺时针旋转效果。

MainActivity:

                   我只是启动Activity就显示动画效果,所以在oncreate()方法中模拟,但是这时候imageButton尺寸大小的值不太容易获取,我上一篇文章已经讲到了这个问题。

                   oncreate中获取组件尺寸大小:    http://blog.csdn.net/u010152805/article/details/17592083

public class MainActivity extends Activity {

	private ImageButton ib;
	float w,y;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
         //在oncreate()中获取imageButton尺寸大小
         ViewTreeObserver vto = ib.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			
			@Override
			public void onGlobalLayout() {
				ib.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				w=ib.getWidth();
				y=ib.getHeight();
				rotateAnim(w,y);
			}
		});
	}
	
	/**
	 * 实现动画旋转效果
	 */
	private void rotateAnim(float w,float y) {
		//图片旋转中心坐标
		final float centerX = w/ 2.0f;
		final float centerY = y/ 2.0f;
		RotateAnimation rotation;
		        rotation = new RotateAnimation(0, -360, centerX, centerY, 310.0f,
				         false);
		rotation.setDuration(1500);//动画持续时间
		rotation.setFillAfter(true);
		rotation.setInterpolator(new DecelerateInterpolator());//设置加速度
		rotation.setRepeatCount(Animation.INFINITE);//重复次数
		rotation.setRepeatMode(Animation.RESTART);//重复动画
		ib.startAnimation(rotation);

	}
}
动画类:

/**
 * 3D动画效果类
 * 三个关键类 (Animation/Camera/Matrix)
 */
public class RotateAnimation extends Animation {
	/**开始角度*/
	private final float mFromDegrees;
	/**结束角度*/
	private final float mToDegrees;
	/**中心坐标X轴*/
	private final float mCenterX;
	/**中心坐标Y轴*/
	private final float mCenterY;
	/**反面*/
	private final boolean mReverse;
	/**相机类*/
	private Camera mCamera;

	/**
	 * 构造方法
	 * @param fromDegrees 开始角度
	 * @param toDegrees 结束角度
	 * @param centerX 中心坐标
	 * @param centerY 中心坐标
	 * @param depthZ
	 * @param reverse
	 */
	public RotateAnimation(float fromDegrees, float toDegrees,
			float centerX, float centerY, float depthZ, boolean reverse) {
		mFromDegrees = fromDegrees;
		mToDegrees = toDegrees;
		mCenterX = centerX;
		mCenterY = centerY;
		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 tran) {
		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 = tran.getMatrix();
		camera.save();//保存状态
		if (mReverse) {
			//x,y,z距离中心轴各自的距离长度
			camera.translate(0.0f, 0.0f, 0.0f);  
		} else {
			camera.translate(0.0f, 0.0f, 0.0f);
		}
		//以多少角度围绕Y轴旋转,可以围绕X,Y,Z三个方向旋转
		camera.rotateY(degrees);
		camera.getMatrix(matrix);
		//恢复保存状态
		camera.restore();
		matrix.preTranslate(-centerX, -centerY);
		matrix.postTranslate(centerX, centerY);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值