2015-06-19 152 views
-1

我有隨機繪製紅色圓圈的代碼。直到我在BOTH類中暫停並恢復方法之後,它才起作用。如果沒有暫停和恢復方法,屏幕將只是黑色而不會改變。爲什麼我需要和onResume方法以及爲什麼在這兩個類中?Android - onPause和onResume

評論代碼是所有的暫停/恢復方法。

public class RandomCircles extends Activity { 

    MySurfaceView mySurfaceView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mySurfaceView = new MySurfaceView(this); 
     setContentView(mySurfaceView); 
    } 


/* @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     mySurfaceView.onResumeMySurfaceView(); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     mySurfaceView.onPauseMySurfaceView(); 
    }*/ 

    class MySurfaceView extends SurfaceView implements Runnable{ 

     Thread thread = null; 
     SurfaceHolder surfaceHolder; 
     volatile boolean running = false; 

     private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     Random random; 

     public MySurfaceView(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
      surfaceHolder = getHolder(); 
      random = new Random(); 
     } 

     /*public void onResumeMySurfaceView(){ 
      running = true; 
      thread = new Thread(this); 
      thread.start(); 
     } 

     public void onPauseMySurfaceView(){ 
      boolean retry = true; 
      running = false; 
      while(retry){ 
       try { 
        thread.join(); 
        retry = false; 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }*/ 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      while(running){ 
       if(surfaceHolder.getSurface().isValid()){ 
        Canvas canvas = surfaceHolder.lockCanvas(); 
        //... actual drawing on canvas 

        int x = random.nextInt(getWidth()); 

        if(getWidth() - x < 100) 
         x -= 100; 
        else if(getWidth() - x > getWidth() - 100) 
         x += 100; 

        int y = random.nextInt(getHeight()); 

        if(getHeight() - y < 100) 
         y -= 100; 
        else if(getHeight() - x > getHeight() - 100) 
         y += 100; 

        int radius; 
        radius = 100; 
        Paint paint = new Paint(); 
        paint.setStyle(Paint.Style.FILL); 
        paint.setColor(Color.WHITE); 
        canvas.drawPaint(paint); 
        // Use Color.parseColor to define HTML colors 
        paint.setColor(Color.parseColor("#CD5C5C")); 
        canvas.drawCircle(x, y, radius, paint); 

        surfaceHolder.unlockCanvasAndPost(canvas); 
       } 
      } 
     } 

    } 
} 
+2

我懷疑你需要兩個。但是,onResume很有意義。這是有道理的,否則你的線程不會啓動。我假設你沒有寫這個代碼? – njzk2

+0

我沒有,但我試圖理解它爲什麼工作 – Sygnerical

+0

所以我開始添加/拿走東西,看看會發生什麼 – Sygnerical

回答

1

前兩個onPause()onResume()方法是Activity生命週期的一部分,並且當Activity暫停調用/恢復。

此圖顯示了Android Activity的生命週期。你可以在它HERE

enter image description here

讀了它與您的Activity附加onResume和方法,是因爲你從Activity各自的方法調用你SurfaceViewonPauseMySurfaceView()onResumeMySurfaceView()方式的原因。如果你沒有這樣做,你的SurfaceView方法永遠不會被調用,因此永遠不會停止/啓動線程。

+0

但我沒有onDestroy或onStop方法,是那些自動或不需要? – Sygnerical

+0

它們在您擴展的Activity類中實現。你只需要覆蓋他們,如果你需要使用他們 – Neil

+0

父功能爲onResume和onPause做什麼? – Sygnerical