camera动画效果

Animation动画的主要接口,其中主要定义了动画的一些属性比如开始时间,持续时间,是否重复播放等等.

Transformation中则包含一个矩阵和alpha值,矩阵是用来做平移,旋转和缩放动画的,而alpha值是用来做alpha动画的,

要实现3D旋转动画我们需要继承自Animation类来实现,我们需要重载getTransformationapplyTransformation,在getTransformationAnimation会根据动画的属性来产生一系列的差值点,然后将这些差值点传给applyTransformation,这个函数将根据这些点来生成不同的Transformation.


1、从小到大,旋转360°的动画效果

public class MyAnimation extends Animation {
	private int halfWidth;
	private int halfHeight;
	
	@Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {
		super.initialize(width, height, parentWidth, parentHeight);
		System.out.println("MyAnimation  initialize");
		setDuration(1000);
		setFillAfter(true);
		halfWidth = width/2;
		halfHeight = height/2;
	}
	
	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		System.out.println("Myanimation applyTransformation");
		final Matrix matrix = t.getMatrix();
		matrix.preScale(interpolatedTime, interpolatedTime);
		matrix.preRotate(interpolatedTime * 360);
		matrix.preTranslate(-halfWidth, -halfHeight);
		matrix.postTranslate(halfWidth, halfHeight);
	}
}

其中包括了旋转的开始和结束角度,中心点、是否扭曲、和一个Camera,这里我们主要分析applyTransformation函数,其中第一个参数就是通过getTransformation函数传递的差指点,然后我们根据这个差值通过线性差值算法计算出一个中间角度degreesCamera类是用来实现绕Y轴旋转后透视投影的,因此我们首先通过t.getMatrix()取得当前的矩阵,然后通过camera.translate来对矩阵进行平移变换操作,camera.rotateY进行旋转.这样我们就可以很轻松的实现3D旋转效果了


2、从远到近,在z面旋转360°的动画效果

public class MyAnimation extends Animation {
	private int halfWidth;
	private int halfHeight;
	private Camera camera = new Camera();
	
	@Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {
		super.initialize(width, height, parentWidth, parentHeight);
		System.out.println("MyAnimation  initialize");
		setDuration(1000);
		setFillAfter(true);
		halfWidth = width/2;
		halfHeight = height/2;
	}
	
	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		System.out.println("Myanimation applyTransformation");
		final Matrix matrix = t.getMatrix();
		camera.save(); //camera.save() 这句话是将当前的摄像头位置保存下来
		
		camera.translate(0.0f, 0.0f, (3000-3000*interpolatedTime));
		camera.rotateY(360*interpolatedTime);
		
		camera.getMatrix(matrix);  //将我们刚才定义的一系列变换应用到变换矩阵上面
		matrix.preTranslate(-halfWidth, -halfHeight);
		matrix.postTranslate(halfWidth, halfHeight);
		
		camera.restore();  //将camera的位置恢复
	}
}

Camera类,中文意思是摄像头,这是一个逻辑概念,把我们手机的屏幕比作摄像头窗口,透过这个窗口,我们看到里面显示的东西(就是我们应用的界面),当然如果我们从不同的角度来看屏幕中的物体,自然就会呈现出一种立体效果

首先我们得到了一个变换矩阵,camera.save() 这句话是将当前的摄像头位置保存下来,以便变换进行完成后恢复成原位,接下来调用camera.translate,这个方法接受3个参数,分别是x,y,z三个轴的偏移量,我们这里只将z轴进行了偏移,已开始的偏移是3000,随着时间的推移,这个偏移会越来越小。这就会形成这样一个效果,我们的View从一个很远的地方向我们移过来,越来越近,最终移到了我们的窗口上面RotateY是给我们的View加上旋转效果,在移动的过程中,视图还会移Y轴为中心进行旋转。随后的 camera.getMatrix(matrix) ,这个是将我们刚才定义的一系列变换应用到变换矩阵上面,调用完这句之后,我们就可以将camera的位置恢复了,以便下一次再使用。

转载网址:http://www.eoeandroid.com/thread-40674-1-1.html


3、绕Y轴旋转180°的动画效果

public class MyAnimation 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 camera = new Camera();
	
	public MyAnimation(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);
		System.out.println("MyAnimation  initialize");
		setDuration(1000);
		setFillAfter(true);
	}
	
	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		System.out.println("Myanimation applayTranformation");
		final float fromDegrees = mFromDegrees; 
		//生成中间角度 
		float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); 
		final float centerX = mCenterX; 
		final float centerY = mCenterY; 
		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); 
	}
}

4、沿Y轴旋转90°时,两个图片的切换

因为要旋转所以我们需要保存视图的缓存信息,通过setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);可以设置该功能

