Why does the android scrollTo method send negative values to the positive direction?

Source: Internet
Author: User

In the latest demo, scrollto and scroller are used to find that the view is rolled to the positive direction of the coordinate axis only when a negative value is passed.

It's hard to understand, because the scrollto annotation is written

* @ Param x the x position to scroll

* @ Param y the y position to scroll

I checked the half-day view code.

Find a possible cause: the code is as follows (just a personal guess)


/**

* Mark the area defined by the rect (l, t, r, B) as needing to be drawn.
* The coordinates of the dirty rect are relative to the view.
* If the view is visible, {@ link # onDraw (android. graphics. Canvas )}
* Will be called at some point in the future. This must be called from
* A UI thread. To call from a non-UI thread, call {@ link # postInvalidate ()}.
* @ Param l the left position of the dirty region
* @ Param t the top position of the dirty region
* @ Param r the right position of the dirty region
* @ Param B the bottom position of the dirty region
*/
Public void invalidate (int l, int t, int r, int B ){
If (skipInvalidate ()){
Return;
}
If (mPrivateFlags & (DRAWN | HAS_BOUNDS) = (DRAWN | HAS_BOUNDS) |
(MPrivateFlags & DRAWING_CACHE_VALID) = DRAWING_CACHE_VALID |
(MPrivateFlags & INVALIDATED )! = INVALIDATED ){
MPrivateFlags & = ~ DRAWING_CACHE_VALID;
MPrivateFlags | = INVALIDATED;
MPrivateFlags | = DIRTY;
Final ViewParent p = mParent;
Final AttachInfo ai = mAttachInfo;
// Noinspection PointlessBooleanExpression, ConstantConditions
If (! HardwareRenderer. RENDER_DIRTY_REGIONS ){
If (p! = Null & ai! = Null & ai. mHardwareAccelerated ){
// Fast-track for GL-enabled applications; just invalidate the whole hierarchy
// With a null dirty rect, which tells the ViewAncestor to redraw everything
P. invalidateChild (this, null );
Return;
}
}
If (p! = Null & ai! = Null & l <r & t <B ){
Final int scrollX = mScrollX;
Final int scrollY = mScrollY;
Final Rect tmip = ai. mTmpInvalRect;
Tmip. set (l-scrollX, t-scrollY, r-scrollX, B-scrollY );
P. invalidateChild (this, tmip );
}
}

}

If the view is visible,onDraw(android.graphics.Canvas)Will be called at some point in the future

That is to say, this method is called before ondraw and a specified rectangle box is provided for the view.

The value of the rectangle is set in this way.

Final int scrollX = mScrollX;
Final int scrollY = mScrollY;
Final Rect tmip = ai. mTmpInvalRect;
Tmip. set (l-scrollX, t-scrollY, r-scrollX, B-scrollY );
P. invalidateChild (this, tmip );

Therefore, scrollto must pass a negative value. Otherwise, the coordinate system of the rectangular box area will move in the negative direction.


So I cannot understand the comment of the view scrollto method.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.