正文  游戏开发 > 游戏引擎 >

Android 游戏开发 View框架

先看效果图:按键盘的上下键矩形就会上下移动;通过实例化Handler对象并重写handkeMessage方法实现了一个消息接收器。然后再线程中通过sendMessage方法发送更新界面的消息,接收器收到更新界面的消息时便执行inv......

先看效果图:

按键盘的上下键矩形就会上下移动;

通过实例化Handler对象并重写handkeMessage方法实现了一个消息接收器。然后再线程中通过sendMessage方法发送更新界面的消息,接收器收到更新界面的消息时便执行invalidate方法更新屏幕显示。

 

package com.yarin.android.TestOnView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class GameView extends View
{
    int  miCount = 0;
    int  y = 0;
    public GameView(Context context)
    {
        super(context);
    }
     
    public void onDraw(Canvas canvas)
    {
        if (miCount < 100)
        {
            miCount++;
        }
        else
        {
            miCount = 0;
        }
        //绘图
        Paint mPaint = new Paint();  
        switch (miCount%4)
        {
        case 0:
            mPaint.setColor(Color.BLUE);     
            break;
        case 1:
            mPaint.setColor(Color.GREEN);     
            break;
        case 2:
            mPaint.setColor(Color.RED);     
            break;
        case 3:
            mPaint.setColor(Color.YELLOW);     
            break;
        default:
            mPaint.setColor(Color.WHITE);     
            break;
        }
        //绘制矩形
        canvas.drawRect((320-80)/2, y, (320-80)/2+80, y+40, mPaint);
    }
}

上面是用来绘制界面的,我们还需要一个类来控制应用的操作

 

package com.yarin.android.TestOnView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class ControlView extends Activity
{
    private static final int    REFRESH        = 0x000001;
   
   
    private GameView            mGameView    = null;

   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

       
        this.mGameView = new GameView(this);

        // 设置显示为我们自定义的View(GameView)
        setContentView(mGameView);

        // 开启线程
        new Thread(new GameThread()).start();
    }

    Handler    myHandler    = new Handler()
    {
        //接收到消息后处理
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case ControlView.REFRESH:
                mGameView.invalidate();
                break;
            }
            super.handleMessage(msg);
        }           
    };

    class GameThread implements Runnable
    {
        public void run()
        {
            while (!Thread.currentThread().isInterrupted())
            {
                Message message = new Message();
                message.what = ControlView.REFRESH;
                //发送消息
                Activity01.this.myHandler.sendMessage(message);
                try
                {
                    Thread.sleep(100);
                }
                catch (InterruptedException e)
                {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
   
    //当然这些事件也可以写在GameView中
    //触笔事件
    public boolean onTouchEvent(MotionEvent event)
    {
        return true;
    }
   
    //按键按下事件
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        return true;
    }
   
    //按键弹起事件
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
        //上方向键
        case KeyEvent.KEYCODE_DPAD_UP:
            mGameView.y-=3;
            break;
        //下方向键
        case KeyEvent.KEYCODE_DPAD_DOWN:
            mGameView.y+=3;
            break;
        }
        return false;
    }
   
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
    {
        return true;
    }
}