Simple usage of Android scroroller

Source: Internet
Author: User

Simple usage of Android scroroller
1. Knowledge Point

Before understanding the Scorller class, you should first know the differences between View ScrollTo (int x, int y)/ScrollBy (int x, int y) and View coordinates, what is the layout coordinate. Then let's take a look at the Scroller source code.

Scoller is generally used in custom views.

Public class Scroller {private int mStartX; // start coordinate point, private int mStartY to the X axis; // start coordinate point, private int mCurrX to the Y axis; // The current coordinate point X axis, that is, after the startScroll function is called, the private int mCurrY value reaches after a certain period of time; // The current coordinate point Y axis, that is, after the startScroll function is called, private float mDeltaX after a certain period of time; // The distance from which the slide should continue, private float mDeltaY in the X axis; // The distance from which the slide should continue, private boolean mFinished in the Y axis; // whether this slide operation has been completed. If this operation is completed, it is true // constructor public Scroller (Context context) {this (context, null);} public final boolean isFinished () {return mFinished;} // force end of this sliding screen operation public final void forceFinished (boolean finished) {mFinished = finished;} public final int getCurrX () {return mCurrX ;} /* Call this when you want to know the new location. if it returns true, * the animation is not yet finished. loc will be altered to provide the * new location. * /// calculate the current coordinate point based on the time that has elapsed, and save the public boolean computescroloffset () {if (mFinished) in the mCurrX and mCurrY values) {// The animation control has been completed, and the return value is 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;} // starts an animation control and advances by (startX, startY) within the duration time (dx, dy) units, that is, the arrival coordinates are (startX + dx, startY + dy) public void startScroll (int 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 methods

MScroller. getCurrX () // obtain the position of the current horizontal scroll of mScroller. getCurrY () // obtain the position of the mscroler in vertical scroll. getFinalX () // obtain the horizontal position of the final stop mscroler. getFinalY () // obtain the vertical position that the mscroler stops. setFinalX (int newX) // set the final horizontal position of the mscroler. If there is no animation effect, Jump directly to the target position. setFinalY (int newY) // sets the final vertical position of the mscroler. If there is no animation effect, Jump directly to the target position. // scroll. startX and startY are the start positions. dx, dy is the offset of rolling, and duration is the time of rolling. startScroll (int StartX, int startY, int dx, int dy) // use the default completion time 250 ms mScroller. startScroll (int startX, int startY, int dx, int dy, int duration) mscroroller. 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.
View's computeScroll () method is used. This method is called when every child View of the view is drawn.
2. Example
Implement pull-down refresh
Main Code:
Public class CustomView extends RelativeLayout {private static final String TAG = "CustomView"; private Scroller mScroller; private GestureDetector listener; public boolean flag = true; public StateListener stateListener; public CustomView (Context 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 mgmgesturelistener ();} public interface StateListener {public void changeText (); public 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-MS Croroller. getFinalY (); smoothScrollBy (dx, dy);} // call this method to set the relative offset of the scrolling public void smoothScrollBy (int dx, int dy) {// set the scroll offset of mScroller. startScroll (mScroller. getFinalX (), mScroller. getFinalY (), dx, dy); invalidate (); // you must call invalidate () to ensure that computeScroll () is called. Otherwise, the interface may not be refreshed, you cannot see the scroll effect} // this method is called during the draw () process when you draw a View. @ Overridepublic void computeScroll () {// first judge whether the mScroller rolling is complete if (mScroller. computescroloffset () {// here, the View scrollTo () is called to complete the actual rolling scrollTo (mScroller. getCurrX (), mScroller. getCurrY (); // you must call this method. Otherwise, you may not be able to see the rolling 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 ;}}}




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.