在Android中暂停和恢复线程

时间:2014-02-11 00:04:00

标签: android multithreading

我是Java和我的Android应用程序中的多线程的初学者,我有一个随机绘制圆圈的SurfaceView,但我希望能够通过按屏幕(ACTION_DOWN)暂停该绘图并在下次恢复它我再次按下它:

     import android.content.Context;

     import android.content.Intent;

     import android.graphics.Canvas;

     import android.graphics.Color;

     import android.graphics.Paint;

     import android.view.MotionEvent;

     import android.view.SurfaceHolder;

     import android.view.SurfaceView;


     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 mThread;
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);
    //paint.setTextSize(15);
}


public void onDraw(Canvas canvas){

    canvas.drawCircle(x, y, radius, paint);
}
/**/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {


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

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}



public synchronized boolean onTouchEvent(MotionEvent event) { 

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



    switch (eventaction ) {
        case MotionEvent.ACTION_DOWN:

                this.mThread.canPause = !this.mThread.canPause;
            synchronized(this.mSurfaceHolder){
                if(this.mThread.canPause){
                    this.mSurfaceHolder.notify();
                }
            }


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

        break;
    }

    invalidate();
    return true;
       }
public final class DrawingThread extends Thread {



    public boolean canPause = false;

    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) {

                            while(canPause){
                                try {

                                                   mSurfaceHolder.wait();
                                }                           catch(InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                            waitThreaed();
                            draw(canvas);
                        }
                } 
                catch(Exception e){

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


       }
}

    public void waitThreaed() {

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

            }
    }
    }

   }

事实上,使用该代码,绘图可以暂停,但无法恢复

1 个答案:

答案 0 :(得分:0)

为了调用wait(),你必须在你正在等待的对象上进行同步。

请参阅此问题以进行讨论:Why must wait() always be in synchronized block