編輯:關於Android編程
按鍵盤的上下鍵矩形就會上下移動;
通過實例化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; } }
Android mvp 架構的自述中我簡單的介紹了mvp,以及怎麼寫mvp。我自己也將mvp運用到了項目中,其實mvp並沒有固定的寫法,正確的去理解架構的思想,都可以有自
當setCanceledOnTouchOutside(true),點擊陰影處,dialog dismiss時鍵盤不消失的問題。如圖一開始覺得很簡單,監聽下onDimiss
在項目中做了列表頁面和詳情頁面,用到了以下幾個知識點,在這裡和大家分享一下:1.數據庫模塊的完善:1.1升級數據庫,抽出版本字段;如果xxx.db 數據庫已經存在了,之後
為了實現android activity之間的數據傳遞,主要利用意圖Intent 對象的方法來完成。 基本數據類型的傳遞是相當簡單了,主要通過如下方式完成如下: putE