Inertia Scrolling with Edge Resistance / snapback

I implemented basic scrolling using touch drag and zoom multi-touch for user view. This works well, but now I would like to add some additional features.

For example, in the Google Maps application, when you drag the screen, after you drag it, it will still move a little (inertia). And some browsers (like iPad Safari) allow you to drag the screen beyond the visible area of ​​the website, but then the screen quickly returns to the edge of the website.

Now I would like to implement something similar, but for this I need to change the active area of ​​the screen after the touch events occurred at regular intervals to complete the animation. How can i do this?

+4
java android touch
source share
2 answers

Use OnGestureListener . To ensure smooth scrolling, create a scroller (in your user view). When the gesture listener detects a transition event, set the scroller up. Then override your own computeScroll () method.

Check out this example to find out how to implement it.

int lastX; int lastY; Scroller scroller; @Override public void computeScroll() { if (scroller.computeScrollOffset()) { if (!scrolledLastFrame) { lastX = scroller.getStartX(); lastY = scroller.getStartY(); } int dx = scroller.getCurrX() - lastX; int dy = scroller.getCurrY() - lastY; lastX = scroller.getCurrX(); lastY = scroller.getCurrY(); doScroll(dx, dy); scrolledLastFrame = true; } else { scrolledLastFrame = false; } } public void doFling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) { scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); invalidate(); } public void doScroll(int dx, int dy) { currentX+=dx; currentY+=dy; invalidate(); } private class ProgramGestureListener extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { doScroll(distanceX, distanceY); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { int max_left = getMaxHorizontalScroll(); int max_top = getMaxVerticalScroll(); int min_left = getMinHorizontalScroll(); int min_top = getMinVerticalScroll(); int startX = getCurrentHorizontalScroll(); int startY = getCurrentVerticalScroll(); doFling(startX, startY, (int) -velocityX, (int) -velocityY, min_left, max_left, min_top, max_top); return true; } } 
+10
source share

Use the Scroller class, I posted a usage example here .

If you use API 9, you can use OverScroller I think.

+1
source share

All Articles