The upper-left corner of the Android phone's screen is the coordinate system, and the y-axis direction is opposite to the y-axis of the Cartesian coordinate system at the same time. By providing an API such asGetLeft , GetTop, Getbottom, GetRight can get the relative position of the control in the parent. At the same time. can also get the absolute position of the control in the screen, the specific use method can bewhere to get view in Android app
When we write some of our own defined sliding controls, we use some APIs such as Scrollto (), Scrollby (), Getscrollx (), getscrolly (). Because there is often confusion about the meaning of the value returned by the function getscrollx (), getscrolly (), especially the positive and negative relationships, this article will use a few graphs to explain these functions to make it easier for everyone to remember.
Note: Calling view's Scrollto () and Scrollby () is used to slide the contents of the view. Instead of changing the location of a particular view. Suppose you want to change the location of a view on the screen, you can use the following method.
Call public void offsetleftandright (int offset) for left and right move methods or public void
scrollTo (int x, int y) is the view in the content slide to the corresponding position . The reference coordinate system origin is the upper-left corner of the parent view.
Calling Scrollto (100, 0) indicates that the contents of the view are moved to the position of x = 0, y =, for example, as seen. Attention. The yellow rectangular area in the figure represents a parent view, and the green dashed rectangle is the contents of the parent view.
Under normal circumstances the size of the two is consistent, this article for display convenience. Draw a little bit of the dashed box. The location of the yellow area in the figure is always the same. What happens is that the content that is displayed is changed.
Similarly, the effects of scrollTo (0, 100) are as seen in:
ScrollTo (100, 100) For example the following:
If the function has a negative number of parameters. The child view will move in the opposite direction.
Scrollby (int x, int y) is in fact a wrapper over the Scrollto, moving in quite a position. The source code for ScrollTo (int x, int y) and Scrollby (int x, int y) are shown below.
/** * Move The scrolled position of your view. This would cause a call to * {@link #onScrollChanged (int, int, int, int)} and the view would be * invalidated. * @param x The amount of pixels to scroll by horizontally<pre name= "code" class= "java" > /** * Set the scroll Ed position of your view. This would cause a call to * {@link #onScrollChanged (int, int, int, int)} and the view would be * invalidated. * @param x position to scroll to * @param y the y position to scroll to * /public void ScrollTo (int x , int y) { if (mscrollx! = x | | mscrolly! = y) { int oldx = MSCROLLX; int oldY = mscrolly; MSCROLLX = x; mscrolly = y; Invalidateparentcaches (); Onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY); if (!awakenscrollbars ()) { postinvalidateonanimation ();}} }
/* @param y the amount of pixels to scroll by vertically */
public void Scrollby (int x, int y) {scrollTo (mscrollx + x, mscrolly + y);}
Visible, MSCROLLX and mscrolly are variables that are specifically used in the view class to record the sliding position. These two functions finally call the onscrollchanged () function, and interested parties can refer to their source code.
Understand how to use Scrollto (int x, int y) and Scrollby (int x, int y). It is not difficult to understand GETSCROLLX () and getscrolly (). The source code for these two functions can be seen as follows:
/** * Return the scrolled left position for this view. The the left edge of * The displayed part of your view. You don't need to draw any pixels * Farther left, since those is outside of the frame of your view on * screen.< c5/>* * @return The left edge of the displayed part of your view, in pixels. * /public final int getscrollx () { return mscrollx; }
/** * Return the scrolled top position of this view. The top edge of * The displayed part of your view. You don't need to draw any pixels above * it, since those is outside of the frame of your view on screen. * * @return The top edge of the displayed part of your view, in pixels. * /public final int getscrolly () { return mscrolly; }
Graphic Android View Scrollto (), Scrollby (), Getscrollx (), getscrolly ()