package edu.qypt.zzh;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public abstract class GameView extends SurfaceView implements Callback,
Runnable {
private static final MotionEvent MotionEvent = null;
protected int refreshTime = 1000 / 60;
private boolean isRunning = false;
protected SurfaceHolder holder;
protected Paint paint;
protected int screenWidth;
protected int screenHeight;
protected CallbackHook callbackHook;
protected boolean su=true;
protected int n=0;
public GameView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(this);
paint = new Paint();
paint.setColor(Color.WHITE);
setFocusable(true);
setKeepScreenOn(true);
}
@Override
public void run() {
while (isRunning) {
if(su==true){
long start = System.currentTimeMillis();
play();
draw();
long end = System.currentTimeMillis();
try {
if (end - start < refreshTime) {
Thread.sleep(refreshTime - (end - start));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void addCallbackHook(CallbackHook callbackHook) {
this.callbackHook = callbackHook;
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
this.screenWidth = this.getWidth();
this.screenHeight = this.getHeight();
if (callbackHook != null) {
callbackHook.onSurfaceCreated(surfaceHolder);
}
new Thread(this).start();
isRunning = true;
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
isRunning = false;
}
protected void draw() {
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
canvas.drawRGB(0, 0, 0);
doDraw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
protected void doDraw(Canvas canvas) {
}
protected abstract void play();
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public abstract class GameView extends SurfaceView implements Callback,
Runnable {
private static final MotionEvent MotionEvent = null;
protected int refreshTime = 1000 / 60;
private boolean isRunning = false;
protected SurfaceHolder holder;
protected Paint paint;
protected int screenWidth;
protected int screenHeight;
protected CallbackHook callbackHook;
protected boolean su=true;
protected int n=0;
public GameView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(this);
paint = new Paint();
paint.setColor(Color.WHITE);
setFocusable(true);
setKeepScreenOn(true);
}
@Override
public void run() {
while (isRunning) {
if(su==true){
long start = System.currentTimeMillis();
play();
draw();
long end = System.currentTimeMillis();
try {
if (end - start < refreshTime) {
Thread.sleep(refreshTime - (end - start));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void addCallbackHook(CallbackHook callbackHook) {
this.callbackHook = callbackHook;
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
this.screenWidth = this.getWidth();
this.screenHeight = this.getHeight();
if (callbackHook != null) {
callbackHook.onSurfaceCreated(surfaceHolder);
}
new Thread(this).start();
isRunning = true;
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
isRunning = false;
}
protected void draw() {
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
canvas.drawRGB(0, 0, 0);
doDraw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
protected void doDraw(Canvas canvas) {
}
protected abstract void play();
}