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; /** * @version 2012-8-16 下午02:25:19 **/ public class myGameView extends View { // 渐变透明 private Animation mAnimationAlpha = null; // 渐变尺寸伸缩 private Animation mAnimationScale = null; // 渐变位置移动 private Animation mAnimationTranslate = null; // 渐变画面旋转 private Animation mAnimationRotate = null; Bitmap bitmap = null; Context context = null; public myGameView(Context context) { super(context); bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.icon)) .getBitmap(); // 设置焦点 就可以使用onKeyDown setFocusable(true); this.context = context; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch(keyCode) { case KeyEvent.KEYCODE_DPAD_UP: mAnimationAlpha = AnimationUtils.loadAnimation(context, R.anim.alpha); startAnimation(mAnimationAlpha); break; case KeyEvent.KEYCODE_DPAD_DOWN: mAnimationScale = AnimationUtils.loadAnimation(context, R.anim.scale); startAnimation(mAnimationScale); break; case KeyEvent.KEYCODE_DPAD_LEFT: mAnimationTranslate = AnimationUtils.loadAnimation(context, R.anim.translate); startAnimation(mAnimationTranslate); break; case KeyEvent.KEYCODE_DPAD_RIGHT: mAnimationRotate = AnimationUtils.loadAnimation(context, R.anim.rotate); startAnimation(mAnimationRotate); break; } return super.onKeyDown(keyCode, event); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawBitmap(bitmap, 0, 0, null); } }alpha
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="2000"> </alpha>rotate
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="360" android:pivotX="0.5" android:pivotY="0.5" android:duration="1000"> </rotate>scale
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" 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"> </scale>translate
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="10" android:toXDelta="100" android:fromYDelta="10" android:toYDelta="100" android:duration="1000"> </translate>
版权声明:本文为博主原创文章,未经博主允许不得转载。