使用OnGestureListener。要提供平滑的滚动,请创建一个滚动器(在您的自定义视图中)。当手势侦听器检测到猛击事件时,将滚动器设置为向上。然后,重写自定义视图的computeScroll()方法。
检查此示例以了解如何实现它。
int lastX;int lastY;Scroller scroller; @Overridepublic 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; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)