1

我有自定义 SurfaceView 包含一个圆圈。我想通过触摸事件旋转这个圆圈。一切都很好,除了触摸完成时我不能暂停 SurfaveView 的 run() 方法。这是我的观点和主题

public class CircleSurfaceView extends SurfaceView implements
    SurfaceHolder.Callback {

private Circle mCircle;
private DrawThread thread;
private float mRotation = 0;

long mLastMillis = 0L;

Paint clearPaint = new Paint();

public CircleSurfaceView(Context context) {
    super(context);
    mCircle = new Circle(0, 200, 200);

    getHolder().addCallback(this);

    this.setZOrderOnTop(true);
    this.getHolder().setFormat(PixelFormat.TRANSPARENT);
    clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}

public void startDrawing() {
    if (thread == null) {
        thread = new DrawThread(this);
        thread.startThread();
    }
}

public void stopDrawing() {
    if (thread != null) {
        thread.stopThread();

        // Waiting for the thread to die by calling thread.join,
        // repeatedly if necessary
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
        thread = null;
    }
}

@Override
protected void onDraw(final Canvas canvas) {
    canvas.drawPaint(clearPaint);
    mCircle.init();
    mCircle.draw(canvas);

    super.onDraw(canvas);
}

float degrees;

public void rotateCircle() {
    mCircle.rotate(mRotation);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    float currentY = event.getY();
    float deltaX, deltaY;
    switch (event.getAction()) {

    case MotionEvent.ACTION_MOVE:
        // Modify rotational angles according to movement
        deltaX = currentX - previousX;
        deltaY = currentY - previousY;
        mRotation += deltaX + deltaY;
    }
    // Save current x, y
    previousX = currentX;
    previousY = currentY;
    rotateCircle();
    return true; // Event handled
}

private float previousX;
private float previousY;

private static int SEG_COUNT = 62;
private static float SEG_IN_GRAD = (float) 360 / SEG_COUNT;

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    startDrawing();
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    stopDrawing();
}

public class DrawThread extends Thread {
private boolean mRun = false;
private SurfaceHolder surfaceHolder = null;
private CircleSurfaceView circleSurfaceView = null;

public DrawThread(CircleSurfaceView circleSurfaceView) {
    this.circleSurfaceView = circleSurfaceView;
    this.surfaceHolder = circleSurfaceView.getHolder();
}

public void startThread() {
    if (mRun == false) {
        mRun = true;
        super.start();
    }
}

public void stopThread() {
    if (mRun == true) {
        mRun = false;
    }
}

@Override
public void run() {
    Canvas c = null;
    while (mRun) {
        c = null;
        try {
            c = surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                if (c != null) {
                    Log.i("run", "run");
                    circleSurfaceView.rotateCircle();
                    circleSurfaceView.onDraw(c);
                }
            }
        } finally {
            // do this in a finally so that if an exception is thrown
            // we don't leave the Surface in an inconsistent state
            if (c != null) {
                surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

}

4

0 回答 0