2015-08-15 20 views
1
public class Terrain { 

    public Bitmap tile; 
    public InputStream is; 
    public ObjectAnimator moveLeft; 
    public Drawable tileDrawable; 
    public Rect bounds; 
    public ImageView terrainImage; 

    public int top = 0; 
    public int bottom = 0; 
    public int left = 0; 
    public int right = 0; 
} 

TerrainFactory類我創建ImageView的將返回所有錯誤的值,其邊界

public class TerrainFactory { 

    public Terrain createNewTerrain(Activity activity, RelativeLayout relativeLayout, 
              final ArrayList<Terrain> terrainArrayList){ 

     terrain = new Terrain(); 
     terrain.is = activity.getResources().openRawResource(+R.drawable.game_tile); 
     terrain.tile = BitmapFactory.decodeStream(terrain.is); 
     terrain.tileDrawable = new BitmapDrawable(activity.getResources(), terrain.tile); 
     terrain.terrainImage = new ImageView(activity); 
     terrain.terrainImage.setImageDrawable(terrain.tileDrawable); 
     relativeLayout.addView(terrain.terrainImage); 

     terrain.terrainImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

      public void onGlobalLayout() { 

       terrain.left = terrain.terrainImage.getLeft(); 
       terrain.right = terrain.terrainImage.getRight(); 
       terrain.bottom = terrain.terrainImage.getBottom(); 
       terrain.top = terrain.terrainImage.getTop(); 

       Log.d("Left ", "Bounds are " + terrain.left); 
       Log.d("Right ", "Bounds are " + terrain.right); 
       Log.d("Bottom ", "Bounds are " + terrain.bottom); 
       Log.d("Top ", "Bounds are " + terrain.top); 
       terrain.terrainImage.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      } 
     }); 
     return terrain; 
    } 
} 

問題是,當我真正運行這個,這是我的日誌告訴我:

  • 左:邊界是0
  • 右:邊界是200
  • 底部:邊界150
  • 上:邊界是0

這裏有一個視覺,如果它的任何幫助: enter image description here

而且,我相信,在頂部的動作條可能有一些與此有關,但我我不太確定。

它不應該告訴我,我的圖像的上限是0.我收回的所有四個數字都是錯誤的。

另外,我知道我不應該在一個問題中提出兩個問題,但我該如何擺脫最左邊那個令人討厭的25像素邊距? 我已經嘗試進入dimens.xml並將所有邊距設置爲0dp,但似乎無法修復它。

不知道這是否是相關的,但這裏是我的PlayActivity.xml,在那裏發生的一切:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.wordpress.somegame.runrun.activity.PlayActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:layout_alignParentBottom="true" 
     android:id="@+id/relativeLayout" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     ></RelativeLayout> 

</RelativeLayout> 

UPDATE:

看來,如果我創建了非嵌套RelativeLayoutImageView,它會返回正確的數字,除了頂部,這可能是因爲操作欄。但是,如果我在嵌套RelativeLayout中生成一個圖像,我最終會得到完全錯誤的數字,所以我認爲它可能與我的XML有關,也許是?但我似乎無法弄清楚爲什麼它適用於非嵌套RelativeLayout,但不適用於嵌套RelativeLayout

回答

0

getLeft(),getRight()getBottom()getTop()返回值相對於父,而不是屏幕座標。由於您將ImageView添加到RelativeLayout作爲沒有特殊佈局參數的第一個孩子,因此它將具有(頂部,左側)==(0,0)。

對於額外的25個像素,您可以在佈局上放置一些彩色背景以查看它們的跨度;或者你可以用ddms檢查佈局轉儲。我懷疑這是你主題的一部分。開始在佈局上將邊距和填充都設置爲0,以查看是否屬於這種情況。

+0

那麼我怎樣才能得到ImageView的getLeft(),getRight(),getBottom()和getTop()相對於它的屏幕座標? – Henry98

+0

另外,我正在使用默認主題。 – Henry98

+0

嘗試'View.getLocationOnScreen' –