Android 遊戲開發 View架構

來源:互聯網
上載者:User

按鍵盤的上下鍵矩形就會上下移動;

通過執行個體化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.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;     } } 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;    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.