When sliding is involved, VIEW is involved. As we all know, the UI interface of android is composed of a View and a derived class of View. View is used as the base class, the various la s in common la s are the subclasses of ViewGroup derived from them. ViewGroup serves as the container of each component to build the overall UI. The following shows the structure of the android UI:
View Source Code
/** * Implement this to do your drawing. * * @param canvas the canvas on which the background will be drawn */ protected void onDraw(Canvas canvas) { }We can find that,
The implementation of View is generally through drawing
OnDraw Method
If you want to change its interface, you can rewrite it.
OnDraw, to achieve your effect, in the source code, you cannot find
public void addView(View child) { addView(child, -1); }This method, because it does not know, only in its derived class such
This method is used in ViewGroup to add a sub-layout.
ViewGroup, as a component container, can contain any component, but you must override its onLayout () method and onMeasure() To set the location and size of the container layout.
First, we must understand that the Android View has no boundaries and the Canvas has no boundaries. However, when we draw a specific View, we perform some operations on the Canvas object. For example: translate (translation), clipRect (CUT), etc., in order to meet our requirements for drawing the Canvas object, we can call this borderless View "view coordinate"-it is not restricted by the physical screen. Normally, a Layout file is only the display area of the view. If it is exceeded, it cannot be displayed in the area of the parent view, we can call this boundary View "layout coordinates" ------ layout size allocated by the parent View to the Child view. In addition, the starting coordinate of a view on the screen is located at the starting point of the view coordinate, as shown in.
In fact, the coordinates in the upper left corner of the parent class view are the origin (), rather than the origin in the upper left corner of the overall ViewGroup.
Because the layout coordinates can only display a specific part of the content, we can only move the coordinates of the layout coordinates to display any position of the View coordinates.
(Note: for example, the cocos2D layout is different from the android layout coordinate origin coordinate, which is the origin in the lower left corner .)
Here we will roughly mention the View andViewGroup, (many gods have analyzed this item on the Internet, but only a small number of excerpt records are made here) in order to bring out the leading roles of scrollTo and scrollBy today.
Check the source code and you will find:
/** * The offset, in pixels, by which the content of this view is scrolled * horizontally. * {@hide} */ @ViewDebug.ExportedProperty(category = "scrolling") protected int mScrollX; /** * The offset, in pixels, by which the content of this view is scrolled * vertically. * {@hide} */ @ViewDebug.ExportedProperty(category = "scrolling") protected int mScrollY; /** * Return the scrolled left position of this view. This is the left edge of * the displayed part of your view. You do not need to draw any pixels * farther left, since those are outside of the frame of your view on * screen. * * @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. This is the top edge of * the displayed part of your view. You do not need to draw any pixels above * it, since those are 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; }
MScrollX: the horizontal offset of x from the starting position of the view.
MScrollY: Indicates departureView start positionY vertical offset
PassGetScrollX () andGetScrollY () method.
Note:MScrollX andMScrollY is not a coordinate, but an offset.
/** * Set the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the 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(); } } } /** * Move the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the amount of pixels to scroll by horizontally * @param y the amount of pixels to scroll by vertically */ public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y); }
From the code above, we can see that,
The difference between scrollTo and scrollBy is that the effect of the two is the same.
ScrollTo (int x, int y ):
If the offset position changesNew value assigned by mScrollY,Change the current location.
Note: x and y represent the offset instead of coordinate points.
For example:
I want to move the view to the coordinate point (100,100), so my offset is (0, 0)-(100,100) = (-100,-100), and I will execute the view. scrollTo (-100,-100.
ScrollBy (int x, int y ):
It can be seen from the source code that it actually callsScrollTo (mScrollX + x, mScrollY + y );
MScrollX + x andMScrollY + y indicates that the offset is generated based on the original offset,In general, it is offset from our current position.
It is moved according to the parent class VIEW. If it is moved beyond the limit, it is not displayed.
View the above information and you will know about it.
The following example shows how to use these two methods,
As follows:
After figuring out the two methods, it is more helpful for the scroroller drag class and implementation of several drag effects.