Smooth scrolling in Android?

I have no experience with OpenGL nor accelerometer, but swipe (called fling in Android's API) is not hard to achieve. First thing you need when making such a custom View is implementing a GestureDetector and call its onTouchEvent() in your view's onTouchEvent() GestureDetector mGD = new GestureDetector(getContext(), new SimpleOnGestureListener() { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // beware, it can scroll to infinity scrollBy((int)distanceX, (int)distanceY); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float vX, float vY) { mScroller. Fling(getScrollX(), getScrollY(), -(int)vX, -(int)vY, 0, (int)mMaxScrollX, 0, (int)mMaxScrollY); invalidate(); // don't remember if it's needed return true; } @Override public boolean onDown(MotionEvent e) { if(!mScroller.isFinished() ) { // is flinging mScroller.

ForceFinished(true); // to stop flinging on touch } return true; // else won't work } }); @Override public boolean onTouchEvent(MotionEvent event) { return mGD. OnTouchEvent(event); } While OnGestureListener.onScroll() calls directly View.scrollBy() for the onFling() method you'll need a Scroller Scroller is a simple object that, as reference says, encapsulates scrolling. It can be used for continuous scrolling or to react to flings.Scroller.fling() begin a "simulation" of fling scroll inside itself, and by watching it you can copy its smoothness with a continuous redrawing animation: Override protected void onDraw(Canvas canvas) { // ....your drawings.... // scrollTo invalidates, so until animation won't finish it will be called // (used after a Scroller.fling() ) if(mScroller.

ComputeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); } } that is, until animation is running, calculate the point we reached and scroll there As a last note: remember to return true in your OnGestureListener.onDown() even if you don't want to do anything on down, or it won't work And be careful, because Scroller in Android 2.2 has a bug for which the fling animation will not actually end even if it reaches the limits you passed as arguments (yet computed offset respects them, so it won't actually move) EDIT instead of calling computeScrollOffset() in onDraw() I've learned here that the correct way is using it in an overridden View.computeScroll().

I have no experience with OpenGL nor accelerometer, but swipe (called fling in Android's API) is not hard to achieve. First thing you need when making such a custom View, is implementing a GestureDetector and call its onTouchEvent() in your view's onTouchEvent() GestureDetector mGD = new GestureDetector(getContext(), new SimpleOnGestureListener() { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // beware, it can scroll to infinity scrollBy((int)distanceX, (int)distanceY); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float vX, float vY) { mScroller. Fling(getScrollX(), getScrollY(), -(int)vX, -(int)vY, 0, (int)mMaxScrollX, 0, (int)mMaxScrollY); invalidate(); // don't remember if it's needed return true; } @Override public boolean onDown(MotionEvent e) { if(!mScroller.isFinished() ) { // is flinging mScroller.

ForceFinished(true); // to stop flinging on touch } return true; // else won't work } }); @Override public boolean onTouchEvent(MotionEvent event) { return mGD. OnTouchEvent(event); } While OnGestureListener.onScroll() calls directly View.scrollBy(), for the onFling() method you'll need a Scroller. Scroller is a simple object that, as reference says, encapsulates scrolling.It can be used for continuous scrolling or to react to flings.

Scroller.fling() begin a "simulation" of fling scroll inside itself, and by watching it you can copy its smoothness with a continuous redrawing animation: @Override protected void onDraw(Canvas canvas) { // ....your drawings.... // scrollTo invalidates, so until animation won't finish it will be called // (used after a Scroller.fling() ) if(mScroller. ComputeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); } } that is, until animation is running, calculate the point we reached and scroll there.As a last note: remember to return true in your OnGestureListener.onDown(), even if you don't want to do anything on down, or it won't work. And be careful, because Scroller in Android 2.2 has a bug for which the fling animation will not actually end even if it reaches the limits you passed as arguments (yet computed offset respects them, so it won't actually move).

----EDIT---- instead of calling computeScrollOffset() in onDraw(), I've learned here that the correct way is using it in an overridden View.computeScroll().

Thanks bigstones. Looks like that I have to read some more to understand your sample and make my code work. I really appreciate your effort to provide code snippet and explanation.

– prashant Feb 10 at 13:39.

The Floating Images app is an open source project. code.google.com/p/floatingimage.

Thank you. I appreciate your help in this. – prashant Feb 21 at 5:40 @rallat : it'not relate to Android.

– Shubh Oct 22 at 8:05.

I would like to add the smooth scrolling functionality into my application. Could anyone offer any examples of smooth scrolling?

The fourth Harry Potter is Harry Potter and The Goblet of Fire. It is a very good book, even though when it was depicted to movie I felt a bit disappointed since the filmmaker cut many parts in the book. Anyway, both the book and movie are great to enjoy.

Have a nice reading and watching. :).

The fourth Harry Potter is Harry Potter and The Goblet of Fire. It is a very good book, even though when it was depicted to movie I felt a bit disappointed since the filmmaker cut many parts in the book. Anyway, both the book and movie are great to enjoy.

Have a nice reading and watching. The fourth Harry Potter is Harry Potter and The Goblet of Fire. It is a very good book, even though when it was depicted to movie I felt a bit disappointed since the filmmaker cut many parts in the book.

Anyway, both the book and movie are great to enjoy. Have a nice reading and watching.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions