Android动画详解

  Android平台提供了两类动画,一类是Tween动画,即对通过对场景里的对象不断进行图像变换(平移、缩放、旋转);第二类动画是Frame动画,即顺序播放实现做好的图像,和播放电影类似。

1.Tween动画

Tween动画通过对VIew的内容完成一系列的图像变换(包括平移、缩放、旋转、改变透明度)来实现动画效果,主要有四种动画效果:

(1)Alpha:渐变透明度动画效果。

(2)Scale:渐变尺寸伸缩动画效果。

(3)Translate:画面转换位置移动动画效果。

(4)Rotate:画面转移旋转动画效果。

  具体来讲,Tween动画就是通过预先定义一组指令,这些指令指定了图形变换的类型,触发时间、持续时间。程序沿着时间线执行这些指令就可以实现动画效果。因此首先需要定义Animation动画对象,然后设置该动画的一些属性,最后通过startAnimation方法来开始动画。各个动画的属性设置如下:

AlphaAnimation(float fromAlpha,float toAlpha)

功能:构建一个渐变透明度动画

参数:fromAlpha为动画起始时透明度;toAlpha为动画结束时透明度(0.0表示完全透明,1.0表示完全不透明)。

ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXvalue, int pivotYType, float pivotYValue)

功能:构建一个渐变尺寸伸缩动画

参数:fromX、toX分别是起始和结束时x坐标上的伸缩尺寸。formY、toY分别是起始和结束y坐标上伸缩尺寸。pivoXType、pivotYType分别是x,y的伸缩模式。pivotXValue、pivotYValue分别为伸缩动画相对于x,y的坐标开始位置。

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

功能:构建一个画面转换位置移动动画。

参数:fromXDelta、fromYDelta分别为起始坐标;toXDelta、toYDelta分别为结束坐标。

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotValue)

功能:构建一个旋转画面的动画

参数:fromDegrees为开始角度;toDegrees为结束的角度。pivotXValue、pivotYType分别为x,y的伸缩模式。pivotXValue、pivotYVAlue分别为伸缩动画相对于x,y的坐标开始位置。

setDuration(long durationMillis)

功能:设置动画显示的时间。

参数:durationMillis为动画显示时间的长短,以毫秒为单位。

startAnimation(Animation animation)

功能:开始播放动画。

参数:animatiom为要播放的动画。

public class GameView extends View
{
	/* 定义Alpha动画 */
	private Animation	mAnimationAlpha		= null;
	
	/* 定义Scale动画 */
	private Animation	mAnimationScale		= null;
	
	/* 定义Translate动画 */
	private Animation	mAnimationTranslate	= null;
	
	/* 定义Rotate动画 */
	private Animation	mAnimationRotate	= null;
	
	/* 定义Bitmap对象 */
	Bitmap				mBitQQ				= null;
	
	public GameView(Context context)
	{
		super(context);
		
		/* 装载资源 */
		mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();
	}
	
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
		/* 绘制图片 */
		canvas.drawBitmap(mBitQQ, 0, 0, null);
	}

	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		switch ( keyCode )
		{
		case KeyEvent.KEYCODE_DPAD_UP:
			/* 创建Alpha动画 */
			mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f);
			/* 设置动画的时间 */
			mAnimationAlpha.setDuration(3000);
			/* 开始播放动画 */
			this.startAnimation(mAnimationAlpha);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			/* 创建Scale动画 */
			mAnimationScale =new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
												Animation.RELATIVE_TO_SELF, 0.5f, 
												Animation.RELATIVE_TO_SELF, 0.5f);
			/* 设置动画的时间 */
			mAnimationScale.setDuration(500);
			/* 开始播放动画 */
			this.startAnimation(mAnimationScale);
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			/* 创建Translate动画 */
			mAnimationTranslate = new TranslateAnimation(10, 100,10, 100);
			/* 设置动画的时间 */
			mAnimationTranslate.setDuration(1000);
			/* 开始播放动画 */
			this.startAnimation(mAnimationTranslate);
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			/* 创建Rotate动画 */
			mAnimationRotate=new RotateAnimation(0.0f, +360.0f,
												 Animation.RELATIVE_TO_SELF,0.5f,
												 Animation.RELATIVE_TO_SELF, 0.5f);
			/* 设置动画的时间 */
			mAnimationRotate.setDuration(1000);
			/* 开始播放动画 */
			this.startAnimation(mAnimationRotate);
			break;
		}
		return true;
	}
}


  以上都是通过编写Java代码实现的。其实,在Android中使用XML布局文件实现动画更加简单。

