0

我有一個視圖,我需要獲取它在屏幕上的頂部和底部位置。所以,我跑瞭如下實驗: 試圖找出什麼打矩形呢,我跑瞭如下實驗如何獲得視圖的矩形外圍

int[] location = { 0, 0 }; 
myView.getLocationOnScreen(location); 
Rect outRect = new Rect(); 
myView.getHitRect(outRect); 

然後打印並得到

outRect.top = 18 
outRect.bottom = 138 
location[0] = 216 
location[1] = 387 

我期待outRect.toplocation[1]是相同。

那麼,如何獲得矩形視圖的頂部和底部位置?

+0

檢查http://stackoverflow.com/questions/5730240/android-get-bounding-rectangle-of-a-view – Ranjit

回答

1

getLocationOnScreen()返回視圖相對於屏幕的X和Y位置:您可以通過將getHeight()的值添加到Y位置來計算底部位置。 getHitRect()返回視圖相對於父視圖的位置,這就是爲什麼你得到不同的數字。