View之坐標(biāo)體系

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ī)型信息如下:

device_info.png

布局文件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ù)覽截圖如下:

xml_preview.png

onResume()中執(zhí)行結(jié)果:

location_onresume.png

onResume中并沒(méi)有獲取到View坐標(biāo)信息

onWindowFocusChanged()中執(zhí)行結(jié)果:

activity 應(yīng)用全屏主題(NoTitle)

location_notitle.png

activity 為應(yīng)用全屏主題(有actionbar)

location_actionbar.png

從上述兩個(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)。

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末剂公,一起剝皮案震驚了整個(gè)濱河市希俩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌纲辽,老刑警劉巖颜武,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異文兑,居然都是意外死亡盒刚,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)绿贞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)因块,“玉大人,你說(shuō)我怎么就攤上這事籍铁∥猩希” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵拒名,是天一觀的道長(zhǎng)吩愧。 經(jīng)常有香客問(wèn)我,道長(zhǎng)增显,這世上最難降的妖魔是什么雁佳? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮同云,結(jié)果婚禮上糖权,老公的妹妹穿的比我還像新娘。我一直安慰自己炸站,他們只是感情好星澳,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著旱易,像睡著了一般禁偎。 火紅的嫁衣襯著肌膚如雪腿堤。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天如暖,我揣著相機(jī)與錄音笆檀,去河邊找鬼。 笑死盒至,一個(gè)胖子當(dāng)著我的面吹牛误债,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播妄迁,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼李命!你這毒婦竟也來(lái)了登淘?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤封字,失蹤者是張志新(化名)和其女友劉穎黔州,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體阔籽,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡流妻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了笆制。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绅这。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖在辆,靈堂內(nèi)的尸體忽然破棺而出证薇,到底是詐尸還是另有隱情,我是刑警寧澤匆篓,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布浑度,位于F島的核電站,受9級(jí)特大地震影響鸦概,放射性物質(zhì)發(fā)生泄漏箩张。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一窗市、第九天 我趴在偏房一處隱蔽的房頂上張望先慷。 院中可真熱鬧,春花似錦谨设、人聲如沸熟掂。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)赴肚。三九已至素跺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間誉券,已是汗流浹背指厌。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留踊跟,地道東北人踩验。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像商玫,于是被迫代替她去往敵國(guó)和親箕憾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360