2014-02-11 134 views
0

我是初學者在Java和我的Android應用程序多線程,我有這個SurfaceView至極繪製一個隨機的圓,但我希望能夠通過按屏幕(ACTION_DOWN)暫停該繪圖,而接下來的時間恢復它,我再按一下:在Android中暫停和恢復線程

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

      } 
    } 
    } 

    } 

其實與代碼圖紙可以暫停,但不能恢復

+0

你嘗試使用Thread.sleep()方法,而不是Thread.wait()? – panini

+0

哦,是的,我有,但就像我說我想手動暫停和恢復它不臨時與一些超時傳遞睡眠作爲參數,或者我錯了嗎? – user3232174

+0

那麼你已經有你的while(canPause)循環,爲什麼不把Thread.sleep(100)放在那裏而不是Thread.wait()?我認爲這應該如何工作你想如何 – panini

回答

0

爲了調用wait(),您必須對同步你正在等待的對象。

見的一個討論這個問題:Why must wait() always be in synchronized block

+0

是啊謝謝,但你可以請看看我的代碼來檢查天氣我有沒有正確的同步對象 – user3232174

+0

你不會或你不會得到錯誤。在action down處理程序中,您正在'this.mSurfaceHolder'上同步,但在'this'上調用notify()。如果你打算在'this.mSurfaceHolder'同步,那麼執行'this.mSurfaceHolder.notify()'。 在on Draw中,您在'this'上調用'wait()'而不同步。你需要做'synchronized(this.mSurfaceHolder){this.mSurfaceHolder.wait()}' –

+0

謝謝。我已經用你的指示更新了我的代碼,並且繪圖可以暫停但不能恢復。 – user3232174