getLocationInWindow 和 getLocationOnScreen的区别

  • View.getLocationInWindow(int[] location)
    一个控件在其父窗口中的坐标位置

  • View.getLocationOnScreen(int[] location)
    一个控件在其整个屏幕上的坐标位置


getLocationInWindow是以B为原点的C的坐标
getLocationOnScreen以A为原点。

getLocationOnScreen示例

start = (Button) findViewById(R.id.start);  
int []location=new int[2];  
start.getLocationOnScreen(location);  
int x=location[0];//获取当前位置的横坐标  
int y=location[1];//获取当前位置的纵坐标

getLocationInWindow示例

start = (Button) findViewById(R.id.start);  
int []location=new int[2];         
start.getLocationInWindow(location);  
int x=location[0];//获取当前位置的横坐标  
int y=location[1];//获取当前位置的纵坐标

results matching ""

    No results matching ""