Simple usage example of Android scroroller

Source: Internet
Author: User

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:

 
 
  1. MScroller. getCurrX () // gets the current horizontal scroll position of mScroller.
  2. MScroller. getCurrY () // gets the current vertical scroll position of mScroller.
  3. MScroller. getFinalX () // obtain the horizontal position of the mscroler to stop.
  4. MScroller. getFinalY () // gets the vertical position of the final stop of mScroller.
  5. 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.
  6. 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.
  7. // 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.
  8. Mscroroller. startScroll (int startX, int startY, int dx, int dy) // use the default completion time of 250 ms.
  9. Mscroroller. startScroll (int startX, int startY, int dx, int dy, int duration)
  10. 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:

 
 
  1. Import android. content. Context;
  2. Import android. util. AttributeSet;
  3. Import android. util. Log;
  4. Import android. view. View;
  5. Import android. widget. LinearLayout;
  6. Import android. widget. Scroller;
  7. Public class CustomView extends LinearLayout {
  8. Private static final String TAG = "Scroller ";
  9. Private Scroller mScroller;
  10. Public CustomView (Context context, AttributeSet attrs ){
  11. Super (context, attrs );
  12. MScroller = new Scroller (context );
  13. }
  14. // Call this method to scroll to the target location
  15. Public void smoothScrollTo (int fx, int fy ){
  16. Int dx = fx-mScroller. getFinalX ();
  17. Int dy = fy-mScroller. getFinalY ();
  18. SmoothScrollBy (dx, dy );
  19. }
  20. // Call this method to set the relative offset of scrolling
  21. Public void smoothScrollBy (int dx, int dy ){
  22. // Sets the scroll offset of mScroller.
  23. Mscroroller. startScroll (mscroroller. getFinalX (), mscroroller. getFinalY (), dx, dy );
  24. Invalidate (); // you must call invalidate () to ensure that computeScroll () is called. Otherwise, the page may not be refreshed and the scrolling effect is invisible.
  25. }
  26. @ Override
  27. Public void computeScroll (){
  28. // First judge whether the mScroller rolling is complete
  29. If (mScroller. computescroloffset ()){
  30. // Call View's scrollTo () to complete the actual scrolling.
  31. ScrollTo (mscroroller. getCurrX (), mscroroller. getCurrY ());
  32. // You must call this method. Otherwise, the scrolling effect may not be displayed.
  33. PostInvalidate ();
  34. }
  35. Super. computeScroll ();
  36. }
  37. }

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.