View關(guān)于坐標(biāo)方法系列
相對(duì)坐標(biāo)系列
MotionEvent 系列方法
-
e.getX()/e.getY()
/** * Returns the X coordinate of this event for the given pointer * <em>index</em> (use {@link #getPointerId(int)} to find the pointer * identifier for this index). * Whole numbers are pixels; the * value may have a fraction for input devices that are sub-pixel precise. * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 * (the first pointer that is down) to {@link #getPointerCount()}-1. * * @see #AXIS_X */
觸摸點(diǎn)相對(duì)于View自身的X,Y坐標(biāo)偏移量。
View 系列方法
-
view.getTop()/getLeft()/getRight()/getBottom()
/** * Left position of this view relative to its parent. * * @return The left edge of this view, in pixels. */
得到的坐標(biāo)是view相對(duì)于其父容器的坐標(biāo),也即是view在父容器中的繪制區(qū)域。實(shí)際上right-left 與bottom-top正好是view的width與height
以父容器的左上角為坐標(biāo)原點(diǎn)
具體測(cè)試代碼結(jié)果下文有貼入勒虾,這里簡(jiǎn)單描述一下結(jié)果脖卖。
revenueLayout的top為100罐脊,恰好是其相對(duì)父容器的一個(gè)margin值炕婶;tvProfit的top為15兜蠕,恰好是其父容器revenueLayout的padding大小蝎毡。
-
view.getTranslationX()/getTranslationY()
/** * The vertical location of this view relative to its {@link #getTop() top} position. * This position is post-layout, in addition to wherever the object's * layout placed it. * * @return The vertical position of this view relative to its top position, * in pixels. */
得到的值是其相對(duì)于初始的left or top 的平移距離厚柳,,這個(gè)與屬性動(dòng)畫(huà)中的translation設(shè)置有關(guān)系
絕對(duì)坐標(biāo)系列
以屏幕的左上角為坐標(biāo)原點(diǎn)
View系列方法
-
view.getLocationInWindow(@Size(2) int[] outLocation)
/** * <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 */
獲取當(dāng)前view在Window中的坐標(biāo)沐兵,也即是window的絕對(duì)坐標(biāo)
-
view.getLocationOnScreen(@Size(2) int[] outLocation)
/** * <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 */
與getLocationInWindow()的返回相同别垮,下面的測(cè)試結(jié)果中也驗(yàn)證了是相同。具體是不是存在特殊的差異扎谎,還未發(fā)現(xiàn)
-
view.getGlobalVisibleRect(Rect r)/getGlobalVisibleRect(Rect r, Point globalOffset)
/** * If some part of this view is not clipped by any of its parents, then * return that area in r in global (root) coordinates. To convert r to local * coordinates (without taking possible View rotations into account), offset * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)). * If the view is completely clipped or translated out, return false. * * @param r If true is returned, r holds the global coordinates of the * visible portion of this view. * @param globalOffset If true is returned, globalOffset holds the dx,dy * between this view and its root. globalOffet may be null. * @return true if r is non-empty (i.e. part of the view is visible at the * root level. */
獲取當(dāng)前view在絕對(duì)坐標(biāo)系中的rect對(duì)象碳想,也即是繪制區(qū)域的絕對(duì)坐標(biāo)邊界。
MotionEvent 系列方法
-
e.getRawX()/e.getRawY()
/** * Returns the original raw X coordinate of this event. For touch * events on the screen, this is the original location of the event * on the screen, before it had been adjusted for the containing window * and views. * * @see #getX(int) * @see #AXIS_X */
觸摸點(diǎn)相對(duì)于屏幕原點(diǎn)的x,y坐標(biāo)毁靶。
測(cè)試案例
機(jī)型信息如下:
布局文件View位置
<RelativeLayout
android:layout_marginTop="100px"
android:id="@+id/revenue_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15px">
<TextView
android:id="@+id/tv_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="余額:0 元"
android:textSize="12dp" />
<TextView
android:id="@+id/tv_profit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="據(jù)歷史收益可得:0.00 元"
android:textSize="12dp" />
</RelativeLayout>
預(yù)覽截圖如下:
onResume()中執(zhí)行結(jié)果:
onResume中并沒(méi)有獲取到View坐標(biāo)信息
onWindowFocusChanged()中執(zhí)行結(jié)果:
activity 應(yīng)用全屏主題(NoTitle)
activity 為應(yīng)用全屏主題(有actionbar)
從上述兩個(gè)運(yùn)行結(jié)果來(lái)看胧奔,getLocationInWindow()與getLocationOnScreen()并未存在差異。
其中tvBalance的position [15,169]老充,Y坐標(biāo)169是100(revenueLayout的margin)+15(revenueLayout的padding)+54(狀態(tài)欄status bar的高度葡盗,此值是反推出來(lái)的)。由此就可以看出獲取的確實(shí)是View的絕對(duì)坐標(biāo)啡浊。
而對(duì)于應(yīng)用非全屏(NoTitle)主題的結(jié)果觅够,position [15,337],恰好就是多出了一個(gè)actionbar的高度,即337-169 = 168巷嚣。而168恰好滿(mǎn)足56*3(density),這里就看到了喘先,在系統(tǒng)默認(rèn)的配置下當(dāng)前的屏幕寬度下,actionbar的高度為56dp,當(dāng)前的density恰好是3.0廷粒,而換算成px就是168窘拯。到了這里红且,整個(gè)測(cè)試結(jié)果就完全驗(yàn)證了,上述方法的解釋涤姊。
備注:
- View的坐標(biāo)體系在OnCreate()中還未進(jìn)行初始化暇番,所以onCreate中無(wú)法獲取View的相關(guān)坐標(biāo),都是return 0思喊;
- View的坐標(biāo)在onWindowFocusChanged(true)時(shí)壁酬,才會(huì)初始化完畢,故在此方法的回調(diào)是獲取View坐標(biāo)的最佳之處恨课。
- 由于onResume與onWindowFocusChanged的調(diào)用時(shí)機(jī)的不同舆乔,在第一次執(zhí)行onResume時(shí)也無(wú)法直接獲取到View的坐標(biāo)。