What is the difference between getTop and getY

I do not understand the difference between getTop () and getY () in the Android view. How do they differ?

+6
source share
4 answers

getTop() returns the y coordinate relative to the parent.

getY() returns the y coordinate relative to the parent type, for example getTop() , plus the translation y returned by getTranslationY() .

For such questions it is often useful to refer to the source:

 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

+4
source

The getY () method returns the Y coordinate according to the parent.

GetTop (), on the other hand, returns the Y coordinate according to its parent representation. If the parent element has a value of 300 as a point Y, and the other view inside it is slightly smaller than it, then 100 is returned, unlike the getY method 300 + 100.

+2
source

First of all, read the documentation for the View class:

As far as I know:

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

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

getWidth (): Returns the width of your view.

getHeight (): return the width of your view.

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

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

+1
source

Document:

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

AND:

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

0
source

Source: https://habr.com/ru/post/987721/


All Articles