2014-02-11 213 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