先看图:
小球碰到边上反弹,下面的横杆接住小球让其再反弹上去。很简单,大家都见过的啦。
这里主要练习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 meth