getTop 和 getY 有什么区别

what is the difference between getTop and getY

我似乎不太了解 android 视图中 getTop() 和 getY() 之间的区别。它们有何不同?

getTop() returns 相对于父级的 y 坐标。

getY() returns 相对于父级的 y 坐标,如 getTop(),加上由 getTranslationY() 返回的 y 平移。

对于类似这样的问题,查阅来源通常很有帮助:

public final int getTop() {
    return mTop;
}

http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/view/View.java#10644

public float getY() {
    return mTop + getTranslationY();
}

http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/view/View.java#10908

首先,阅读 View class:

的文档

据我所知:

getX() : The visual x position of this view, in pixels.

getY() : The visual y position of this view, in pixels.

getWidth() : Return the width of the your view.

getHeight() : Return the height of the your view.

getTop() : Top position of this view relative to its parent.

getLeft() : Left position of this view relative to its parent.

文档:

getTop () : Top position of this view relative to its parent. Returns The top of this view, in pixels.

并且:

getY () :The visual y position of this view, in pixels. This is equivalent to the translationY property plus the current top property. Returns The visual y position of this view, in pixels.

getY()方法return根据父级的Y坐标。

另一方面,getTop() return根据其父视图获取 Y 坐标。 如果 parent 有 300 作为 Y 点,并且它里面的另一个视图比它低一点,那么 returns 100 不像 getY 方法 returns 300+100.