<alpha>动画布局中,fromAlpha表示其实透明度,toAlpha表示结束透明度,duration表示动画时间长短

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="2000"
/> 
</set>

<scale>动画布局

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <scale
          android:interpolator="@android:anim/accelerate_decelerate_interpolator"
          
          android:fromXScale="0.0"    //动画起始时x坐标上的伸缩尺寸
          android:toXScale="1.0"      //动画结束时x坐标上的伸缩尺寸
          
          android:fromYScale="0.0"    //动画起始时y坐标上伸缩尺寸
          android:toYScale="1.0"      //动画结束时y坐标上的伸缩尺寸
          
          android:pivotX="50%"        //动画相对于物件的x坐标的开始位置
          android:pivotY="50%"        //动画相对于物件的y坐标的开始位置
          
          android:fillAfter="false"   
          android:duration="500" />
</set>

<translate>动画布局

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="10"      //动画起始时x坐标上的位置
android:toXDelta="100"       //动画结束时x坐标上的位置
android:fromYDelta="10"      //动画起始时y坐标上的位置
android:toYDelta="100"       //动画结束时y坐标上的位置
android:duration="1000"
/>
</set>

<rotate>动画布局

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        
<rotate 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        
        android:fromDegrees="0"      //动画起始时物件的角度
        android:toDegrees="+360"     //动画结束时物件旋转的角度可以大于360度
                
        android:pivotX="50%"         //动画相对于物件的x坐标的开始位置
        android:pivotY="50%"         //动画相对于物件的y坐标的开始位置
         
        android:duration="1000" />
</set>

  只需要在程序中装载这些XML布局,然后开始播放动画即可。

public class GameView extends View
{
	/* 定义Alpha动画 */
	private Animation	mAnimationAlpha		= null;
	
	/* 定义Scale动画 */
	private Animation	mAnimationScale		= null;
	
	/* 定义Translate动画 */
	private Animation	mAnimationTranslate	= null;
	
	/* 定义Rotate动画 */
	private Animation	mAnimationRotate	= null;
	
	/* 定义Bitmap对象 */
	Bitmap				mBitQQ				= null;
	
	Context mContext = null;
	public GameView(Context context)
	{
		super(context);
		
		mContext = context;
		
		/* 装载资源 */
		mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();
	}
	
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
		/* 绘制图片 */
		canvas.drawBitmap(mBitQQ, 0, 0, null);
	}
	
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		switch ( keyCode )
		{
		case KeyEvent.KEYCODE_DPAD_UP:
			/* 装载动画布局 */
			mAnimationAlpha = AnimationUtils.loadAnimation(mContext,R.anim.alpha_animation);
			/* 开始播放动画 */
			this.startAnimation(mAnimationAlpha);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			/* 装载动画布局 */
			mAnimationScale = AnimationUtils.loadAnimation(mContext,R.anim.scale_animation);
			/* 开始播放动画 */
			this.startAnimation(mAnimationScale);
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			/* 装载动画布局 */
			mAnimationTranslate = AnimationUtils.loadAnimation(mContext,R.anim.translate_animation);
			/* 开始播放动画 */
			this.startAnimation(mAnimationTranslate);
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			/* 装载动画布局 */
			mAnimationRotate = AnimationUtils.loadAnimation(mContext,R.anim.rotate_animation);
			/* 开始播放动画 */
			this.startAnimation(mAnimationRotate);
			break;
		}
		return true;
	}
}


2.Frame动画

  Frame动画只需要创建一个AnimationDrawable对象来表示Frame动画,然后通过addFrame方法把每一帧要显示地内容添加进去,最后通过start方法播放这个动画,同时还可以通过setOneShot方法来设置该动画是否重复播放。

public class GameView extends View
{
	/* 定义AnimationDrawable动画 */
	private AnimationDrawable	frameAnimation	= null;
	Context						mContext		= null;
	
