android中getLocationInWindow 和 getLocationOnScreen的区别
原文鏈接:http://blog.csdn.net/ouyang_peng/article/details/46902957
//一個控件在其父窗口中的坐標位置 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];//獲取當前位置的縱坐標getLocationInWindow
/*** <p>Computes the coordinates of this view in its window. The argument* must be an array of two integers. After the method returns, the array* contains the x and y location in that order.</p>** @param outLocation an array of two integers in which to hold the coordinates*/public void getLocationInWindow(@Size(2) int[] outLocation) {if (outLocation == null || outLocation.length < 2) {throw new IllegalArgumentException("outLocation must be an array of two integers");}outLocation[0] = 0;outLocation[1] = 0;transformFromViewToWindowSpace(outLocation);}/** @hide */public void transformFromViewToWindowSpace(@Size(2) int[] inOutLocation) {if (inOutLocation == null || inOutLocation.length < 2) {throw new IllegalArgumentException("inOutLocation must be an array of two integers");}if (mAttachInfo == null) {// When the view is not attached to a window, this method does not make senseinOutLocation[0] = inOutLocation[1] = 0;return;}float position[] = mAttachInfo.mTmpTransformLocation;position[0] = inOutLocation[0];position[1] = inOutLocation[1];if (!hasIdentityMatrix()) {getMatrix().mapPoints(position);}position[0] += mLeft;position[1] += mTop;ViewParent viewParent = mParent;while (viewParent instanceof View) {final View view = (View) viewParent;position[0] -= view.mScrollX;position[1] -= view.mScrollY;if (!view.hasIdentityMatrix()) {view.getMatrix().mapPoints(position);}position[0] += view.mLeft;position[1] += view.mTop;viewParent = view.mParent;}if (viewParent instanceof ViewRootImpl) {// *cough*final ViewRootImpl vr = (ViewRootImpl) viewParent;position[1] -= vr.mCurScrollY;}inOutLocation[0] = Math.round(position[0]);inOutLocation[1] = Math.round(position[1]);}getLocationOnScreen
/*** <p>Computes the coordinates of this view on the screen. The argument* must be an array of two integers. After the method returns, the array* contains the x and y location in that order.</p>** @param outLocation an array of two integers in which to hold the coordinates*/public void getLocationOnScreen(@Size(2) int[] outLocation) {getLocationInWindow(outLocation);final AttachInfo info = mAttachInfo;if (info != null) {outLocation[0] += info.mWindowLeft;outLocation[1] += info.mWindowTop;}}Android中獲取坐標點的一些方法解釋
一、getLocationInWindow和getLocationOnScreen的區別
// location [0]—>x坐標,location [1]—>y坐標
int[] location = new int[2] ;
// 獲取在當前窗口內的絕對坐標,getLeft , getTop, getBottom, getRight, 這一組是獲取相對在它父窗口里的坐標。
view.getLocationInWindow(location);
// 獲取在整個屏幕內的絕對坐標,注意這個值是要從屏幕頂端算起,也就是包括了通知欄的高度。
view.getLocationOnScreen(location);
如果在Activity的OnCreate()事件輸出那些參數,是全為0,要等UI控件都加載完了才能獲取到這些。
在onWindowFocusChanged(boolean hasFocus)中獲取為好。
View.getLocationInWindow()和 View.getLocationOnScreen()在window占據全部screen時,返回值相同,不同的典型情況是在Dialog中時。當Dialog出現在屏幕中間時,View.getLocationOnScreen()取得的值要比View.getLocationInWindow()取得的值要大。
二、Android View坐標getLeft, getRight, getTop, getBottom
理解坐標,位置概念
這里涉及坐標系的概念:
坐標系在二維視圖中通過X軸和Y軸兩個數字為組合表示某個點的絕對坐標。 例如(30, 100) 通常表示X軸30, Y軸100交叉的一個點
在Android中可以把left相當于X軸值, top相當于Y軸值, 通過這兩個值Android系統可以知道視圖的繪制起點,在通過Wdith 和 Height 可以得到視圖上下左右具體值,就可以在屏幕上絕對位置繪制視圖。right 與 bottom計算如下:right = left + width;bottom = top + height;相應API
視圖左側位置 view.getLeft()
視圖右側位置 view.getRight()
視圖頂部位置 view.getTop();
視圖底部位置 view.getBottom();
視圖寬度 view.getWidth();
視圖高度 view.getHeight()
實例分析
按照我的理解:
藍色區域位置 left = 0, top = 0 坐標(0, 0 )
黃色區域位置 left = 60, top = 115 坐標(60, 115)
綠色區域位置 left = 115, top = 170 坐標(115, 170)
委屈 綠色區域,這里理解錯誤,我認為綠色區域的位置是針對于藍色區域的(0, 0)坐標的值,從上圖的右下角打印出的坐標值就可以看出與下方我列出的值不一致,看看下面的圖就明白了
總結: 視圖的left , top , right , bottom 的值是針對其父視圖的相對位置, 綠色區域是針對其父視圖(即黃色區域為(0, 0)點)的坐標,不應該是(115, 170 ) 而是 (55, 55)
public class MainActivity extends AppCompatActivity {private RelativeLayout rl1;private RelativeLayout rl2;private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);rl1 = (RelativeLayout) findViewById(R.id.rl1);rl2 = (RelativeLayout) findViewById(R.id.rl2);tv = (TextView) findViewById(R.id.tv);int[] rl1_location = new int[2];int[] rl2_location = new int[2];int[] tv_location = new int[2];rl1.getLocationInWindow(rl1_location);rl2.getLocationInWindow(rl2_location);tv.getLocationInWindow(tv_location);System.out.println(rl1_location[0] + ":" + rl1_location[1]);System.out.println(rl2_location[0] + ":" + rl2_location[1]);System.out.println(tv_location[0] + ":" + tv_location[1]);System.out.println("rl2 left:" + rl2.getLeft() + " top: " + rl2.getTop());rl1.getLocationOnScreen(rl1_location);rl2.getLocationOnScreen(rl2_location);tv.getLocationOnScreen(tv_location);System.out.println(rl1_location[0] + ":" + rl1_location[1]);System.out.println(rl2_location[0] + ":" + rl2_location[1]);System.out.println(tv_location[0] + ":" + tv_location[1]);} } 12-09 09:25:20.345 8345-8345/com.google.testdemo I/System.out: 0:243 12-09 09:25:20.345 8345-8345/com.google.testdemo I/System.out: 126:631 12-09 09:25:20.345 8345-8345/com.google.testdemo I/System.out: 426:931 12-09 09:25:20.345 8345-8345/com.google.testdemo I/System.out: rl2 left:126 top: 388 12-09 09:25:20.345 8345-8345/com.google.testdemo I/System.out: 0:243 12-09 09:25:20.345 8345-8345/com.google.testdemo I/System.out: 126:631 12-09 09:25:20.345 8345-8345/com.google.testdemo I/System.out: 426:931總結
以上是生活随笔為你收集整理的android中getLocationInWindow 和 getLocationOnScreen的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: setDrawingCacheEnabl
- 下一篇: 属性动画的应用