HolderSurfaceView.java
package com.lyc;
import android.graphics.PixelFormat;
import android.view.SurfaceView;
public class HolderSurfaceView {
private HolderSurfaceView() {
}
private SurfaceView mSurfaceView;
private static HolderSurfaceView mHolderSurfaceView = null;
public static HolderSurfaceView getInstance() {
if (mHolderSurfaceView == null)
mHolderSurfaceView = new HolderSurfaceView();
return mHolderSurfaceView;
}
public void setSurfaceView(SurfaceView view) {
mSurfaceView = view;
mSurfaceView.setZOrderOnTop(true);
mSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
public SurfaceView getSurfaceView() {
return mSurfaceView;
}
}
DrawTopBase.java
package com.lyc;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class DrawTopBase implements Runnable, Callback ,OnTouchListener{
protected DrawStatus mStatus = DrawStatus.NoWork;
protected SurfaceView mSurfaceView;
protected SurfaceHolder mSurfaceHolder;
protected Context mContext;
protected Rect mSurfaceRect = new Rect(0, 0, 0, 0); //前两个参数是左上角的坐标,后两个参数是右下角的坐标(不是宽度和高度)
public DrawTopBase() {
setSurfaceView(HolderSurfaceView.getInstance().getSurfaceView());
}
public void setSurfaceView(SurfaceView view) {
mSurfaceView = view;
mContext = mSurfaceView.getContext();
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
// mSurfaceRect.set(new Rect(0, 0, mSurfaceView.getWidth(), mSurfaceView.getHeight()));
mSurfaceRect.set(new Rect(100, 100, 250, 250));
set();
}
public void set() {
setRect(mSurfaceRect);
}
protected Thread mThread = null;
public void begin() {
if (mThread == null) {
mThread = new Thread(this);
mThread.start();
}
}
public void end() {
mStatus = DrawStatus.Ending;
}
protected void doWork(Canvas canvas) {
}
protected void endWork() {
}
protected Paint mPaint = new Paint();
protected void clear(Canvas canvas) {
mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(mPaint);
mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC));
}
protected void clear() {
synchronized (mSurfaceHolder) {
Canvas canvas = this.mSurfaceHolder.lockCanvas();
try {
clear(canvas);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null)
mSurfaceHolder.unlockCanvasAndP