先看图:
小球碰到边上反弹,下面的横杆接住小球让其再反弹上去。很简单,大家都见过的啦。
这里主要练习SurfaceView,Callback以及在SurfaceView中使用线程,看主要的View类:
package com.wt.app;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.Paint.FontMetrics;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.SurfaceHolder.Callback;public class GameView extends SurfaceView implements Callback,Runnable{ private static final String TAG="GameView"; SurfaceHolder holder=null; int x,y=0; int speedX=0,speedY=0; int ball_x,ball_y=0; int ball_speedX=5,ball_speedY=5; int r=5; public GameView(Context context) { super(context); // TODO Auto-generated constructor stub holder=this.getHolder(); holder.addCallback(this); this.setFocusable(true); } 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 x=(Consts.screenWidth-50)>>1; y=(Consts.screenHeight-10); ball_x=10; ball_y=10; new Thread(this).start(); } public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } boolean isKeyDown=false; boolean isRunning=true; public void run() { // TODO Auto-generated method stub Paint paint=new Paint(); while(isRunning){ move(); paint(paint); try { Thread.sleep(15); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void paint(Paint paint) { // TODO Auto-generated method stub Canvas canvas=holder.lockCanvas(); //刷屏 canvas.drawColor(Color.WHITE);// paint.setColor(Color.WHITE);// canvas.drawRect(0, 0, Consts.screenWidth, Consts.screenHeight, paint); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLACK); canvas.drawRect(x, y, x+50, y+10, paint); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); paint.setColor(Color.RED); canvas.drawCircle(ball_x, ball_y, r, paint); if(gameOver){ paint.setStrokeWidth(1); paint.setColor(Color.RED); paint.setTextSize(25); FontMetrics fm=paint.getFontMetrics(); int textHeight=(int)Math.ceil((fm.descent-fm.top)); float textWidth = paint.measureText("Game Over"); canvas.drawText("Game Over", (Consts.screenWidth-textWidth)/2, (Consts.screenHeight-textHeight)/2, paint); } holder.unlockCanvasAndPost(canvas); } boolean gameOver=false; public void move(){ if(isKeyDown){ x+=speedX; if(x<0){ x=0; } if(x>Consts.screenWidth-50){ x=Consts.screenWidth-50; } } ball_x+=ball_speedX; ball_y+=ball_speedY; if(ball_x<r) ball_speedX=-ball_speedX; if(ball_x>Consts.screenWidth-r) ball_speedX=-ball_speedX; if(ball_y<r) ball_speedY=-ball_speedY; Rect r1=new Rect(); r1.set(x, y, x+50, y+10); Rect r2=new Rect(); r2.set(ball_x-r, ball_y-r, ball_x+r, ball_y+r); if(Tools.isCollideWith(r1, r2)){ ball_speedY=-ball_speedY; } if(ball_y>Consts.screenHeight){ gameOver=true; isRunning=false; } }}
Activity类:
package com.wt.app;import android.app.Activity;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.Display;import android.view.KeyEvent;import android.view.Window;import android.view.WindowManager;public class GameActivity extends Activity { /** Called when the activity is first created. */ private static final String TAG="GameActivity"; GameView view; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //无标题栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //窗口宽高 DisplayMetrics dm=new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); Consts.screenWidth=dm.widthPixels; Consts.screenHeight=dm.heightPixels; // Consts.screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();// Consts.screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); Log.i(TAG,"Consts.screenWidth="+Consts.screenWidth); Log.i(TAG,"Consts.screenHeight="+Consts.screenHeight);// setContentView(R.layout.main); view=new GameView(this); setContentView(view); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub view.isKeyDown=true; switch (keyCode) { case KeyEvent.KEYCODE_DPAD_LEFT: view.speedX=-5; break; case KeyEvent.KEYCODE_DPAD_RIGHT: view.speedX=5; break; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { // TODO Auto-generated method stub view.isKeyDown=false; view.speedX=0; return super.onKeyUp(keyCode, event); } }
其他辅助类如下:
package com.wt.app;import android.graphics.Rect;public class Tools { public static final boolean isCollideWith(int ax,int ay,int aw,int ah,int bx,int by,int bw,int bh){ if(ay-by>bh||ax-bx>bw||by-ay>ah||bx-ax>aw){ return false; } return true; } public static final boolean isCollideWith(Rect r1,Rect r2){ if(r2.left-r1.left>=r1.width()||r1.left-r2.left>=r2.width()||r2.top-r1.top>=r1.height()||r1.top-r2.top>=r2.height()){ return false; } return true; }}
package com.wt.app;public class Consts { public static int screenWidth; public static int screenHeight;}
适合新手练习用!