There are many ways to implement View sliding in android development. There are several common ways:
1. Continuous modification of View's Layout Params;
2. Displacement of View is achieved by animation.
3. Call ScrlTo () and ScrllBy () of View.
Here's the third way.
First, look at the usage of ScrollTo(),ScrllBy():
scrollTo(int x, int y):
scrollTo() indicates that the View scrolls a certain distance relative to its initial position.
- When x is positive, it means that View scrolls x distance to the left
- When x is negative, the View moves x distance to the right
- When y is positive, it means that the View scrolls up the Y distance.
- When y is negative, it means that View moves y distance downward
scrollBy(intx, int y):
See the source code to know
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}
It scrolls on the basis of the original mScrollX,mScrollY.
Note: It's not that the View itself is scrolling, but that the content in the View is scrolling.
It is true that the above two methods can scroll View, but if we want to make View scroll smoothly like our usual ViewPager, we can't do it only with these two methods, because the scroll generated by them is incoherent and flickering, so the system also provides Scroller class for us to achieve the smooth scroll of View.
Scroller from the source code can see how to use:
* <pre> private Scroller mScroller = new Scroller(context);
* ...
* public void zoomIn() {
* // Revert any animation currently in progress
* mScroller.forceFinished(true);
* // Start scrolling by providing a starting point and
* // the distance to travel
* mScroller.startScroll(0, 0, 100, 0);
* // Invalidate to request a redraw
* invalidate();
* }</pre>
*
* <p>To track the changing positions of the x/y coordinates, use
* {@link #computeScrollOffset}. The method returns a boolean to indicate
* whether the scroller is finished. If it isn't, it means that a fling or
* programmatic pan operation is still in progress. You can use this method to
* find the current offsets of the x and y coordinates, for example:</p>
*
* <pre>if (mScroller.computeScrollOffset()) {
* // Get current x and y positions
* int currX = mScroller.getCurrX();
* int currY = mScroller.getCurrY();
* ...
* }</pre>
According to the description in the above document, the use of Scroller can be summarized as the following five main steps:
- Initialize Scroller
- Call startScroll() to start scrolling
- Perform invalidate() refresh interface
- Rewrite View's computeScroll() and implement scroll-related business logic within it
- Execute invalidate() refresh interface again
mScroller.startScroll(int startX,int startY,int dx,int dy)
startX: Represents the starting position of the scroll of the View X coordinates;
startY: Represents the starting position of the View Y coordinate scroll
dx: Represents the distance the View needs to scroll from the beginning of the X coordinate (where the positive and negative dx is the same as the X of ScrllTo())
Dy: Represents the distance the View needs to scroll from the Y coordinate (positive and negative dy are the same as scrllTo's y)
/**
* Start scrolling by providing a starting point, the distance to travel,
* and the duration of the scroll.
*
* @param startX Starting horizontal scroll offset in pixels. Positive
* numbers will scroll the content to the left.
* @param startY Starting vertical scroll offset in pixels. Positive numbers
* will scroll the content up.
* @param dx Horizontal distance to travel. Positive numbers will scroll the
* content to the left.
* @param dy Vertical distance to travel. Positive numbers will scroll the
* content up.
* @param duration Duration of the scroll in milliseconds.
*/
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
mMode = SCROLL_MODE;
mFinished = false;
mDuration = duration;
mStartTime = AnimationUtils.currentAnimationTimeMillis();
mStartX = startX;
mStartY = startY;
mFinalX = startX + dx;
mFinalY = startY + dy;
mDeltaX = dx;
mDeltaY = dy;
mDurationReciprocal = 1.0f / (float) mDuration;
}
From the source code, we can see that when startScroll() is invoked, there is no scrolling operation inside, but only an assignment operation. So when this code is invoked, we need to call invalidate() method to redraw the View to call computeScroll in View. The computeScroll() method in View source code is an empty implementation, so we can use this method. There's scrolling logic.
For example:
public void computeScroll(){
if (mScroller.computeScrollOffset()) {
scrollTo(0, mScroller.getCurrY());
postInvalidate();
}
super.computeScroll();
}
In addition, in the computeScroll() method, we usually use mScroller.compuScrollOffset() to determine whether the scroll of View is continuing, return true to indicate that it is continuing, false to indicate the end.