一.新建一个工程“LearnSurfaceView”
二.新建一个类“MySurfaceView”
public class MySurfaceView extends SurfaceView implementsSurfaceHolder.Callback {
private Paint paint=null;
public MySurfaceView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.RED);
getHolder().addCallback(this);
}
public void draw(){
Canvas canvas = getHolder().lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawLine(0,0,getWidth()/2,getHeight()/2,paint);
canvas.drawCircle(getWidth()/2,getHeight()/2,50,paint);
canvas.drawRect(0,0,100,100,paint);
getHolder().unlockCanvasAndPost(canvas);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
draw();
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override
public void surfaceDestroyed(SurfaceHolder holder) { }
}
三.修改MainActivity.java文件中的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MySurfaceView(this));
}