Android scroller Simple usage

Source: Internet
Author: User
Tags gety

1. Knowledge points

    • Before you know the Scorller class, you should know that the view's Scrollto (int x, int y)/scrollby (int x, int y), what is the view coordinates, and what is the layout coordinates. Then we look at the source code of Scroller.

Scoller is generally used in custom view

public class Scroller {private int mstartx;    Starting coordinate point, x-axis direction private int mstarty;     Starting coordinate point, y-axis direction private int mcurrx;     The current coordinate point x-axis, that is, after the call to the Startscroll function, the value reached after a certain time private int mcurry;  The current coordinate point y-axis, that is, after the call to the Startscroll function, the value reached after a certain time private float mdeltax;  The distance should continue to slide, the x-axis direction private float Mdeltay;  The distance should continue to slide, the y-axis direction private Boolean mfinished;      Whether the slide operation has been completed, true//constructor public Scroller (context context) {This (context, NULL) if completed;      Public Final Boolean isfinished () {return mfinished;      }//Force the end of this slide operation public final void Forcefinished (Boolean finished) {mfinished = finished;      } public final int Getcurrx () {return mcurrx;  }/* Call this if you want to know the new location.  If it returns True, * The animation is not yet finished. Loc'll is altered to provide the * new location.     The current coordinate point is calculated based on the current elapsed time and is stored in the Mcurrx and Mcurry values public Boolean Computescrolloffset () {if (mfinished) {///has completed this animation control and returned directly to false return false;          } int timepassed = (int) (Animationutils.currentanimationtimemillis ()-mstarttime); if (timepassed < mduration) {switch (mmode) {case Scroll_mode:float x = (                  float) timepassed * mdurationreciprocal;                  ... mcurrx = Mstartx + math.round (x * mdeltax);                  Mcurry = Mstarty + math.round (x * mdeltay);              Break          ...          }              else {Mcurrx = Mfinalx;              Mcurry = Mfinaly;          Mfinished = true;      } return true; }//Start an animation control, by (StartX, Starty) in duration time forward (Dx,dy) units, that is, the arrival coordinates (STARTX+DX, starty+dy) out of public void startscroll (i          NT StartX, int starty, int dx, int dy, int duration) {mfinished = false;          mduration = Duration; Mstarttime = AnimatioNutils.currentanimationtimemillis ();       Mstartx = StartX;          Mstarty = Starty;  MFINALX = StartX + dx;          Mfinaly = Starty + dy;            Mdeltax = DX;          Mdeltay = dy;  ...      }   }

Some ways to introduce

Mscroller.getcurrx ()//Get Mscroller Current horizontal scroll position  mscroller.getcurry ()//Get the position  of the current vertical scroll of Mscroller MSCROLLER.GETFINALX ()//Get Mscroller final stop horizontal position  mscroller.getfinaly ()//Get vertical position  of Mscroller final stop MSCROLLER.SETFINALX (int newx)//Set the horizontal position of the Mscroller final stop, no animation effect, jump directly to the target position  mscroller.setfinaly (int newy)// Set Mscroller final Stop vertical position, no animation effect, jump directly to target position    //scroll, StartX, starty to start scrolling position, dx,dy for scrolling offset, duration for complete scrolling time  Mscroller.startscroll (int startX, int starty, int dx, int dy)//Use default finish time 250ms  mscroller.startscroll (int startX, int s tarty, int dx, int dy, int duration)    mscroller.computescrolloffset ()//return value is boolean,true description scrolling is not complete, false indicates that scrolling is complete. This is a very important method, usually placed in View.computescroll (), to determine whether scrolling is over or not.
    • The Computescroll () method of the view is used, and this method is called when drawing each of the view's sub-view
2. For example, the effect of pull-down refresh
Main code:
public class CustomView extends Relativelayout {private static final String TAG = "CustomView";p rivate Scroller Mscroller; Private Gesturedetector mgesturedetector;public Boolean flag = True;public Statelistener statelistener;public CustomView (Context context) {This (context, null);} Public CustomView (context context, AttributeSet Attrs) {Super (context, attrs); setclickable (true); Setlongclickable ( true); Mscroller = new Scroller (context); mgesturedetector = new Gesturedetector (context, new Customgesturelistener ());} public interface statelistener{public void Changetext ();p ublic void Recovertext (); public void Setstatelistener (Statelistener statelistener) {this.statelistener = Statelistener;} 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.GETF Inaly (); Smoothscrollby (dx, dy);} Call this method to set the relative offset of the scrolling public void Smoothscrollby (int dx, int dy) {//Set Mscroller's scroll offset mscroller.startscroll ( Mscroller.getfinalx (), mscroller.getfinaly (), DX, DY);Invalidate ();//Must call invalidate () to ensure that computescroll () will be called, otherwise it will not necessarily refresh the interface, see the scrolling effect}//when drawing the view, the method is called in the Draw () procedure. @Overridepublic void Computescroll () {///first determine if Mscroller scrolling is complete if (Mscroller.computescrolloffset ()) {//Call view Scrollto here () complete the actual scrolling Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());//You must call the method, otherwise you may not see the scrolling effect postinvalidate ();} Super.computescroll ();} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case MOTIONEVENT.ACTION_UP:LOG.I (TAG, "Get Sy" + getscrolly ()); System.out.println (GETSCROLLX () + "x--" +getscrolly () + "Y"); Smoothscrollto (0, 0); System.out.println ("Up"); Break;default:int distance = getscrolly (); System.out.println ("Distance" +distance); if (distance <= 0) {flag = True;if (Distance >-200) { Statelistener.recovertext ();} else if (distance < -200) {statelistener.changetext ();} Return Mgesturedetector.ontouchevent (event);} Else{flag = False;return false;}} Return Super.ontouchevent (event);} Class Customgesturelistener implements Gesturedetector.ongesturelisteneR {@Overridepublic Boolean ondown (motionevent e) {//TODO auto-generated method Stubreturn true;} @Overridepublic void Onshowpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onsingletapup (motionevent e) {//TODO auto-generated method Stubreturn false;} @Overridepublic boolean onscroll (motionevent E1, motionevent e2,float Distancex, float Distancey) {if (flag) {int dis = (int ) ((distanceY-0.5)/4); LOG.I (TAG, Dis + "."); Smoothscrollby (0, dis);} return false;} @Overridepublic void Onlongpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onfling ( Motionevent E1, motionevent E2, float velocityx,float velocityy) {//TODO auto-generated method Stube1.gety (); E2.gety (); System.out.println (e1.gety () + "------" +e2.gety ()); return false;}}




Android scroller Simple usage

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.