Activity类
- Java code
//ButtonTestActivity.javapackage button.text;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.Button;public class ButtonTestActivity extends Activity { /** Called when the activity is first created. */ private GameView mGameView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); Button startButton = (Button)findViewById(R.id.button1); Button optionButton = (Button)findViewById(R.id.button2); mGameView = (GameView) findViewById(R.id.gameView1); startButton.setOnClickListener(new StartListener()); } class StartListener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub mGameView.setState(GameView.GAME_START); System.out.println("已经点击start"); } }}
SurfaceView类
- Java code
//GameView.javapackage button.text;import java.io.InputStream;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;public class GameView extends SurfaceView implements SurfaceHolder.Callback,Runnable { private Bitmap startBackGround,home; private SurfaceHolder mSurfaceHolder; private Canvas mCanvas; private Paint mPaint; private Thread tR; public static int screen_width,screen_height; /**游戏主菜单状态常量*/ public static final int GAME_MENU = 0; public static final int GAME_START = 1; public static final int GAME_OPTION = 2; public static final int GAME_HELP = 3; /**游戏状态*/ public int gameState=GAME_MENU; public GameView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(); } public GameView(Context context){ super(context); init(); } public void setState(int mState){ this.gameState = mState; } public void draw(Canvas canvas,Paint paint){ switch (gameState){ case GAME_MENU: canvas.drawBitmap(startBackGround, 0, 0, paint); break; case GAME_START: canvas.drawBitmap(home, 0, 0, paint); System.out.println("已经画完home"); break; case GAME_OPTION: break; case GAME_HELP: break; } System.out.println("已经画完startBackGround"); } private void init(){ InputStream Is = this.getResources().openRawResource(R.drawable.schoolcorridor); startBackGround = BitmapFactory.decodeStream(Is); InputStream Is2 = this.getResources().openRawResource(R.drawable.home); home = BitmapFactory.decodeStream(Is2); mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); mPaint = new Paint(); System.out.println("资源加载就绪"); } public void run() { // TODO Auto-generated method stub System.out.println("开始画"+gameState); try{ mCanvas=mSurfaceHolder.lockCanvas(); long startTime=System.currentTimeMillis(); mCanvas.drawColor(Color.BLACK); draw(mCanvas,mPaint); long endTime=System.currentTimeMillis(); if(500>(endTime-startTime)){ Thread.sleep(500-(endTime-startTime)); } }catch (Exception e) { Log.e("Error", "刷新屏幕出错!"+e); }finally{ if(mCanvas!=null){ mSurfaceHolder.unlockCanvasAndPost(mCanvas); } } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub screen_width=getWidth(); screen_height=getHeight(); Thread tR = new Thread(this); tR.start(); } public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }