0

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

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 方法。

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

4

1 回答 1

0

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

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

于 2013-07-20T04:36:02.540 回答