public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback
{
private DrawThread mThread = null;
public MySurfaceView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public MySurfaceView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public MySurfaceView(Context context)
{
super(context);
init();
}
private void init()
{
Log.d(AppConstants.LOG_TAG, "init");
SurfaceHolder holder = getHolder();
holder.addCallback(this);
mThread = new DrawThread(holder);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
Log.d(AppConstants.LOG_TAG, "onSizeChanged");
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.d(AppConstants.LOG_TAG, "surfaceCreated");
mThread.setRun(true);
mThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Log.d(AppConstants.LOG_TAG, "surfaceChanged");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.d(AppConstants.LOG_TAG, "surfaceDestroyed");
mThread.setRun(false);
}
/**
* 绘制线程类
*
*/
public class DrawThread extends Thread
{
private SurfaceHolder mHolder = null;
private boolean isRun = false;
public DrawThread(SurfaceHolder holder)
{
Log.d(AppConstants.LOG_TAG, "DrawThread Constructor");
mHolder = holder;
}
public void setRun(boolean isRun)
{
Log.d(AppConstants.LOG_TAG, "DrawThread setRun: " + isRun);
this.isRun = isRun;
}
@Override
public void run()
{
Log.d(AppConstants.LOG_TAG, "DrawThread run");
int count = 0;
while (isRun)
{
Canvas canvas = null;
synchronized (mHolder)
{
try
{
Log.d(AppConstants.LOG_TAG, "Drawing-------------");
canvas = mHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
Paint p = new Paint();
p.setColor(Color.BLACK);
Rect r = new Rect(100, 50, 300, 250);
canvas.drawRect(r, p);
canvas.drawText("这是第" + (count++) + "秒", 100, 310, p);
Thread.sleep(1000);// 睡眠时间为1秒
}
catch (Exception e)
{
Log.d(AppConstants.LOG_TAG, "throw Exception in run");
e.printStackTrace();
}
finally
{
if (null != canvas)
{
mHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
}