以下实现在camera预览界面上显示倒计时计数
//用到的变量
//use to paint the count down time that delaying capture
private Paint mDelayTimerPainter = new Paint();
//use to clear the lastest paint of mDelayTimerPainter
private Paint mDelayTimerUnPainter = new Paint();
//the surface that use to paint by mDelayTimerPainter
private SurfaceView mDelayTimerView;
private DelayTimerOverlayHandler mDelayTimerOverlayHandler =
newDelayTimerOverlayHandler();
private int remainTime = 0;
//创建surfaceView对象
mDelayTimerView= (SurfaceView) findViewById(R.id.delay_timer_overlay);
//获取surfaceHolder
SurfaceHolder delayTimerHolder = mDelayTimerView.getHolder();
//为delayTimerHolder添加mDelayTimerOverlayHandler的回调函数
//mDelayTimerOverlayHandler中重写SurfaceHolder.Callback的三个函数
mDelayTimerOverlayHandler.setDelayTimerSurfaceHolder(delayTimerHolder);
delayTimerHolder.addCallback(mDelayTimerOverlayHandler);
//将mDelayTimerView放在其他surfaceview的顶层显示
mDelayTimerView.setZOrderMediaOverlay(true);
//先不显示该view
mDelayTimerView.setVisibility(View.GONE);
//设置mDelayTimerPainter的颜色
mDelayTimerPainter.setColor(getResources().getColor(R.color.delay_time_remain_text));
//设置mDelayTimerPainter画笔的粗细
mDelayTimerPainter.setStrokeWidth(1);
//设置mDelayTimerPainter画笔的文字大小
mDelayTimerPainter.setTextSize(24);
//这个用于绘制另一个paint,用于在每次更新倒计时前遮盖之前显示的文字
mDelayTimerUnPainter.setColor(Color.TRANSPARENT);
// PorterDuff.Mode.CLEAR可以使得绘制在surfaceview上面的都清除
mDelayTimerUnPainter.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mDelayTimerUnPainter.setStrokeWidth(1);
//继承自SurfaceHolder.Callback的mDelayTimerOverlayHandler
class DelayTimerOverlayHandler implements SurfaceHolder.Callback {
private SurfaceHolder mDelayTimerSurfaceHolder;
public final String TAG = "DelayTimerOverlayHandler";
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
mDelayTimerSurfaceHolder = holder;
mDelayTimerSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mDelayTimerSurfaceHolder = holder;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mDelayTimerSurfaceHolder = null;
}
public SurfaceHolder getDelayTimerSurfaceHolder() {
return mDelayTimerSurfaceHolder;
}
public void setDelayTimerSurfaceHolder(SurfaceHolder surfaceHolder) {
mDelayTimerSurfaceHolder = surfaceHolder;
}
}
public void onShutterButtonClick() {
//…
//按下拍照键后的处理
//将surfaceView弄为可见
mDelayTimerView.setVisibility(View.VISIBLE);
//绘制
mDelayTimerView.postInvalidate();
remainTime = delayTime;
mHandler.post(delayTimerRunnable);
}
private Runnable delayTimerRunnable = new Runnable() {
publicvoid run() {
if(remainTime> 0)
{
String time =Integer.toString(remainTime);
String countDown =getString(R.string.pref_camera_delay_snap_count_down,time);
//加锁canva
Canvas delayTimerCanvas =
mDelayTimerOverlayHandler.getDelayTimerSurfaceHolder().lockCanvas();
if (delayTimerCanvas != null) {
//清空之前绘制内容
delayTimerCanvas.drawRect(0,0,300,300, mDelayTimerUnPainter);
mDelayTimerView.postInvalidate();
//绘制倒计时计数
delayTimerCanvas.drawText(countDown, 10,150, mDelayTimerPainter);
mDelayTimerOverlayHandler.getDelayTimerSurfaceHolder().unlockCanvasAndPost(delayTimerCanvas);
mDelayTimerView.postInvalidate();
}
--remainTime;
mHandler.postDelayed(delayTimerRunnable, 1000);
}
Else
//将view不显示
mDelayTimerView.setVisibility(View.GONE);
}
};