Illustration: getRawX, getRawY, getX, getY in MotionEvent, getScrollX, getScrollY, and motioneventgetrawx in View
1. Differences between getRawX, getRawY, getX, and getY
When writing android custom controls or determining user gesture operations, you often need to use getRawX (), getRawY (), getX (), and getY () in MotionEvent () obtain the distance between the touch point on the X and Y axes. All four methods return a float parameter in pixels ). GetRawX () and getRawY () return the position of the touch point relative to the screen, while getX () and getY () return the position of the touch point relative to the View.
The following two figures intuitively show the differences between the methods. A Button is placed in the center of the screen and OnTouchListener is registered for it. The green dot in the figure is the position of the touch point.
2. getScrollX and getScrollY in the View
The values of getScrollX () and getScrollY () are generated by calling View's scrollTo (int x, int y) or scrollBy (int x, int y, scrollTo moves the content in the View to the specified coordinates x and y.
The upper left corner of the View, rather than the upper left corner of the screen. ScrollBy (int x, int y) is to change the relative position in the View. The parameters x and y are the relative positions from the previous time.
Text interpretation is always hard to understand, so we can directly and intuitively.
(Figure 1) (figure 2) (figure 3)
1. In Figure 1, a button is placed in the center of the screen, and the content of the button is placed in its upper left corner.
2. Call the scrollTo (-100,-100) method of the button. Result 2 shows that the content in the button is moved to the position in the upper-left corner (-100,-100) of the button.
3. Call the scrollBy (-100,-100) method for the button in Figure 2. As shown in result 3, the content in the button is moved to the position (-100,-100) relative to Figure 2.
The getScrollX () and getScrollY () values are as follows:
06-15 15:44:56.072 20471-20471/com.test.yangy.studiotest V/ScrollActivity﹕ btn scroll X=-20006-15 15:44:56.072 20471-20471/com.test.yangy.studiotest V/ScrollActivity﹕ btn scroll Y=-200
It is worth noting that when the content in the View is moved to the right, the value of getScrollX () is negative. Similarly, a negative number is transferred to the x of scrollTo and scrollBy, And the content in the view is moved to the right, to the left.
When the content in the View moves downward, the value of getScrollY () is negative. Similarly, a negative number is input to the y of scrollTo and scrollBy. The content in the view moves downward, and vice versa.