2012-04-23 286 views
3

一切正常,當我開始我的應用程序。但是當我進入下一個活動並返回SurfaceView時,它看起來像black。當我調試時,我可以看到需要在畫布上繪製的線條,但沒有顯示出來。
這裏是我的代碼:恢復到SurfaceView顯示黑屏

public class DrawView extends SurfaceView implements Runnable 
{ 

    Thread thread = null; 
    SurfaceHolder holder; 
    Bitmap background; 
    Canvas canvas; 
    Matrix matrixUP = new Matrix(); 
    Matrix matrixDOWN = new Matrix(); 
    boolean ok = false; 

    public DrawView(Context context) 
    { 
     super(context); 
     holder = getHolder(); 
    } 

    public void run() 
    { 
     while(ok) 
     { 
      if(!holder.getSurface().isValid()) 
      { 
       continue; 
      } 
      watch_hand = Bitmap.createScaledBitmap(watch_hand, width/4, height/6, false); 
      watch_hand_down = Bitmap.createScaledBitmap(watch_hand_down, width/4, height/6, false); 
      background = BitmapFactory.decodeResource(getResources(), R.drawable.needles); 
      background = Bitmap.createScaledBitmap(background, width, height/2+20, false); 
      canvas = holder.lockCanvas(); 
      matrixUP.setTranslate(CENTER_X - watch_hand.getWidth()/2, height/4 - watch_hand.getHeight() - 40); 
      matrixUP.preRotate((float) degreUP , watch_hand.getWidth()/2 , watch_hand.getHeight()-10); 

      matrixDOWN.setTranslate(CENTER_X - watch_hand_down.getWidth()/2, height/2 - watch_hand_down.getHeight() - 20); 
      matrixDOWN.preRotate((float) degreDOWN , watch_hand_down.getWidth()/2 , 10); 

      canvas.drawARGB(255, 255, 255, 255); 
      canvas.drawBitmap(background, 0, 0, null); 
      canvas.drawBitmap(watch_hand, matrixUP, null); 
      canvas.drawBitmap(watch_hand_down, matrixDOWN, null); 
      holder.unlockCanvasAndPost(canvas); 
     }   
    } 

    public void pause() 
    { 
     ok = false; 
     while(true) 
     { 
      try 
      { 
       thread.join(); 
      }catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
      break; 
     } 
     thread = null; 
    } 

    public void resume() 
    { 
     ok = true; 
     thread = new Thread(this); 
     thread.start(); 
    }   
} 

當你看到我使用的是恢復和暫停功能。他們從onCreate被調用。 爲什麼會發生這種情況,我該如何解決這個問題?

謝謝!

+0

同樣的事情發生在我的情況下,你可以分享的解決方案。 – 2013-03-23 13:33:10

回答

0

從表面視圖所在活動的onResume()和onPause()方法調用表面視圖(在您的情況下爲DrawView)的resume()和pause()方法。你提到你是從onCreate()調用的。

這應該解決問題。

1

答案取決於你如何改變其他活動。

基本上,當活動暫停,停止或破壞時,您必須考慮如何存儲表面的當前狀態。

例如,您可以在您的活動中重載onSaveInstanceState以保存degreUP和degreDOWN(通過它們未在當前代碼段中定義的方式)。並在onRestoreInstanceState中恢復它們。請參閱Saving Android Activity state using Save Instance State

+0

degreUP正在設置「onclick」。當我調試一切正常。 canvas.drawARGB(255,255,255,255)行不關聯任何東西,但仍然沒有顯示。這是爲什麼? – roiberg 2012-04-23 09:41:43

+0

就像我說過的,真正的問題是你沒有實現恢復你的應用程序的手段。 – 2012-04-23 09:43:38

+0

我建議閱讀我發佈的鏈接。 – 2012-04-23 09:43:56