【Android 应用开发】Android 组件 位置坐标 属性 ( 组件位置属性 | 父容器坐标系坐标 | 窗口坐标系坐标 | 屏幕坐标系坐标 | 触摸坐标 )
文章目錄
- I . View 坐標(biāo)體系總結(jié)
- II . View 組件的 left , top , right , bottom 父容器相對(duì)位置
- III . View 組件的 x , y , translationX , translationY 屬性
- IV . View 組件 窗口坐標(biāo) 屬性
- V . View 組件 屏幕坐標(biāo) 屬性
- VI . View 組件的觸摸位置 屬性
- VII . View 坐標(biāo)獲取 0 的解決方案
I . View 坐標(biāo)體系總結(jié)
left , top , right , bottom 是組件相對(duì)于父容器的位置 , 該值一般不會(huì)改變 ;
x , y 是組件當(dāng)前相對(duì)于父容器的位置 ;
translationX , translationY 分別是相對(duì)于 left , top 值的偏移大小 ;
left + translationX = x , top + translationY = y ;
窗口坐標(biāo)獲取 : getLocationInWindow ;
屏幕坐標(biāo)獲取 : getLocationOnScreen ;
觸摸坐標(biāo) : x , y 觸摸坐標(biāo)是相對(duì)于組件的坐標(biāo) , rawX , rawY 是相對(duì)于屏幕的坐標(biāo) ;
II . View 組件的 left , top , right , bottom 父容器相對(duì)位置
1 . View 組件的基礎(chǔ)位置 : left , top , right , bottom 是 View 組件的最基本的位置屬性 , 其作用是 描述該組件相對(duì)于父容器的位置 ;
2 . 相對(duì)父容器位置 : 這里特別注意 , left , top , right , bottom 屬性 , 是 相對(duì)于父容器的位置 , 不是 相對(duì)于 Activity 界面位置 , 也不是 相對(duì)于屏幕的位置 ;
3 . 屬性本質(zhì) : left , top , right , bottom , 四個(gè)屬性本質(zhì)是坐標(biāo)值 ;
4 . 父容器 坐標(biāo)系 及 坐標(biāo)原點(diǎn) : 四屬性的坐標(biāo)值原點(diǎn) (0,0)(0, 0)(0,0) 是父容器的左上角位置 , xxx 軸向右為正 , 向左為負(fù) , yyy 軸向下為正 , 向上為負(fù) ;
5 . 組件基礎(chǔ)位置屬性 : 組件的基礎(chǔ)位置屬性是不會(huì)改變的 ;
① left 屬性 : 組件的左側(cè) xxx 軸坐標(biāo) , 值為左側(cè)距離父容器左側(cè)的長(zhǎng)度 , 本組件左上角頂點(diǎn) , 在父容器坐標(biāo)系的 xxx 值 ;
② top 屬性 : 組件的頂部 yyy 軸坐標(biāo) , 值為頂部距離父容器頂部的高度 , 本組件左上角頂點(diǎn) , 在父容器坐標(biāo)系的 yyy 值 ;
③ right 屬性 : 組件的右側(cè) xxx 軸坐標(biāo) , 值為右側(cè)距離父容器左側(cè)的長(zhǎng)度 , 本組件右下角頂點(diǎn) , 在父容器坐標(biāo)系的 xxx 值 ;
④ bottom 屬性 : 組件的底部 yyy 軸坐標(biāo) , 值為底部距離父容器頂部的長(zhǎng)度 , 本組件右下角頂點(diǎn) , 在父容器坐標(biāo)系的 yyy 值 ;
III . View 組件的 x , y , translationX , translationY 屬性
1 . View 組件的當(dāng)前位置 : x , y , translationX , translationY 是 View 組件的當(dāng)前位置 , 其作用是 描述當(dāng)前的實(shí)際位置 , 該位置也是相對(duì)于父容器的坐標(biāo) , 這里要與基礎(chǔ)位置區(qū)分開(kāi) ;
2 . 組件當(dāng)前位置 屬性 : x , y 是當(dāng)前的位置 , 這個(gè)位置會(huì)隨著 translationX 和 translationY 的值改變而改變 ;
① x : 表示組件左上角當(dāng)前的實(shí)際位置的 x 坐標(biāo) , 該坐標(biāo)是父容器坐標(biāo)系中的坐標(biāo) , 原點(diǎn)是父容器左上角位置 ;
② y : 表示組件左上角當(dāng)前的實(shí)際位置的 y 坐標(biāo) , 該坐標(biāo)是父容器坐標(biāo)系中的坐標(biāo) , 原點(diǎn)是父容器左上角位置 ;
3 . 偏移位置 : translationX , translationY , 是基于基礎(chǔ)位置 left , top 的偏移位置 ;
4 . 當(dāng)前位置 和 偏移位置 的 初始值 : x 的初始值是 left , y 的初始值是 top , translationX 初始值是 0 , translationY 初始值是 0;
5 . View 組件 坐標(biāo)屬性之間的關(guān)系 :
① x , left , translationX 關(guān)系 :
x=left+translationXx = left + translationX x=left+translationX
② y , top , translationY 關(guān)系 :
y=top+translationYy = top + translationY y=top+translationY
6 . 代碼分析 :
① View 組件獲取 x 屬性代碼分析 : x 的值 , 由 left 加上 translationX 得到 ;
/*** The visual x position of this view, in pixels. This is equivalent to the* {@link #setTranslationX(float) translationX} property plus the current* {@link #getLeft() left} property.** @return The visual x position of this view, in pixels.*/ @ViewDebug.ExportedProperty(category = "drawing") public float getX() {return mLeft + getTranslationX(); }② View 組件獲取 y 屬性代碼分析 : y 的值 , 由 top 加上 translationY 得到 ;
/*** The visual y position of this view, in pixels. This is equivalent to the* {@link #setTranslationY(float) translationY} property plus the current* {@link #getTop() top} property.** @return The visual y position of this view, in pixels.*/ @ViewDebug.ExportedProperty(category = "drawing") public float getY() {return mTop + getTranslationY(); }IV . View 組件 窗口坐標(biāo) 屬性
1 . 獲取 View 組件窗口坐標(biāo) : 這里的窗口指的是 Activity 窗口 ;
① 所在坐標(biāo)系 : 坐標(biāo)系的原點(diǎn) (0,0)(0 ,0)(0,0) 是 Activity 窗口的左上角位置 ;
② 區(qū)分屏幕坐標(biāo) : Activity 窗口上還有一個(gè)狀態(tài)欄 , 這里要與屏幕坐標(biāo)系區(qū)分開(kāi) ;
③ 獲取方法 : 調(diào)用 View 組件對(duì)象的 getLocationInWindow ( ) 方法 , 數(shù)組 0 索引是 x 坐標(biāo) , 數(shù)組 1 索引是 y 坐標(biāo) ;
int[] array = new int[2]; surfaceView.getLocationInWindow(array);V . View 組件 屏幕坐標(biāo) 屬性
1 . 獲取 View 組件屏幕坐標(biāo) : 這里的屏幕指的是手機(jī)屏幕 ;
① 所在坐標(biāo)系 : 坐標(biāo)系的原點(diǎn) (0,0)(0 ,0)(0,0) 是 手機(jī)屏幕的左上角位置 ;
② 獲取方法 : 調(diào)用 View 組件對(duì)象的 getLocationOnScreen ( ) 方法 , 數(shù)組 0 索引是 x 坐標(biāo) , 數(shù)組 1 索引是 y 坐標(biāo) ;
int[] array = new int[2]; surfaceView.getLocationOnScreen(array);VI . View 組件的觸摸位置 屬性
View 組件的觸摸事件的位置屬性 :
① x , y 相對(duì)于組件坐標(biāo) : 當(dāng)觸摸組件時(shí) , 從 MotionEvent 事件中獲取的 x , y 坐標(biāo)是 相對(duì)于本組件的坐標(biāo) , 即坐標(biāo)系的原點(diǎn) (0,0)( 0, 0 )(0,0) 是該組件的左上角位置 ;
② rawX , rawY 相對(duì)于屏幕坐標(biāo) : 從 MotionEvent 事件中獲取的 rawX , rawY 坐標(biāo) , 是當(dāng)前觸摸點(diǎn)相對(duì)于屏幕的坐標(biāo) , 即坐標(biāo)系的原點(diǎn) (0,0)(0, 0)(0,0) 是屏幕的左上角位置 ;
VII . View 坐標(biāo)獲取 0 的解決方案
1 . 問(wèn)題描述 : 在獲取組件 位置 , 寬高 時(shí) , 經(jīng)常遇到獲取組件的值為 000 的情況 , 這是因?yàn)樵摻M件還沒(méi)有繪制完成 ;
2 . 解決方案 : 獲取 ViewTreeObserver , 監(jiān)聽(tīng)布局樹(shù)的繪制情況 , 組件繪制時(shí) , 需要測(cè)量 ( Measure ) , 擺放 ( Layout ) , 繪制 ( Draw ) , 此時(shí)監(jiān)聽(tīng)的是擺放操作 , 擺放完畢后 , 就可以獲取組件的坐標(biāo)和大小屬性 ;
3 . 監(jiān)聽(tīng) Layout 代碼 :
surfaceView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {//此時(shí)可以獲取 View 的坐標(biāo)和大小 , 不為 0} });總結(jié)
以上是生活随笔為你收集整理的【Android 应用开发】Android 组件 位置坐标 属性 ( 组件位置属性 | 父容器坐标系坐标 | 窗口坐标系坐标 | 屏幕坐标系坐标 | 触摸坐标 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【数据挖掘】卷积神经网络 ( 池化 |
- 下一篇: 【数据挖掘】K-NN 分类 ( 简介 |