簡體   English   中英

ImageView返回其邊界的所有錯誤值

[英]ImageView is returning all the wrong values for its bounds

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

    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
  • Top:界限為0

這是視覺效果,如果有幫助的話: 在此處輸入圖片說明

另外,我相信頂部的操作欄可能與此有關,但我不太確定。

它不應該告訴我圖像的上限是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>

更新:

看來,如果我在非嵌套的RelativeLayout創建一個ImageView ,它會返回除頂部以外的正確數字,這可能是由於操作欄而導致的。

但是,如果我在嵌套的RelativeLayout創建圖像,最終將得到完全錯誤的數字,因此我認為它可能與XML有關,也許嗎? 但是我似乎無法弄清楚為什么它適用於非嵌套的RelativeLayout ,但是不適用於嵌套的RelativeLayout

getLeft()getRight()getBottom()getTop()返回相對於父級而不是屏幕坐標的值。 由於您是將ImageView作為第一個子級添加到RelativeLayout ,而沒有特殊的布局參數,因此它將具有(top,left)==(0,0)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM