Android动画分析

Android动画模式主要有以下几种:

一、帧动画Frame animation)

       即让多重图片重复播放,可通过xml文件实现。例:

  <?xml version="1.0" encoding="utf-8"?>
    <!-- 
	根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
	根标签下,通过item标签对动画中的每一个图片进行声明
	android:duration 表示展示所用的该图片的时间长度
     -->
     <animation-list
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:oneshot="true" >
  	<item android:drawable="@drawable/icon1" android:duration="150"></item>
  	<item android:drawable="@drawable/icon2" android:duration="150"></item>
  	<item android:drawable="@drawable/icon3" android:duration="150"></item>
  	<item android:drawable="@drawable/icon4" android:duration="150"></item>
  	<item android:drawable="@drawable/icon5" android:duration="150"></item>
  	<item android:drawable="@drawable/icon6" android:duration="150"></item>
   </animation-list>

二、渐变动画tweened animation)

   即移动(translate)、缩放(scale)、旋转(rotate)、透明(alpha)。这种动画可通过代码也可通过xml配置实现。网上例子很多,这里不做举例。

   对两个函数特别说明:

    1、setFillAfter(true):动画结束后是否停留在最后一帧。

    2、setFillBefore(true):动画结束后是否停留在第一帧。


三、属性动画(Property animation)

     帧动画的动画过程是对view进行重绘,但是view的属性值是不会变的,动画的执行过程只是改变view的重绘效果;

     属性动画在动画的执行过程是不停的改变view的属性,且不停的重绘view。


四、视图动画(View animation)

   要实现视图动画,需继承重写Animation。这里说明重要的几个重写的函数:

    1、初始化函数: public void initialize(int width, int height, int parentWidth, int parentHeight)

    2、动画效果函数: protected void applyTransformation(float interpolatedTime, Transformation t) 

        其中 参数 interpolatedTime 是一个0~1的浮点数,一直变化,直到动画结束。其持续时间和this.setDuration(参数);中‘参数’有关,这个参数表示一个毫秒数,如2500表示2.5秒。

   代码实例:

import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

public class CustomAnimation extends Animation {

	private static final String TAG = "CustomAnimation";

	private Camera mCamera = null;

	private float mCenterX = 0f;

	private float mCenterY = 0f;

	private int mIndex = -1;

	public CustomAnimation(int index) {
		mCamera = new Camera();
		mIndex = index;
	}

	@Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {
		// TODO Auto-generated method stub
		Log.i(TAG, "CustomAnimation initialize width: " + width + " ,height: " + height);
		super.initialize(width, height, parentWidth, parentHeight);
		mCenterX = width / 2;
		mCenterY = height / 2;

		setDuration(2500);
		setFillAfter(true);
		setInterpolator(new LinearInterpolator());
	}

	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		// TODO Auto-generated method stub
		Log.i(TAG, "CustomAnimation applyTransformation: " + interpolatedTime);
		super.applyTransformation(interpolatedTime, t);
		final Matrix matrix = t.getMatrix();
		mCamera.save();
		transformationStyle(interpolatedTime, matrix);

		mCamera.getMatrix(matrix);
		matrix.preTranslate(-mCenterX, -mCenterY);
		matrix.postTranslate(mCenterX, mCenterY);

		mCamera.restore();
	}

	private void transformationStyle(float interpolatedTime, Matrix matrix) {
		switch (mIndex) {
		case 0:
			mCamera.translate(0.0f, 0.0f, 0.0f);
			mCamera.rotateY(360 * interpolatedTime);
			break;
		case 1:
			mCamera.translate(-500.f * interpolatedTime, 0.0f, 0.0f);
			break;
		case 2:
			mCamera.translate(0.0f, -300.f * interpolatedTime, 0.0f);
			break;
		case 3:
			mCamera.translate(0.0f, 0.0f, 1300.f * interpolatedTime);
			break;
		default:
			break;
		}

	}

}




 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值