1.编码方式实现
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;
}
}
2。资源文件定义方式实现动画
alpha_animation.xml
<?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>
rotate_animation.xml
<?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"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" />
</set>
scale_animation.xml
<?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"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="500" />
</set>
translate_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="10"
android:toXDelta="100"
android:fromYDelta="10"
android:toYDelta="100"
android:duration="1000"
/>
</set>
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;
}
3.实现FrameAnimation
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);
Paint p=new Paint();
p.setARGB(255, 255, 0, 0);
canvas.drawRect(50, 50, 100, 100, p);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch ( keyCode )
{
case KeyEvent.KEYCODE_DPAD_UP:
/* 开始播放动画 */
frameAnimation.start();
break;
}
return true;
}
}
//加载动画文档资源描述
/* 定义一个ImageView用来显示动画 */
ImageView img = new ImageView(mContext);
/* 装载动画布局文件 */
img.setBackgroundResource(R.anim.frameanimation);
/* 构建动画 */
frameAnimation = (AnimationDrawable) img.getBackground();
/* 设置是否循环 */
frameAnimation.setOneShot( false );
/* 设置该类显示的动画 */
this.setBackgroundDrawable(frameAnimation);
res/anim/frameanimation.xml
<?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>