具体介绍上一章已经解释的很清楚了,这里直接上代码:
路径res/anim
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>
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
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;
}
}