欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android如何獲取子View的位置及坐標(biāo)詳解

 更新時(shí)間:2020年10月29日 08:43:35   作者:萌果愛(ài)吃檸檬  
這篇文章主要給大家介紹了關(guān)于Android如何獲取子View的位置及坐標(biāo)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、View

1.1、View 概述

視圖 (View) 是一個(gè)容器,專(zhuān)門(mén)負(fù)責(zé)布局。表現(xiàn)為顯示在屏幕上的各種視圖,如 TextView、LinearLayout 等。

1.2、View 分類(lèi)

View 主要分為兩類(lèi),具體如下表格所示:

類(lèi)別 示例 特點(diǎn)
單一視圖 即一個(gè) View,如 TextView、EditText 不包含子View
視圖組 即多個(gè) View 組成的 ViewGroup,如 RelativeLayout 包含子View

1.3、View 類(lèi)簡(jiǎn)介

View 類(lèi)是 Android 中各種組件的基類(lèi);

View 的構(gòu)造函數(shù)有四個(gè),具體如下所示:

public View(Context context) {

}

public View(Context context, @Nullable AttributeSet attrs) {
 this(context, attrs, 0);
}

public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 this(context, attrs, defStyleAttr, 0);
}
 
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
 
}

源碼中 View 的構(gòu)造函數(shù)

通過(guò)源碼的注釋我們可以看出:

  • 如果 View 是在 Java 代碼里面 new 的,則調(diào)用第一個(gè)構(gòu)造函數(shù)-->View(Context);
  • 如果 View 是在 xml 里聲明的,則調(diào)用第二個(gè)構(gòu)造函數(shù)-->View(Context, AttributeSet)。

二、Android 坐標(biāo)系

Android 坐標(biāo)系和數(shù)學(xué)上的坐標(biāo)系是不一樣的,定義如下:

  • 屏幕的左上角為坐標(biāo)原點(diǎn)。
  • 向右為 x 軸增大方向。
  • 向下為 y 軸增大方向。

具體如下圖所示:

三、View 的位置

View 的位置是相對(duì)于父控件而言的,由 4 個(gè)頂點(diǎn)確定,如下圖 A、B、C、D 所示:

 

確定 View 的位置有四個(gè)參數(shù),分別是 Top、Bottom、Left、Right:

  • Top:子 View 左上角距父 View 頂部的距離。
  • Left:子 View 左上角距父 View 左側(cè)的距離。
  • Bottom:子 View 右下角距父 View 頂部的距離。
  • Right:子 View 右下角距父 View 左側(cè)的距離

具體如下圖所示:

四、獲取 View 位置的方式

View 的位置是通過(guò) getTop()、getLeft()、getBottom()、getRight() 函數(shù)進(jìn)行獲取的。

這里我寫(xiě)了一個(gè)小例子來(lái)演示這四個(gè)方法,如下所示:(獲取內(nèi)部子 View 的位置)

 

因?yàn)槭菫榱搜菔?View 的位置,所有我這里用絕對(duì)布局,并且大小的單位都是用 px,具體布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">

 <RelativeLayout
  android:id="@+id/rl_1"
  android:layout_width="600px"
  android:layout_height="600px"
  android:layout_x="200px"
  android:layout_y="200px"
  android:background="@color/colorPrimaryDark">

  <View
   android:id="@+id/view"
   android:layout_width="300px"
   android:layout_height="300px"
   android:layout_centerInParent="true"
   android:background="@color/colorAccent" />

 </RelativeLayout>

</AbsoluteLayout>

我們現(xiàn)在用四個(gè)方法來(lái)獲取一下 View 的位置,具體代碼如下所示:

public class CoordinateActivity extends AppCompatActivity {

 private View mView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_coordinate);

  rl1 = findViewById(R.id.rl_1);
  mView = findViewById(R.id.view);

 }

 @Override
 protected void onResume() {
  super.onResume();

  new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
    MyLogUtils.i(mView.getTop() + "--Top --mView");
    MyLogUtils.i(mView.getBottom() + "--Bottom --mView");
    MyLogUtils.i(mView.getLeft() + "--Left --mView");
    MyLogUtils.i(mView.getRight() + "--Right --mView");
    MyLogUtils.i(mView.getX() + "--X --mView");
    MyLogUtils.i(mView.getY() + "--Y --mView");
   }
  }, 200);
 }
}

打印結(jié)果如下所示:

最外層紫色的 View 的坐標(biāo)是(200,200),大小是 600px,在它內(nèi)部,有一個(gè)大小為 300px 的子 View 位于其中心位置,所以上述打印結(jié)果是完全正確的。

注意:

  • 我這里調(diào)用 getTop() 等方法是在 onResume() 里面,并且延時(shí)了 200ms,是因?yàn)槿绻谎舆t直接調(diào)用,會(huì)出現(xiàn) View 還沒(méi)有繪制完,所以獲取到的位置都是 0,所以就用最簡(jiǎn)單的延遲處理了一下(這里的處理方法有很多,比如 View.post() 等);
  • getX() 和 getY() 的意思是獲取子 View 相對(duì)父容器的坐標(biāo),所以這里結(jié)果都是 150。

總結(jié)

到此這篇關(guān)于Android如何獲取子View的位置及坐標(biāo)的文章就介紹到這了,更多相關(guān)Android獲取子View位置及坐標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論