我正在Android上开发一款Reversi游戏,并寻找一种在每次移动后刷新视图的有效方法。我知道我不能在主线程上做到这一点,所以我想到了AsyncTask。
很高兴听到任何想法;)
好的,我认为在这种情况下代码不是必需的,但是我们走了:
a)我的游戏视图类:
public class GameView extends SurfaceView{
SurfaceHolder holder;
ViewRefreshThread vrt;
Game game;
Drawer drawer;
int player;
public GameView (Context context)
{
super(context);
holder = getHolder();
vrt = new ViewRefreshThread (this);
game = new Game();
drawer = new Drawer(context, game);
player = GameBoard.BLACK;
holder.addCallback(new SurfaceHolder.Callback(){
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(vrt.isNew == true)
{
vrt.isNew = false;
vrt.start();
}
else vrt.run = true;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
vrt.run = false;
}
});
//AsyncUpdate u = new AsyncUpdate();
//u.execute(this);
}
public void pause()
{
vrt.run = false;
}
public void resume()
{
vrt.run = true;
}
/**
* Function recalculating the coordinates of the pressed screen location to the game board positon
* @param float touchX - x coordinate in pixels of the pressed location
* @param float touchY - y coordinate in pixels of the pressed location
* @return int[] result - array of the calculated coordinates
*/
public int[] coordsToField (float touchX, float touchY)
{
int chunk = this.getWidth()/8;
int x, y;
x = y = 0;
for(int i = 0; i < 8; i++)
{
if(chunk*(i+1) > touchX)
{
x = i;
break;
}
}
for(int j = 0; j < 8; j++)
{
if(chunk*(j+1) > touchY)
{
y = j;
break;
}
}
int[] result = {x, y};
return result;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
boolean move = false;
int[] arr = coordsToField (event.getX(), event.getY());
Field f = new Field (arr[0], arr[1]);
/*player's move */
if(!game.isAvailable(game.game_board, f))
{
return false;
}
move = game.move(f, game.game_board, game.getActive_player());
game.changePlayer();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*computer's move */
int depth = 0;
f = game.alg.findBestMove(game.game_board, depth, game.getActive_player());
if (f!=null)
{
game.move(f, game.game_board, game.getActive_player());
}
game.changePlayer();
return super.onTouchEvent(event);
}
public int changePlayer (int player)
{
return player == GameBoard.BLACK ? GameBoard.WHITE : GameBoard.BLACK;
}
}
B)我的新课程:
public class ViewRefreshThread extends Thread
{
GameView gview;
private final static int FPS = 100;
protected boolean run = true;
protected boolean isNew = true;
public ViewRefreshThread(GameView gview) {
this.gview = gview;
}
@Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (true) {
if (!run) {
try {
Thread.sleep(100L);
} catch (InterruptedException ignore) {
}
continue;
}
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = gview.getHolder().lockCanvas();
synchronized (gview.getHolder()) {
gview.drawer.draw(c);
}
}
finally {
if (c != null) {
gview.getHolder().unlockCanvasAndPost(c);
gview.postInvalidate();
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {
}
}
}
}