当我们选择列表中的图像资源后在onItemClick中将选择的资源Id对应的图像设置到ImageView中,然后通过applyRotation来启动一个动画,前面有了Animation的实现,我们要完成3D翻转动画就很简单,直接构建一个Animation对象,设置其属性(包括动画监听),这里将动画的监听设置为DisplayNextView,可以用来显示下一个视图,在其中的动画结束监听(onAnimationEnd)中,通过一个现成SwapViews来交换两个画面,交换过程则是设置ImageView和ListView的显示相关属性,并构建一个Animation对象,对另一个界面进行旋转即可,然后启动动画,整个转换过程实际上就是将第一个界面从0度转好90度,然后就爱你过第二个界面从90度转到0度,这样就形成了一个翻转动画

1、定义的动画效果和上一样

2、actiivty类

public class MainActivity extends Activity implements
		AdapterView.OnItemClickListener, View.OnClickListener {
	// 照片列表
	private ListView mPhotosList;
	private ViewGroup mContainer;
	private ImageView mImageView;
	// 照片的名字,用于显示在list中
	private static final String[] PHOTOS_NAMES = new String[] { "Lyon",
			"Livermore", "Tahoe Pier", "Lake Tahoe", "Grand Canyon", "Bodie" };
	// 资源id
	private static final int[] PHOTOS_RESOURCES = new int[] {
			R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
			R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mPhotosList = (ListView) findViewById(android.R.id.list);
		mImageView = (ImageView) findViewById(R.id.picture);
		mContainer = (ViewGroup) findViewById(R.id.container);
		// 准备ListView
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, PHOTOS_NAMES);
		mPhotosList.setAdapter(adapter);
		mPhotosList.setOnItemClickListener(this);
		// 准备ImageView
		mImageView.setClickable(true);
		mImageView.setFocusable(true);
		mImageView.setOnClickListener(this);
		// 设置需要保存缓存
		mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
	}

	public void onItemClick(AdapterView parent, View v, int position, long id) {
		// 设置ImageView
		mImageView.setImageResource(PHOTOS_RESOURCES[position]);
		applyRotation(position, 0, 90);
	}

	// 点击图像时,返回listview
	public void onClick(View v) {
		applyRotation(-1, 180, 90);
	}
         /**
	 * Setup a new 3D rotation on the container view.
	 * 
	 * @param position
	 *            the item that was clicked to show a picture, or -1 to show the
	 *            list
	 * @param start
	 *            the start angle at which the rotation must begin
	 * @param end
	 *            the end angle of the rotation
	 */
	private void applyRotation(int position, float start, float end) {
		// 计算中心点
		final float centerX = mContainer.getWidth() / 2.0f;
		final float centerY = mContainer.getHeight() / 2.0f;
		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final MyAnimation rotation = new MyAnimation(start, end, centerX,
				centerY, 310.0f, true);
		rotation.setDuration(500);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		// 设置监听
		rotation.setAnimationListener(new DisplayNextView(position));
		mContainer.startAnimation(rotation);
	}

	/**
	 * This class listens for the end of the first half of the animation. It
	 * then posts a new action that effectively swaps the views when the
	 * container is rotated 90 degrees and thus invisible.
	 */
	private final class DisplayNextView implements Animation.AnimationListener {
		private final int mPosition;

		private DisplayNextView(int position) {
			mPosition = position;
		}

		public void onAnimationStart(Animation animation) {
		}

		// 动画结束
		public void onAnimationEnd(Animation animation) {
			mContainer.post(new SwapViews(mPosition));
		}

		public void onAnimationRepeat(Animation animation) {
		}
	}

	/**
	 * This class is responsible for swapping the views and start the second
	 * half of the animation.
	 */
	private final class SwapViews implements Runnable {
		private final int mPosition;

		public SwapViews(int position) {
			mPosition = position;
		}

		public void run() {
			final float centerX = mContainer.getWidth() / 2.0f;
			final float centerY = mContainer.getHeight() / 2.0f;
			MyAnimation rotation;
			if (mPosition > -1) {
				// 显示ImageView
				mPhotosList.setVisibility(View.GONE);
				mImageView.setVisibility(View.VISIBLE);
				mImageView.requestFocus();
				rotation = new MyAnimation(90, 0, centerX, centerY, 0f, false);
			} else {
				// 返回listview
				mImageView.setVisibility(View.GONE);
				mPhotosList.setVisibility(View.VISIBLE);
				mPhotosList.requestFocus();
				rotation = new MyAnimation(90, 0, centerX, centerY, 310.0f,
						false);
			}
			rotation.setDuration(500);
			rotation.setFillAfter(true);
			rotation.setInterpolator(new DecelerateInterpolator());
			// 开始动画
			mContainer.startAnimation(rotation);
		}
	}
}

转载地址:http://www.eoeandroid.com/thread-102460-1-1.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值