android SurfaceView onDraw方法随机停止处理

时间:2013-07-19 21:58:45

标签: android multithreading view drawing surfaceview

我创建了SurfaceView类,用于通过onTouch方法在视图上绘图...我已经阅读并学习了一些关于SurfaceView和Drawing Activity的示例代码,并创建了以下类:

public class DrawingSurface extends SurfaceView implements
        SurfaceHolder.Callback{
    private DrawingThread drawingthread;
    public Paint mPaint;

    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;

    private float cx = 0, cy = 0;
    private boolean easer = false;
    private boolean touch = false;
    private Paint mEarserPaint;
    int count = 0;


    public DrawingSurface(Context context, AttributeSet attrs) {
        super(context, attrs);

         getHolder().addCallback(this);

    }

    public DrawingSurface(Context context) {
        super(context);

         getHolder().addCallback(this);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        System.out.println("onSizeChange");
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        System.out.println("onSurfaceChange");

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        System.out.println("onSurfaceCreated");
        // For drawing that is called in the onDraw method
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xF0000000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);


        mEarserPaint = new Paint();
        mEarserPaint.setAntiAlias(true);
        mEarserPaint.setDither(true);
        mEarserPaint.setColor(0xF0000000);
        mEarserPaint.setStyle(Paint.Style.STROKE);
        mEarserPaint.setStrokeJoin(Paint.Join.ROUND);
        mEarserPaint.setStrokeCap(Paint.Cap.ROUND);
        mEarserPaint.setStrokeWidth(12);


        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

         drawingthread = new DrawingThread(getHolder(), this);
         drawingthread.setRunning(true);
         drawingthread.start();
         setFocusable(true);


    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        System.out.println("OnDestroy");
        boolean retry = true;
        drawingthread.setRunning(false);
        while (retry) {
            try {
                drawingthread.join();
                retry = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void onDraw(Canvas canvas) {

        // on earser mode draw circal on touch
        if (easer && touch) {
            canvas.drawColor(0xFFAAAAAA);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawCircle(cx, cy, 50, mEarserPaint);

        } else {
            canvas.drawColor(0xFFAAAAAA);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        // get the touch postion for drawing the circal
        cx = event.getX();
        cy = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            touch = true;
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            touch = true;
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            touch = false;
            break;
        }
        return true;
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }

        if (easer)
            mCanvas.drawPath(mPath, mPaint);
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath, mPaint);
        mPath.reset();
    }

    public void onAttributeChange(Paint paint, boolean e) {
        mPaint = paint;
        easer = e;
    }

    public Bitmap getDrawingSurface() {
        return mBitmap;
    }

}

这是SurfaceView的线程类:

public class DrawingThread extends Thread {
    private SurfaceHolder drawingHolder;
    private DrawingSurface drawingSurface;
    private boolean run = false;

    public DrawingThread(SurfaceHolder surfaceholder, DrawingSurface surfaceview) {
        drawingHolder = surfaceholder;
        drawingSurface = surfaceview;
    }

    public void setRunning(boolean running) {
        run = running;
    }

    @Override
    public void run() {
        Canvas c;
        while (run) {
            c = null;
            try {
                c = drawingHolder.lockCanvas(null);
                if (c != null) {
                    synchronized (drawingHolder) {
                        drawingSurface.onDraw(c);
                    }
                }
            }finally {
                if (c != null)
                    drawingHolder.unlockCanvasAndPost(c);
            }

        }
    }

}

这在开始时运行良好,但是在我继续绘图的同时(在5秒到3分钟之间)它停止在视图上随机绘制(不会崩溃)...我认为onDraw方法停止处理我不知道不知道为什么,日志中没有异常,当onDraw停止响应我的触摸时,不会调用onDestory方法。

希望你能帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要使用DrawingThread。您的DrawingSurface类重写onDraw并调用invalidate()以请求重绘它。这应该足以做你想做的事。

我会注释掉DrawingThread,看看它是否神奇地为你而来。我写了一个网络白板应用程序,它从看起来像你开始的相同示例代码开始。