SurfaceView:
自己通过另一个线程控制UI线程,可以使动画播放流畅,现对于View。比如棋类可以不用surfaceview,因为棋类是点一下动一下,需求的动画效果不高。像有小人在不断的跑动,就需要surfaceview。
基本用法:
1。 extends SurfaceView
2。implements SurfaceHolder.Callback
并实现他们必须的方法。
简单使用实例:
public class MySurfaceView extends SurfaceView implements Callback{
private Paint paint=null;
public MySurfaceView(Context context) {
super(context);
paint = new Paint();
paint.setColor(0xffe51c23);
SurfaceHolder hold = this.getHolder();
hold.addCallback(this);
}
private void draw() {
Canvas canvas = getHolder().lockCanvas();
canvas.drawColor(0xff5677fc);
canvas.save();
canvas.rotate(30, 100, 100);
canvas.drawLine(100, 100, 500, 500, paint);
canvas.restore();
canvas.drawRect(100, 100, 200, 200, paint);
getHolder().unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
draw();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
方法surfaceview里要写
SurfaceHolder hold = this.getHolder();
hold.addCallback(this);
surfaceCreated里写canvas时一定要有:
Canvas canvas = getHolder().lockCanvas();
getHolder().unlockCanvasAndPost(canvas);
在这两句中间画图。