關(guān)于Android left, top, x, y, translationX, translationY 那點(diǎn)事

既然選擇了遠(yuǎn)方,便只顧風(fēng)雨兼程.
該圖片來自網(wǎng)絡(luò)資源,若有侵權(quán),請留言,我將自行刪除
之前對View的left , top ,x ,y ,translationX, translationY,有所研究,但是時(shí)間一長,腦袋中的東西大都都還給了時(shí)間.好記性不如爛筆頭,今天重翻 任玉剛大神的 《Android 開發(fā)藝術(shù)探究》,一切從頭開始, come on.
image.png
咱們按照上面的圖繪實(shí)例來說一說 left,top,x,y,translationX,以及translationY.
  • left,top,right,bottom

view 的位置坐標(biāo)和父容器的關(guān)系.png

view 的位置主要由它的四個頂點(diǎn)來控制的,分別對應(yīng)View的四個屬性:left,top,right,buttom.其中:

left 是左上橫坐標(biāo)
top 是左上縱坐標(biāo)
right 是右下橫坐標(biāo)
bottom是右下縱坐標(biāo)

需要注意的是,這些坐標(biāo)都是相對于View的父容器來說的,因此它是相對坐標(biāo).在Android中,x軸 和 y軸的正方向分別為右 和 下.

  • 根據(jù)上圖所描述的我們可以得出,view的寬高和坐標(biāo)之間的關(guān)系
width = right - left
height = bottom - top 

如何得到left, top,right,bottom 的值呢? 在View的源碼中它們有自己對應(yīng)的是mLeft,mTop,mRight,mBottom這四個成員變量,并且 它們都擁有自己的get/set方法.

  @ViewDebug.ExportedProperty(category = "layout")
    protected int mLeft;
    /**
     * The distance in pixels from the left edge of this view's parent
     * to the right edge of this view.
     * {@hide}
     */
    @ViewDebug.ExportedProperty(category = "layout")
    protected int mRight;
    /**
     * The distance in pixels from the top edge of this view's parent
     * to the top edge of this view.
     * {@hide}
     */
    @ViewDebug.ExportedProperty(category = "layout")
    protected int mTop;
    /**
     * The distance in pixels from the top edge of this view's parent
     * to the bottom edge of this view.
     * {@hide}
     */
    @ViewDebug.ExportedProperty(category = "layout")
    protected int mBottom;
  • x,y,translationX,translationY

從3.0開始,view增加了額外的幾個參數(shù):x,y,translationX 和 translationY.其中x,y是view左上角的位置,而translationX和translationY是View左上角相對于父容器的偏移量,這幾個參數(shù)也是相對父容器的坐標(biāo),并且translationX和translationY默認(rèn)是0.其實(shí)這樣說有點(diǎn)籠統(tǒng).咱們還是看圖.


平移過程中view帚桩,及x仪或,y 發(fā)生的變化.png

當(dāng)view<紅框> 靜止不動的時(shí)候 (x,y) == (left,top),此時(shí)translationX/Y.默認(rèn)為0.當(dāng)view向下做平移運(yùn)動的時(shí)候,此刻view發(fā)生了偏移,并產(chǎn)生了偏移量,這個偏移相對于X軸就是translationX,相對于Y軸就是translationY.如上圖所示.View 在平移的過程中,top 和 left 表示的是原始左上角的信息,其值并不會發(fā)生變化,此時(shí)發(fā)生改變的是x,y,translationX/Y.它們之間的換算關(guān)系如下:

x = left + translationX
y = top + translationY
  • 總結(jié):

1. left ,top ,right,bottom的值都是 View相對于父容器.具體為父容器的左上角,以及右下角.
2. translationX/Y 是View左上角相對與父容器的偏移量,translationX/Y默認(rèn)為0,和View的四個基本位置參數(shù)一樣,View也為它們提供了get/set方法.
3.View在發(fā)生平移的時(shí)候,top和left表示的原始左上角的位置信息,其值不會發(fā)生變化.
  • 代碼實(shí)現(xiàn)說明

xml代碼

 <TextView
        android:padding="@dimen/dp_10"
        android:background="@color/colorPrimary"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="hello"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bt_query" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bt_query"
        app:srcCompat="@mipmap/ic_launcher" />

kotlin 代碼

override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       // textView 
       textView.top = 10
       textView.left = 20

       getViewLocation(textView)

       textView.translationX = 30.0f   // x 軸正方向
       textView.translationY = 50.0f  // y 軸正方向

       getViewLocation(textView)

     // imageView 動畫處理
       imageView.setOnClickListener {
           getViewLocation(imageView)   
           ObjectAnimator.ofFloat(imageView,"translationX",-30f)  // 左平移
          .setDuration(1000)
          .start()
       }
   }
     
private fun getViewLocation(view: View?) {
       val top = view?.top
       val left = view?.left
       val x = view?.x
       val y = view?.y

       var translationX = view?.translationX
       var translationY = view?.translationY


       println("查看當(dāng)前 top left x y translationX,translationY 的數(shù)據(jù):$top , $left , $x , $y ,$translationX , $translationY")

   }

日志打印

System.out: 查看當(dāng)前 top left x y translationX,translationY 的數(shù)據(jù):10 , 20 , 20.0 , 10.0 ,0.0 , 0.0
System.out: 查看當(dāng)前 top left x y translationX,translationY 的數(shù)據(jù):10 , 20 , 50.0 , 60.0 ,30.0 , 50.0

// ImageView
I/System.out: 查看當(dāng)前 top left x y translationX,translationY 的數(shù)據(jù):360 , 432 , 432.0 , 360.0 ,0.0 , 0.0
I/System.out: 查看當(dāng)前 top left x y translationX,translationY 的數(shù)據(jù):360 , 432 , 402.0 , 360.0 ,-30.0 , 0.0
 

如果您發(fā)現(xiàn)這篇文章,有錯誤之處,希望請您留言,并指出.我定虛心接受.并感激不盡!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末西疤,一起剝皮案震驚了整個濱河市望蜡,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌帆吻,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異季二,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)揭措,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門胯舷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人绊含,你說我怎么就攤上這事桑嘶。” “怎么了躬充?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵逃顶,是天一觀的道長。 經(jīng)常有香客問我充甚,道長以政,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任伴找,我火速辦了婚禮盈蛮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘技矮。我一直安慰自己抖誉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布衰倦。 她就那樣靜靜地躺著袒炉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪耿币。 梳的紋絲不亂的頭發(fā)上梳杏,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天,我揣著相機(jī)與錄音淹接,去河邊找鬼十性。 笑死,一個胖子當(dāng)著我的面吹牛塑悼,可吹牛的內(nèi)容都是我干的劲适。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼厢蒜,長吁一口氣:“原來是場噩夢啊……” “哼霞势!你這毒婦竟也來了烹植?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤愕贡,失蹤者是張志新(化名)和其女友劉穎草雕,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體固以,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡墩虹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了憨琳。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片诫钓。...
    茶點(diǎn)故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖篙螟,靈堂內(nèi)的尸體忽然破棺而出菌湃,到底是詐尸還是另有隱情,我是刑警寧澤遍略,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布惧所,位于F島的核電站,受9級特大地震影響墅冷,放射性物質(zhì)發(fā)生泄漏纯路。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一寞忿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧顶岸,春花似錦腔彰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至卷谈,卻和暖如春杯拐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背世蔗。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工端逼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人污淋。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓顶滩,卻偏偏與公主長得像,于是被迫代替她去往敵國和親寸爆。 傳聞我的和親對象是個殘疾皇子礁鲁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容