androidゲームアニメーション

7431 ワード

public class MyView extends View implements AnimationListener {
    public static MyView mv;
    private Paint paint;
    private Bitmap bmp;
    private int x = 50;
    private Animation mAlphaAnimation;
    private Animation mScaleAnimation;
    private Animation mTranslateAnimation;
    private Animation mRotateAnimation;

    public MyView(Context context) {
        super(context);
        mv = this;
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        this.setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //    
        canvas.drawColor(Color.BLACK);
        canvas.drawText("   ↑          ", 80, this.getHeight() - 80, paint);
        canvas.drawText("   ↓           ", 80, this.getHeight() - 60, paint);
        canvas.drawText("   ←             ", 80, this.getHeight() - 40, paint);
        canvas.drawText("   →           ", 80, this.getHeight() - 20, paint);
        //    icon
        canvas.drawBitmap(bmp, this.getWidth() / 2 - bmp.getWidth() / 2, this.getHeight() / 2 - bmp.getHeight() / 2, paint);
        x += 1;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {//          
            mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f);
            mAlphaAnimation.setAnimationListener(this);
            mAlphaAnimation.setDuration(3000);
            // //         3000   =3 
            this.startAnimation(mAlphaAnimation);
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {//           
            mScaleAnimation = new ScaleAnimation(0.0f, 2.0f, 1.5f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f);
            mScaleAnimation.setAnimationListener(this);
            mScaleAnimation.setDuration(2000);
            this.startAnimation(mScaleAnimation);
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {//             
            mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100);
            mTranslateAnimation.setAnimationListener(this);
            mTranslateAnimation.setDuration(2000);
            this.startAnimation(mTranslateAnimation);
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {//           
            mRotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            mRotateAnimation.setAnimationListener(this);
            mRotateAnimation.setDuration(3000);
            this.startAnimation(mRotateAnimation);
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onAnimationStart(Animation animation) {
        Log.e("", "AnimationStart!");

    }
    @Override
    public void onAnimationEnd(Animation animation) {
        Log.e("", "AnimationEnd!");

    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        Log.e("", "AnimationRepeat!");
    }
}