In Android, The scroroller class is a Helper class for smooth View scrolling. This is usually used in custom views. In a View, a private member is defined ). When you set the position of the mScroller scroll, it does not cause the View to scroll. It is usually used to record/calculate the scroll position of the View, and then overwrite the View's computeScroll () to complete the actual scroll.
The related APIs are described as follows:
- MScroller. getCurrX () // gets the current horizontal scroll position of mScroller.
- MScroller. getCurrY () // gets the current vertical scroll position of mScroller.
- MScroller. getFinalX () // obtain the horizontal position of the mscroler to stop.
- MScroller. getFinalY () // gets the vertical position of the final stop of mScroller.
- Mscroroller. setFinalX (int newX) // you can specify the horizontal position of the mscroroller. If no animation effect exists, you can directly jump to the target position.
- MScroller. setFinalY (int newY) // you can specify the vertical position of the mscroler. If no animation effect exists, you can directly jump to the target position.
- // Scroll. startX and startY are the positions where the rolling starts, dx and dy are the offset of the rolling, and duration is the time when the rolling is completed.
- Mscroroller. startScroll (int startX, int startY, int dx, int dy) // use the default completion time of 250 ms.
- Mscroroller. startScroll (int startX, int startY, int dx, int dy, int duration)
- MScroller. computescroloffset () // the return value is boolean. true indicates that the rolling is not completed. false indicates that the rolling is completed. This is a very important method. It is usually placed in View. computeScroll () to determine whether the rolling process ends.
For example, to customize a mmview, use Scroller to implement scrolling:
- Import android. content. Context;
- Import android. util. AttributeSet;
- Import android. util. Log;
- Import android. view. View;
- Import android. widget. LinearLayout;
- Import android. widget. Scroller;
- Public class CustomView extends LinearLayout {
- Private static final String TAG = "Scroller ";
- Private Scroller mScroller;
- Public CustomView (Context context, AttributeSet attrs ){
- Super (context, attrs );
- MScroller = new Scroller (context );
- }
- // Call this method to scroll to the target location
- Public void smoothScrollTo (int fx, int fy ){
- Int dx = fx-mScroller. getFinalX ();
- Int dy = fy-mScroller. getFinalY ();
- SmoothScrollBy (dx, dy );
- }
- // Call this method to set the relative offset of scrolling
- Public void smoothScrollBy (int dx, int dy ){
- // Sets the scroll offset of mScroller.
- Mscroroller. startScroll (mscroroller. getFinalX (), mscroroller. getFinalY (), dx, dy );
- Invalidate (); // you must call invalidate () to ensure that computeScroll () is called. Otherwise, the page may not be refreshed and the scrolling effect is invisible.
- }
- @ Override
- Public void computeScroll (){
- // First judge whether the mScroller rolling is complete
- If (mScroller. computescroloffset ()){
- // Call View's scrollTo () to complete the actual scrolling.
- ScrollTo (mscroroller. getCurrX (), mscroroller. getCurrY ());
- // You must call this method. Otherwise, the scrolling effect may not be displayed.
- PostInvalidate ();
- }
- Super. computeScroll ();
- }
- }