GameView.java
上传用户:zjhndq
上传日期:2021-05-06
资源大小:26k
文件大小:2k
- package com.guomin.simEngin;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.os.Handler;
- import android.os.Message;
- import android.view.KeyEvent;
- import android.view.View;
- public class GameView extends View {
-
- Thread t1;
-
- long tag=0;
- int tagX = 100,tagY = 100;
-
- private long mMoveDelay = 300;
- private long mLastMove;
-
- Paint paint = new Paint();
-
- GameView(Context context){
- super(context);
- setFocusable(true);
- update();
- }
-
- //还不知道原理的循环????
- private RefreshHandler mRedrawHandler = new RefreshHandler();
-
- class RefreshHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- GameView.this.update();
- GameView.this.invalidate();
- }
- public void sleep(long delayMillis) {
- this.removeMessages(0);
- sendMessageDelayed(obtainMessage(0), delayMillis);
- }
- };
-
- //游戏循环
- public void update() {
- long now = System.currentTimeMillis();
- if (now - mLastMove > mMoveDelay) {
- mLastMove = now;
- //处理
- tag++;
- }
- mRedrawHandler.sleep(mMoveDelay);
- }
-
- public boolean onKeyDown(int keyCode, KeyEvent msg) {
- if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
- tagY--;
- invalidate();
- return (true);
- }
- if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
- tagY++;
- invalidate();
- return (true);
- }
- if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
- tagX--;
- invalidate();
- return (true);
- }
- if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
- tagX++;
- invalidate();
- return (true);
- }
- if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER||keyCode == KeyEvent.KEYCODE_ENTER) {
- invalidate();
- return (true);
- }
-
- return super.onKeyDown(keyCode, msg);
- }
-
-
- public void onDraw(Canvas canvas){
- paint.setColor(Color.YELLOW);
- canvas.drawText(tag+"", tagX, tagY, paint);
- }
- }