2014-02-25 48 views
0

我有這樣的XML,與佈局表面(其犯規覆蓋整個屏幕):view.getTop()reutrns錯誤的結果

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/FrameLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="10dp" 
     android:orientation="vertical" > 
    </LinearLayout> 


    <LinearLayout 
     android:id="@+id/surface" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_weight="1" > 
    </LinearLayout> 




<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="0dp" 
    android:layout_gravity="bottom" > 

    <ImageView 
     android:id="@+id/right" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignTop="@+id/down" 
     android:layout_toRightOf="@+id/down" 
     android:src="@drawable/right" /> 

    <ImageView 
     android:id="@+id/left" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignTop="@+id/down" 
     android:layout_toLeftOf="@+id/down" 
     android:src="@drawable/left" /> 

    <ImageView 
     android:id="@+id/up" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/right" 
     android:src="@drawable/up" /> 

    <ImageView 
     android:id="@+id/down" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_below="@+id/up" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/down" /> 
</RelativeLayout> 

</LinearLayout> 

我注意到surface控制在MainActivity並試圖繪製上它的邊框寬度爲10px。

這裏在MainActivity:

  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; 
     LinearLayout view; 


    public GameView(Context context,LinearLayout view) { 
     super(context); 
     this.view=view; 
     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(); 
        } 
       } 
      } 
    }); 
} 

    public void run() { 
     long stepPerSecond=1000/FPS; 
     long startTime; 
     long sleepTime; 
     Canvas c = null; 
     try{ 
      c=this.getHolder().lockCanvas(); 
      synchronized (this.getHolder()) { 
       c.drawColor(Color.BLUE); 
       Paint p=new Paint(); 
       p.setColor(Color.BLACK); 
       c.drawRect(view.getLeft(), view.getTop(), view.getLeft()+10, view.getBottom(),p); 
       c.drawRect(view.getLeft(), view.getTop(),view.getRight(),view.getTop()+10,p); 
       c.drawRect(view.getRight()-10, view.getTop(), view.getRight(),view.getBottom(),p); 
       c.drawRect(view.getLeft(), view.getBottom()-10, view.getRight(),view.getBottom(),p); 
       } 
     } 
     catch(Exception e){ 
     } 
     finally{ 
      if(c!=null){ 
       this.getHolder().unlockCanvasAndPost(c); 
      } 
     } 

    } 

的問題是,整個框架是大約6像素視圖的頂下,在這裏是應該的。我的意思是view.getTop()view.getBottom()返回一個worng結果,結果是大約6px的結果,然後應該是(如果視圖的頂部座標是x,那麼它返回x + 6)。

+1

您是否嘗試過?這聽起來很像你沒有考慮到TitleBar(是的,除非使用Theme.NoTitleBar,你必須這樣做) – Guardanis

+0

如何使用getLocationInWindow()獲得視圖的底部? – Omer

回答

1

支出後第2天,我發現使用getLocationInWindow()來獲取視圖的窗口位置解決方案

int top = view.getTop(); 
int bottom = view.getBottom(); 

// use these line instead of upper code 
int statusBarHeight = (int) Math.ceil(25 * context.getResources().getDisplayMetrics().density); 
int top = (childTextView.getBottom()+childTextView.getHeight())-statusBarHeight ; 
int bottom = (childTextView.getTop()+childTextView.getHeight())-statusBarHeight ; 
+0

嘿,我真的很感謝你的努力。 3年前我已經離開了這個項目,所以我不知道你有什麼解決方案,但我真的很佩服你試圖幫助和修復一些3年前發佈的帖子。它將有助於其他未來的問題 – Omer

+0

最受歡迎的先生 –