View,是Android的一个超类,这个类几乎包含了所有的屏幕类型。每一个View都有一个用于绘图的画布,这个画布可以进行任意扩展。
在游戏开发中叶可以自定义视图(View),这个画布的功能更能满足我们在游戏开发中的需要。在Android中,任何一个View类都只需重写onDraw 方法来实现界面显示,自定义的视图可以是复杂的3D实现,也可以是非常简单的文本形式等。
游戏的核心是不断地绘图和刷新界面,Android中提供了 invalidate 方法来实现界面刷新。注意,invalidate 不能直接在线程中调用, 就是不可以在子线程中调用,因此它违背了 Android的单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI 线程中执行,因此Android中最常用的方法就是利用Handler来实现UI线程的更新。 其实用 AsyncTask 也可以。
具体示例:
Activity:
- public class Activity01 extends Activity {
- private static final String TAG = "mThread";
- private static final int REFRESH = 0x000001;
- private GameView mGameView = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.mGameView = new GameView(this);
- mGameView.x = 320.0f;
- mGameView.y = 120.0f;
- setContentView(mGameView);
- new Thread(new GameThread()).start();
- }
- Handler myHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case Activity01.REFRESH:
- mGameView.invalidate(); // repaint View
- break;
- }
- super.handleMessage(msg);
- }
- };
- class GameThread implements Runnable {
- @Override
- public void run() {
- while (!Thread.currentThread().isInterrupted()) {
- Message message = new Message();
- message.what = Activity01.REFRESH;
- Activity01.this.myHandler.sendMessage(message);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- if(event.getAction() == MotionEvent.ACTION_DOWN){
- mGameView.x = event.getX();
- mGameView.y = event.getY();
- }
- return true;
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode == KeyEvent.KEYCODE_BACK){
- this.finish();
- }
- return true;
- }
- }
public class Activity01 extends Activity {
private static final String TAG = "mThread";
private static final int REFRESH = 0x000001;
private GameView mGameView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mGameView = new GameView(this);
mGameView.x = 320.0f;
mGameView.y = 120.0f;
setContentView(mGameView);
new Thread(new GameThread()).start();
}
Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Activity01.REFRESH:
mGameView.invalidate(); // repaint View
break;
}
super.handleMessage(msg);
}
};
class GameThread implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Message message = new Message();
message.what = Activity01.REFRESH;
Activity01.this.myHandler.sendMessage(message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
mGameView.x = event.getX();
mGameView.y = event.getY();
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
this.finish();
}
return true;
}
}
gameview
- public class GameView extends View {
- int count = 0;
- float x = 0, y = 0;
- public GameView(Context context) {
- super(context);
- }
- public void onDraw(Canvas canvas) {
- if (count < 100) {
- count++;
- } else {
- count = 0;
- }
- Paint mPaint = new Paint();
- switch (count % 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(x - 40, y - 20, x + 40, y + 20, mPaint);
- }
- }
public class GameView extends View {
int count = 0;
float x = 0, y = 0;
public GameView(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
if (count < 100) {
count++;
} else {
count = 0;
}
Paint mPaint = new Paint();
switch (count % 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(x - 40, y - 20, x + 40, y + 20, mPaint);
}
}
运行效果: