2 cielhu06 cielhu06 于 2015.06.02 19:10 提问

求助返回键退出后再次进入hold.lockCanvas()返回null

public class RecordWaveformView extends SurfaceView implements Callback {

private static final String TAG = "RecordWaveformView";
private SurfaceHolder mHolder;

private int rateX = 352;// X轴缩小的比例
private int rateY = 0;// Y轴缩小的比例
private int baseLine = 0;// Y轴基线

private int draw_time = 1000 / 15;// 绘图间隔时间
private int divider = 2;// 3个像素一个数据
private long c_time;

public RecordWaveformView(Context context) {
    this(context, null);
}

public RecordWaveformView(Context context, AttributeSet attrs) {
    super(context, attrs);
    MyLogger.i(TAG, "RecordWaveformView");
    mHolder = getHolder();
    mHolder.addCallback(this);
    setFocusable(true);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    MyLogger.i(TAG, "onMeasure");
}

public void startDraw(byte[] pcmData) {
    MyLogger.i(TAG, "startDraw");
    // initData(pcmData);
    Message msg = Message.obtain();
    msg.obj = pcmData;
    mDrawThread.mHandler.sendMessage(msg);
}

class DrawThread extends Thread {
    private Handler mHandler;
    private ArrayList<Short> inBuf = new ArrayList<Short>();
    private ByteArrayInputStream byteArrayInputStream;

    private byte[] buffer = new byte[1764];
    byte[] obj;

    public void run() {

        while (isRunning) {
            Looper.prepare();

            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    MyLogger.i(TAG, "handleMessage");
                    obj = (byte[]) msg.obj;
                    initData(obj);
                    draw(inBuf);
                }
            };

            Looper.loop();
        }

    }

    private void initData(byte[] pcmData) {
        MyLogger.i(TAG, "initData" + pcmData.length);
        byteArrayInputStream = new ByteArrayInputStream(pcmData);
        MyLogger.i(TAG, "byteArrayInputStream");
        int readsize = byteArrayInputStream.read(buffer, 0, 1764);
        int len = readsize / rateX;
        for (int i = 0; i < len; i++) {
            inBuf.add((short) ((0x0000 | buffer[i + 1]) << 8 | buffer[i]));
        }

    }

    private void draw(ArrayList<Short> inBuf) {
        // TODO Auto-generated method stub
        MyLogger.i(TAG, "draw" + inBuf.size());

        if (rateY == 0) {
            rateY = 50000 / getHeight();
            baseLine = getHeight() / 2;
        }
        Canvas mCanvas = null;
        synchronized (mHolder) {
            try {
                **mCanvas = mHolder.lockCanvas();**
                // 获得canvas
                MyLogger.i(TAG, "mHolder"+mCanvas +mHolder);
                if (mCanvas != null) {
                    Paint paint = new Paint();
                    paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
                    mCanvas.drawPaint(paint);
                    paint.setXfermode(new PorterDuffXfermode(Mode.SRC));

                    MyLogger.i(TAG, "drawColor");
                    int start = getWidth() - inBuf.size() * divider;
                    MyLogger.i(TAG, "start=" + start);
                    int py = baseLine;
                    if (inBuf.size() > 0) {
                        py += inBuf.get(0) / rateY;
                    }
                    int y;
                    mCanvas.drawLine(0, baseLine, start - divider,
                            baseLine, mWaveformPaint);
                    for (int i = 0; i < inBuf.size(); i++) {
                        y = inBuf.get(i) / rateY + baseLine;// 调节缩小比例,调节基准线
                        MyLogger.i(TAG, "y=" + y);
                        mCanvas.drawLine(start + (i - 1) * divider, py,
                                start + i * divider, y, mWaveformPaint);
                        py = y;
                    }

                }
            } catch (Exception e) {
            } finally {
                if (mCanvas != null)
                    mHolder.unlockCanvasAndPost(mCanvas);
            }
        }

    }
}

private Paint mWaveformPaint;

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    MyLogger.i(TAG, "surfaceChanged");

}

private DrawThread mDrawThread;
private boolean isRunning;

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    MyLogger.i(TAG, "surfaceCreated");
    mWaveformPaint = new Paint();
    mWaveformPaint.setColor(Color.WHITE);
    mDrawThread = new DrawThread();
    mDrawThread.start();
    isRunning = true;

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    MyLogger.i(TAG, "surfaceDestroyed");
    mDrawThread.inBuf.clear();
    isRunning = false;
}

}

1个回答

qq_22202859
qq_22202859   2015.06.03 09:27
cielhu06
cielhu06 这个是全代码,就是在一个activity中的回调方法里面不停的调用startDraw方法,这两个网页我看,解决不了
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片