Android 3D旋转动画之Camera 和 Matrix

前面两篇博文讲解的都是Android 的2D动画效果,要想做出非常炫酷的3D动画效果怎么办?android 并没有提供3D动画接口给用户,所以我们得自己重写这样一个3D接口动画。

接口如下:

/*
 * @Title: My3dAnimation.java
 * @Description: TODO<请描述此文件是做什么的>
 * @author: xjp
 * @data: 2014年9月15日 上午8:54:10
 * @version: V1.0
 */
package com.xjp.animator;

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

/**
 * TODO<请描述这个类是干什么的>
 *
 * @author xjp
 * @data: 2014年9月15日 上午8:54:10
 * @version: V1.0
 */
public class My3dAnimation extends Animation {

	private final float mFromDegrees;
	private final float mToDegrees;
	private final float mCenterX;
	private final float mCenterY;
	private final float mDepthZ;
	private Camera mCamera;
	private int mDirection;
	private final static int ROTATE_X = 0;//沿着x轴旋转
	private final static int ROTATE_Y = 1;//沿着y轴旋转

	/**
	 * 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 direction
	 *            the direction of the 3D rotation
	 * @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
	 */
	public My3dAnimation(int direction, float fromDegrees, float toDegrees,
			float centerX, float centerY, float depthZ) {
		mDirection = direction;
		mFromDegrees = fromDegrees;
		mToDegrees = toDegrees;
		mCenterX = centerX;
		mCenterY = centerY;
		mDepthZ = depthZ;
	}

	@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 (centerX!=0){
			if (interpolatedTime < 0.5) {
				camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
			} else {
				camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
			}
		}

		switch (mDirection) {
		case ROTATE_X:
			camera.rotateX(degrees);
			break;
		case ROTATE_Y:
			camera.rotateY(degrees);
			break;
		}

		camera.getMatrix(matrix);
		camera.restore();
		matrix.preTranslate(-centerX, -centerY);
		matrix.postTranslate(centerX, centerY);
	}
}

示例代码如下:

package com.xjp.animator;

import com.xjp.animator.R;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.app.Activity;

public class MainActivity extends Activity implements
		android.view.View.OnClickListener {

	private ImageView img1;
	private ImageView img2;
	private ImageView img3;
	private ViewGroup mContainer;
	private final static int ROTATE_X = 0;
	private final static int ROTATE_Y = 1;
	private My3dAnimation my3dAnimation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViews();
	}

	/**
	 * TODO<请描述这个方法是干什么的>
	 *
	 * @throw
	 * @return void
	 * @param
	 */
	private void findViews() {
		// TODO Auto-generated method stub
		mContainer = (ViewGroup) findViewById(R.id.container);
		mContainer
				.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);

		img1 = (ImageView) findViewById(R.id.img_left);
		img1.setOnClickListener(this);
		img2 = (ImageView) findViewById(R.id.img_right);
		img2.setOnClickListener(this);

		img3 = (ImageView) findViewById(R.id.img_3);
		img3.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		float centerX = v.getWidth() / 2.0f;
		float centerY = v.getHeight() / 2.0f;
		if (v.getId() == R.id.img_left) {
			my3dAnimation = new My3dAnimation(ROTATE_X, 0, 180, centerX,
					centerY, 310f);
		}
		if (v.getId() == R.id.img_right) {
			my3dAnimation = new My3dAnimation(ROTATE_Y, 0, 180, centerX,
					centerY, 310f);
		}
		if (v.getId() == R.id.img_3) {
			centerX = 0;
			my3dAnimation = new My3dAnimation(ROTATE_Y, 0, 20, centerX,
					centerY, 310f);
		}
		my3dAnimation.setDuration(1000);
		my3dAnimation.setInterpolator(new LinearInterpolator());
		my3dAnimation.setFillAfter(true);
		v.startAnimation(my3dAnimation);
	}
}

完结。

时间: 2024-12-21 23:37:54

Android 3D旋转动画之Camera 和 Matrix的相关文章

Android自定义动画类——实现3D旋转动画

Android中的补间动画分为下面几种: (1)AlphaAnimation :透明度改变的动画. (2)ScaleAnimation:大小缩放的动画. (3)TranslateAnimation:位移变化的动画. (4)RotateAnimation:旋转动画. 然而在实际项目中透明度.缩放.位移.旋转这几种动画并不能满足我们的需求,比如我们需要一个类似下面的3D旋转动画. 这时候就需要用到自定义动画,自定义动画需要继承Animation,并重写applyTransformation(floa

Android立体旋转动画实现与封装(支持以X、Y、Z三个轴为轴心旋转)

本文主要介绍Android立体旋转动画,或者3D旋转,下图是我自己实现的一个界面 立体旋转分为以下三种: 1. 以X轴为轴心旋转 2. 以Y轴为轴心旋转 3. 以Z轴为轴心旋转--这种等价于android默认自带的旋转动画RotateAnimation 实现立体旋转核心步骤: 1. 继承系统Animation重写applyTransformation方法 通过applyTransformation方法的回调参数 float interpolatedTime, Transformation t 来

android:3D垂直翻转动画-FlipAnimation

需求 对ImageView进行类似于翻纸牌的动画 解决 各种Animator的组合 第一步动画: 动画代码文件1,card_flip_left_out.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 先缩小 --> <objec

android运用旋转动画

Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果:第二类是 Frame 动画,即顺序播放事先做好的图像.本文分析 Tween动画的rotate实现旋转效果.下面直接给出代码,并且附近中有完整的demo 主类: public class MainActivity extends ActionBarActivity {    private ImageView infoOperatingIV; @Override    

css3 3d旋转动画

    <!doctype html> <html> <head> <meta charset="utf-8"> <title>css3 3d动画 keyframes</title> </head> <body> <style>/*************** ANIMATIONS ***************/ @-webkit-keyframes spin { from {

CSS3 3D旋转动画代码实例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

百思不得其解,关于立方体旋转动画(垂直方向)

============问题描述============ 想做个自定义的view 竖直滑动手势就是翻页. 效果大概是这样的 我用的是属性动画. 用的rotation那个方法让下面的view从-90转到0 上面的view从0转到90 但是 效果跟预期不一样. 求助,有做过的大神来帮帮忙.十分感谢. 下面是我的主要的代码 public class MainActivity extends Activity {     private Button btn;     private ImageView

Android中轴旋转特效实现,制作别样的图片浏览器

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10766017 Android API Demos中有很多非常Nice的例子,这些例子的代码都写的很出色,如果大家把API Demos中的每个例子研究透了,那么恭喜你已经成为一个真正的Android高手了.这也算是给一些比较迷茫的Android开发者一个指出了一个提升自我能力的方向吧.API Demos中的例子众多,今天我们就来模仿其中一个3D变换的特效,来实现一种别样的图片浏览器

Android进阶——属性动画Property Animation详解(一)

引言 前一篇文章Android入门--补间动画和帧动画应用小结总结了补间动画和帧动画及一些相关类的应用,基本可以掌握简单的缩放.旋转.透明度变化.平移的动画效果,但是需要实现更复杂的动画效果时,比如说希望View的切换动画.Layout的切换动画.3D旋转动画等等,这些View Animation都无法做到.此时Property Animation应运而生,这篇主要总结下属性动画的相关知识点. 一.Property Animation属性动画概述 属性动画,在我最先接触到Android 1.5时