2014-02-28 202 views
0

我需要使用view.getWidth(),但我不能因爲我沒有膨脹視圖呢。所以在使用view.getWidth()之前,我怎樣才能膨脹視圖。膨脹視圖

MainAvtivity

 public class MainActivity extends Activity { 
    GameView gv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.game_view); 
     LinearLayout surface = new LinearLayout(this); 
     surface = (LinearLayout)findViewById(R.id.surface); 
     gv= new GameView(this,surface); 
     surface.addView(gv); 
} 

GameView類:

 public class GameView extends SurfaceView implements Runnable { 

     private boolean isRunning=false; 
     private final long FPS=12; 
     int PART_SIZE=6; 

     private SurfaceHolder holder; 
     Thread snakethread; 
     Snake snake; 
     Arena a; 
     Brick food; 
     LinearLayout view; 
     Bitmap arena; 


    public GameView(Context context,LinearLayout view) { 
     super(context); 
     this.view=view; 
     arena = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); 
     this.a=new Arena(); 
     snakethread=new Thread(this); 
     holder= getHolder(); 
     this.setBackgroundColor(Color.TRANSPARENT);     
     this.setZOrderOnTop(true); 
     getHolder().setFormat(PixelFormat.TRANSPARENT); 

     holder.addCallback(new SurfaceHolder.Callback(){ 

      @Override 
      public void surfaceCreated(SurfaceHolder arg0) { 
       setRunning(true); 
       snakethread.start(); 

      } 

      @Override 
      public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,int arg3) { 



      } 

      @Override 
      public void surfaceDestroyed(SurfaceHolder arg0) { 

       setRunning(false); 

       while(true){ 
        try { 
         snakethread.join(); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
    }); 
     snake=new Snake(20, 50, "RIGHT",PART_SIZE); 
} 

    public void run() { 
     long stepPerSecond=1000/FPS; 
     long startTime; 
     long sleepTime; 
     Canvas c = null; 
     try{ 
      c=this.getHolder().lockCanvas(); 
      synchronized (this.getHolder()) { 
       ArenaDraw(c); 
       } 
     } 
     catch(Exception e){ 
     } 
     finally{ 
      if(c!=null){ 
       this.getHolder().unlockCanvasAndPost(c); 
      } 
     } 
     while(isRunning){ 
      c=null; 
      startTime=System.currentTimeMillis(); 
      try{ 
       c=this.getHolder().lockCanvas(); 
       synchronized (this.getHolder()) { 
        Paint p=new Paint(); 
        snakeDraw(c); 
        } 
      } 
      catch(Exception e){ 
      } 
      finally{ 
       if(c!=null){ 
        this.getHolder().unlockCanvasAndPost(c); 
       } 
      } 
      sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime); 
      if(sleepTime>0) 
       try { 
        Thread.sleep(sleepTime); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
     } 
    } 

回答

1

您可以使用ViewTreeObserver

final ViewTreeObserver observer = surface.getViewTreeObserver(); 
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
    public boolean onPreDraw() { 
     observer.removeOnPreDrawListener(this); 
     // do what you need to do, getWidth() is now valid 
     surface.getWidth(); 
     return false; // will not draw this initial frame 
    } 
}); 

請注意,這是不夠的膨脹的觀點。您的視圖需要在getWidth()有效之前通過膨脹度量佈局生命週期。

查看更多about how android draws views

+0

我應該在哪裏添加它?我在 surface =(LinearLayout)findViewById(R.id.surface)之後添加了它。在mainActivity中,它的傭人錯誤 – Omer

+0

你可以在'surface'被初始化後的任何地方將它添加到'onCreate()'中。請注意,您應該在'onPreDraw'內創建'GameView'對象,因爲GameView的構造函數依賴於'surface.getWidth()'來正確工作。 – sulai