public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback
{
private SurfaceHolder holder;
public MySurfaceView(Context context)
{
super(context);
holder = this.getHolder();// 获取holder
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Canvas canvas = holder.lockCanvas(null);
Paint mPaint = new Paint();
mPaint.setColor(Color.WHITE);
canvas.drawRect(new RectF(40, 60, 80, 80), mPaint);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
}
SurfaceHolder.Callback
{
private SurfaceHolder holder;
public MySurfaceView(Context context)
{
super(context);
holder = this.getHolder();// 获取holder
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Canvas canvas = holder.lockCanvas(null);
Paint mPaint = new Paint();
mPaint.setColor(Color.WHITE);
canvas.drawRect(new RectF(40, 60, 80, 80), mPaint);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
}