在Android中的SurfaceView中暂停和恢复线程

时间:2014-02-09 11:25:32

标签: android

我有一个SurfaceView,我会定期用一个特殊的线程绘制一个小圆圈 但我想当我按下surfaceView时线程停止绘制圆圈,当我压制它时,线程重新开始绘制圆圈。

我尝试使用线程方法,我可以使用sleep()方法暂时停止它,但我不明白如何使用wait和notify,我甚至找到了一些例子,但没有得到他们的帮助

我的代码是:

    public class GameView extends SurfaceView implements SurfaceHolder.Callback { 


private float x = 100;
private float y = 100;
private int radius = 20;
private Paint paint;
private SurfaceHolder mSurfaceHolder;
private DrawingThread mTh  ead;
private Context myContext;

public GameView(Context context) {
    super(context);
    this.myContext = context;
    setWillNotDraw(false);
    paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setTextAlign(Paint.Align.LEFT);
    mSurfaceHolder = getHolder();
    mSurfaceHolder.addCallback(this);
}


public void onDraw(Canvas canvas){

    canvas.drawCircle(x, y, radius, paint);
}



public boolean onTouchEvent(MotionEvent event) { 

    int eventaction = event.getAction();
    int X = (int)event.getX();
    int Y = (int)event.getY();



    switch (eventaction ) {
        case MotionEvent.ACTION_DOWN:

            // I want to do my job here


            break;
        case MotionEvent.ACTION_MOVE:
        break;
        case MotionEvent.ACTION_UP:

        break;
    }

    invalidate();
    return true;
    }
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {


        mThread = new DrawingThread(mSurfaceHolder, myContext);
        mThread.mRun = true;
        mThread.start();

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}


public final class DrawingThread extends Thread {



    public boolean att = true;

    public long delaiAttente = 1000;

    boolean mRun;

    Canvas mcanvas;

    SurfaceHolder surfaceHolder;

    Context context;



    public DrawingThread(SurfaceHolder sholder, Context ctx)

    {

    surfaceHolder = sholder;

    context = ctx;

    mRun = false;
    }



    void setRunning(boolean bRun)

    {

    mRun = bRun;

    }


    boolean keepDrawing = true;



    @Override
    public void run() {
        while (keepDrawing) {
                Canvas canvas = null;
                try {

                         canvas = mSurfaceHolder.lockCanvas();
                        synchronized (mSurfaceHolder) {
                            draw(canvas);
                        }
                } 
                catch(Exception e){

                }
                finally {
                        if (canvas != null)
                        mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
                waitThreaed();

    }
}

    public void waitThreaed() {

         try {
                    x = (float) (getWidth()*Math.random());
                    y = (float) (getHeight()*Math.random());
                    this.sleep(1000);
                    postInvalidate();
            } catch (InterruptedException e) {

            }
    }
   }

    }

1 个答案:

答案 0 :(得分:0)

你不能只使用这样的东西:

    Timer drawTimer = new Timer("draw");
    updateTimer.schedule(new TimerTask() {
        public void run() {
            draw();
        }
    }, 0, 1000);
    private void draw() {
    runOnUiThread(new Runnable() {
        public void run() { ...}}}

我不明白为什么你需要继承Thread。