View獲取位置

本文搬運了以下作者的內容加以整理,僅僅方便自己查閱:
1.CSDN博主「chaseDreamer_」的原創文章
原文鏈接:https://blog.csdn.net/chasedreamer_/article/details/97262581
2.《Android開發藝術探索》

1.getLocationInWindow

這個方法是將view的左上角座標存入數組中.此座標是相對當前activity而言.
若是普通activity,則y座標爲可見的狀態欄高度 + 可見的標題欄高度 + view左上角到標題欄底部的距離。
可見的意思是:在隱藏了狀態欄/標題欄的情況下,它們的高度以0計算.
若是對話框式的activity,則y座標爲可見的標題欄高度+view到標題欄底部的距離,此時是無視狀態欄的有無的。

int[] position = new int[2];  
textview.getLocationInWindow(position);  
System.out.println("getLocationInWindow:" + position[0] + "," + position[1]);  

2.getLocationOnScreen

這個方法跟上面的差不多,也是將view的左上角座標存入數組中.但此座標是相對整個屏幕而言。x、y座標爲view左上角到屏幕頂部的距離。

int[] position = new int[2];  
textview.getLocationOnScreen(position);  
System.out.println("getLocationOnScreen:" + position[0] + "," + position[1]);  

3.View中相對座標

View左上角相對於父容器的橫向偏移量

getTranslationX()  
public float getTranslationX ()

View左上角相對於父容器的豎向偏移量

getTranslationY()
public float getTranslationY ()

View原始左上角的Y座標的值,不管View怎麼移動,這個值不會發生改變

getTop () 
public final int getTop ()

View原始左上角X座標的值,同上,也是不會改變

getLeft()    
public final int getLeft ()

View原始右下角Y座標的值,同上,也是不會改變

getBottom()  
public final int getBottom ()

View原始右下角X座標的值,同上,也是不會發生改變

getRight()  
public final int getRight ()

獲取View左上角的X座標,相當於getLeft()+getTranslationX()

getX() 
public float getX ()

獲取View左上角的Y座標,相當於getTop+getTranslationY()

getY() 
public float getY()

view圍繞其旋轉和縮放的點的X位置,在view的動畫中會用得到

getPivotX()
public float getPivotX ()

view圍繞其旋轉和縮放的點的Y位置

getPivotY()  
public float getPivotY ()

獲取View的中心點座標
如果想要獲取View中心點的座標,首先你需要知道它某個角的座標和他的長和寬

int top = view.getTop();
int left = view.getLeft();
 int right = view.getRight();
 int bottom = view.getBottom();
	
//View的 寬
int viewWidth = right - left;
//View的 高
int viewHeight = bottom - top;
//中心點的Y座標
int centerY = top + viewHeight/2;
//中心點的X座標
int centerX = left + viewWidth/2;

4.檢測View是否繪製成功,獲取view的座標

1.view.post(runnable) 推薦!
通過post可以將一個runnable投遞到消息隊列的尾部,然後等待Looper調用此runnable的時候,View也已經初始化好了。這裏要注意,view.post的前提是view沒有被銷燬,對於使用了lottie庫實現的view要慎用

  protected void onStart() {
        super.onStart();
        view.post(new Runnable() {
                @Override
                public void run() {
                        int width = view.getMeasuredWidth();
                        int height = view.getMeasuredHeight();
                }
        });
    }

2. Activity/View#onWindowFocusChanged
onWindowFocusChanged這個方法的含義是:View已經初始化完畢了,寬/高已經準備好了,這個時候去獲取寬/高是沒問題的。需要注意的是,onWindowFocusChanged會被調用多次,當Activity的窗口得到焦點和失去焦點時均會被調用一次。具體來說,當Activity繼續執行和暫停執行時,onWindowFocusChanged均會被調用,如果頻繁地進行onResume和onPause,那麼onWindowFocusChanged也會被頻繁地調用。

public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
                int width = view.getMeasuredWidth();
                int height = view.getMeasuredHeight();
        }
}

3.ViewTreeObserver
使用ViewTreeObserver的衆多回調可以完成這個功能,比如使用OnGlobalLayoutListener這個接口,當View樹的狀態發生改變或者View樹內部的View的可見性發現改變時,onGlobalLayout方法將被回調,因此這是獲取View的寬/高一個很好的時機。需要注意的是,伴隨着View樹的狀態改變等,onGlobalLayout會被調用多次。
一定要注意,OnGlobalLayoutListener用完要及時移除

view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
 protected void onStart() {
        super.onStart();
        ViewTreeObserver observer = view.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {
                        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        int width = view.getMeasuredWidth();
                        int height = view.getMeasuredHeight();
                }
        });
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章