package game.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class GameTest extends Activity implements Runnable {
/** Called when the activity is first created. */
MyView myView;
Handler handler;
boolean bExit = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐去电池等图标和一切修饰部分(状态栏部分)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 隐去标题栏(程序的名字)
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
myView = new MyView(this);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
myView.invalidate();
}
};
setContentView(myView);
new Thread(this).start();
}
@Override
public void run() {
while (!bExit) {
handler.sendEmptyMessage(0);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyView extends View {
private Paint paint;
private int left;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true);// 设置画笔无锯齿(如果不设置可以看到效果很差)
this.setKeepScreenOn(true);// 设置背景常亮
paint.setColor(Color.RED);
left = 0;
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawColor(Color.WHITE);// 设置刷屏颜色
Rect rect = new Rect(left, 20, left + 10, 30); // 这里最后两个参数不是宽高、而是矩形右下角的坐标
canvas.drawRect(rect, paint);
left += 10;
if(left > 200)
left = 0;
}
}