Android支持动画效果。常见的有Tween Animation和Frame Animation.其中Frame Animation我们在墙面介绍过了,它比较简单。这里来介绍下Tween
Tween Animation 常见可以分成四种:
ScaleAnimation :渐变尺寸伸缩效果
AlphaAnimation :渐变透明度动画效果
TranslateAnimation :画面转换位置动画效果
RotateAnimation :画面转移动画效果
其中,我们可以在代码中直接使用,也可以把Animation做成资源XML文件,从资源中装载。
Animation Java Code的使用示例:
package com.rocky.demo; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; public class GameView extends View { private Animation mAnimationAlpha=null; private Animation mAnimationScale=null; private Animation mAnimationTranslate=null; private Animation mAnimationRotate=null; private Context mContext=null; Bitmap mBackGroundBitmap=null; int index=0; public GameView(Context context) { super(context); // TODO Auto-generated constructor stub mContext=context; mBackGroundBitmap=((BitmapDrawable)this.getResources().getDrawable(R.drawable.image2)).getBitmap(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawBitmap(mBackGroundBitmap, 0,0,null); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch(keyCode) { case KeyEvent.KEYCODE_DPAD_DOWN: { //mAnimationAlpha=AnimationUtils.loadAnimation(mContext, R.anim.alpha_animation); mAnimationAlpha=new AlphaAnimation(0.1f,1.0f); mAnimationAlpha.setDuration(10000); this.startAnimation(mAnimationAlpha); } break; case KeyEvent.KEYCODE_DPAD_UP: { //mAnimationRotate=AnimationUtils.loadAnimation(mContext, R.anim.rotate_animation); mAnimationRotate=new RotateAnimation(0.0f,+360.0f,Animation.ABSOLUTE,400,Animation.ABSOLUTE,300); mAnimationRotate.setDuration(1000); this.startAnimation(mAnimationRotate); } break; case KeyEvent.KEYCODE_DPAD_LEFT: { //mAnimationScale=AnimationUtils.loadAnimation(mContext, R.anim.scale_animation); mAnimationScale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.5f); mAnimationScale.setDuration(4000); this.startAnimation(mAnimationScale); } break; case KeyEvent.KEYCODE_DPAD_RIGHT: { //mAnimationTranslate=AnimationUtils.loadAnimation(mContext, R.anim.translate_animation); mAnimationTranslate=new TranslateAnimation(0,800,0,600); mAnimationTranslate.setDuration(4000); this.startAnimation(mAnimationTranslate); } break; } return super.onKeyDown(keyCode, event); } }
Android动画解析 --XML
<alpha>
<scale>
<translate>
<rotate>
如何使用XML 中的动画效果
下面给出一个AnimationXml文件的具体内容。
<?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,rotate,translate等等都是类似的。