	/* 定义一个Drawable对象 */
	Drawable				mBitAnimation				= null;
	public GameView(Context context)
	{
		super(context);
		
		mContext = context;
		
		/* 实例化AnimationDrawable对象 */
		frameAnimation = new AnimationDrawable();
		
		/* 装载资源 */
		//这里用一个循环了装载所有名字类似的资源
		//如“a1.......15.png”的图片
		//这个方法用处非常大
		for (int i = 1; i <= 15; i++)
		{
			int id = getResources().getIdentifier("a" + i, "drawable", mContext.getPackageName());
			mBitAnimation = getResources().getDrawable(id);
			/* 为动画添加一帧 */
			//参数mBitAnimation是该帧的图片
			//参数500是该帧显示的时间,按毫秒计算
			frameAnimation.addFrame(mBitAnimation, 500);
		}
		
		/* 设置播放模式是否循环false表示循环而true表示不循环 */
		frameAnimation.setOneShot( false );  
		
		/* 设置本类将要显示这个动画 */
		this.setBackgroundDrawable(frameAnimation);
	}
	
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
	}
	
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		switch ( keyCode )
		{
		case KeyEvent.KEYCODE_DPAD_UP:		
			/* 开始播放动画 */
			frameAnimation.start();
			break;
		}
		return true;
	}
}

  使用XML布局来实现Frame动画,只需要在XML文件中通过oneshot设置动画是否重复播放,然后将所有的帧都列出来即可,最后在程序中通过setBackgroundResource方法加载这个XML文件,通过用getBackground方法得到动画,通过用色堂BackgroundDrawable方法设置要显示地动画,通过用start开始播放动画即可。

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item android:drawable="@drawable/a1" android:duration="500" />
    <item android:drawable="@drawable/a2" android:duration="500" />
    <item android:drawable="@drawable/a3" android:duration="500" />
    <item android:drawable="@drawable/a4" android:duration="500" />
    <item android:drawable="@drawable/a5" android:duration="500" />
    <item android:drawable="@drawable/a6" android:duration="500" />
    <item android:drawable="@drawable/a7" android:duration="500" />
    <item android:drawable="@drawable/a8" android:duration="500" />
    <item android:drawable="@drawable/a9" android:duration="500" />
    <item android:drawable="@drawable/a10" android:duration="500" />
    <item android:drawable="@drawable/a11" android:duration="500" />
    <item android:drawable="@drawable/a12" android:duration="500" />
    <item android:drawable="@drawable/a13" android:duration="500" />
    <item android:drawable="@drawable/a14" android:duration="500" />
    <item android:drawable="@drawable/a15" android:duration="500" />      
</animation-list>

public class GameView extends View
{
	/* 定义AnimationDrawable动画对象 */
	private AnimationDrawable	frameAnimation	= null;
	Context						mContext		= null;
	public GameView(Context context)
	{
		super(context);
		
		mContext = context;
		
		/* 定义一个ImageView用来显示动画 */
		ImageView img = new ImageView(mContext);
		
		/* 装载动画布局文件 */
		img.setBackgroundResource(R.anim.frameanimation);		
		
		/* 构建动画 */
		frameAnimation = (AnimationDrawable) img.getBackground();
		
		/* 设置是否循环 */
		frameAnimation.setOneShot( false );  
		
		/* 设置该类显示的动画 */
		this.setBackgroundDrawable(frameAnimation);
	}
	
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
	}
	
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		switch ( keyCode )
		{
		case KeyEvent.KEYCODE_DPAD_UP:	
			/* 开始播放动画 */
			frameAnimation.start();
			break;
		}
		return true;
	}
}

3.CIF动画

  要想播放GIF动画,首先需要对GIF图像进行解码,然后将GIF中的每一帧分别提取出来保存到一个容器中,然后根据需要连续绘制每一帧。

	public GameView(Context context)
	{
		super(context);
		
		mContext = context;
		/* 解析GIF动画 */
		mGifFrame=GifFrame.CreateGifImage(fileConnect(this.getResources().openRawResource(R.drawable.gif1)));
		/* 开启线程 */
		new Thread(this).start();
	}
	
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		/* 下一帧 */
		mGifFrame.nextFrame();
		/* 得到当前帧的图片 */
		Bitmap b=mGifFrame.getImage();
		
		/* 绘制当前帧的图片 */
		if(b!=null)
			canvas.drawBitmap(b,10,10,null);
	}
	
	
	/**
	 * 线程处理
	 */
	public void run()
	{
		while (!Thread.currentThread().isInterrupted())
		{
			try
			{
				Thread.sleep(100);
			}
			catch (InterruptedException e)
			{
				Thread.currentThread().interrupt();
			}
			//使用postInvalidate可以直接在线程中更新界面
			postInvalidate();
		}
	}
	
	/* 读取文件 */
	public byte[] fileConnect(InputStream is)
	{
		try
		{					    
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int ch = 0;
			while( (ch = is.read()) != -1) 
			{
				baos.write(ch);
			}			      
			byte[] datas = baos.toByteArray();
			baos.close(); 
			baos = null;
			is.close();
			is = null;
			return datas;
		}
		catch(Exception e)
		{
			return null;
		}
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值