View和SurfaceView都可以用于绘制图形,但各有各的适用场合。一般情况,主动更新,不考虑UI线程的限制,双缓存加速等情况下会优先考虑SurfaceView。
下面把上篇中的MyView通过继承SurfaceView来重新编写,具体如下:
public class MyView extends SurfaceView implements Callback, Runnable {
private Paint mPaint;
private SurfaceHolder mSurfaceHolder;
private Thread mThread;
private void initial() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
this.setKeepScreenOn(true);
mPaint.setColor(Color.RED);
mThread = new Thread(this);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
}
public MyView(Context context) {
super(context);
initial();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
initial();
}
private void draw() {
Canvas mCanvas = null;
t