在Android中,滚动通常通过使用ScrollView 该类来实现 。任何可能超出其容器边界的标准布局应该嵌套在a中ScrollView以提供由框架管理的可滚动视图。只有在特殊情况下才需要实现自定义滚动条。本课描述了这样一种场景:显示滚动效果以响应使用滚动条的触摸手势。
您可以使用滚动条(Scroller或OverScroller)收集产生滚动动画以响应触摸事件所需的数据。它们是相似的,但 OverScroller 包括用于向用户指示他们已经在泛或手动手势之后到达内容边缘的方法。当用户到达内容边缘时,InteractiveChart示例使用EdgeEffect类(实际上是EdgeEffectCompat类)来显示“发光”效果。
注意:我们建议您使用OverScroller而不是Scroller滚动动画。 OverScroller提供与旧设备的最佳后向兼容性。
另外请注意,在实现自动滚动时,您通常只需要使用滚动条。ScrollView而 HorizontalScrollView如果你窝在其中布局做这一切×××。
使用平台标准的滚动物理(摩擦,速度等),滚动器用于随着时间的推移进行动画滚动。滚动器本身并不实际绘制任何东西。随着时间的推移,滚动器会为您追踪滚动偏移量,但它们不会自动将这些位置应用于您的视图。您有责任按照使滚动动画看起来平滑的速率获取和应用新的坐标。
请参阅以下相关资源:
了解滚动术语
“滚动”是一个可以在Android中采用不同含义的词,具体取决于上下文。
滚动是移动视口的一般过程(也就是您正在查看的内容的“窗口”)。当滚动在x轴和y轴上时,称为 平移。随该类提供的示例应用程序InteractiveChart演示了两种不同类型的滚动,拖动和拖动:
- 拖动是用户在触摸屏上拖动手指时发生的滚动类型。简单的拖动往往是通过覆盖来实现 onScroll()在 GestureDetector.OnGestureListener。有关拖动的更多讨论,请参阅 拖动和缩放。
- 甩甩是用户快速拖动手指时发生的滚动类型。用户抬起手指后,通常需要继续滚动(移动视口),但是会减速直到视口停止移动。投掷可以通过重写来实现 onFling() 在GestureDetector.OnGestureListener,并且通过使用滚动条对象。这是本课的主题。
通常将滚动条对象与滑动手势结合使用,但它们可以用于任何需要UI显示滚动以响应触摸事件的上下文中。例如,您可以重写 onTouchEvent()以直接处理触摸事件,并产生滚动效果或响应这些触摸事件的“贴紧页面”动画。
实现基于触摸的滚动
本节介绍如何使用滚动条。下面显示的代码片段来自InteractiveChart本课程提供的示例。它使用a GestureDetector,并覆盖该 GestureDetector.SimpleOnGestureListener方法 onFling()。它OverScroller用来跟踪扔手势。如果用户在投掷手势后到达内容边缘,则应用程序会显示“发光”效果。
注:该InteractiveChart示例应用程序显示一个图表,你可以缩放,平移,滚动,等等。在以下代码片段中, mContentRect表示图表将被绘制到的视图内的矩形坐标。在任何给定时间,整个图表域和范围的一个子集被绘制到这个矩形区域中。 mCurrentViewport表示当前在屏幕中可见的图表部分。由于像素偏移通常被视为整数, mContentRect因此属于该类型Rect。由于图域和范围是十进制/浮点值,mCurrentViewport因此属于该类型RectF。
代码片段的第一部分显示了以下内容的实现 onFling():
// The current viewport. This rectangle represents the currently visible
// chart domain and range. The viewport is the part of the app that the
// user manipulates via touch gestures.
private RectF mCurrentViewport =
new RectF(AXIS_X_MIN, AXIS_Y_MIN, AXIS_X_MAX, AXIS_Y_MAX);
// The current destination rectangle (in pixel coordinates) into which the
// chart data should be drawn.
private Rect mContentRect;
private OverScroller mScroller;
private RectF mScrollerStartViewport;
...
private final GestureDetector.SimpleOnGestureListener mGestureListener
= new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
// Initiates the decay phase of any active edge effects.
releaseEdgeEffects();
mScrollerStartViewport.set(mCurrentViewport);
// Aborts any active scroll animations and invalidates.
mScroller.forceFinished(true);
ViewCompat.postInvalidateOnAnimation(InteractiveLineGraphView.this);
return true;
}
...
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
fling((int) -velocityX, (int) -velocityY);
return true;
}
};
private void fling(int velocityX, int velocityY) {
// Initiates the decay phase of any active edge effects.
releaseEdgeEffects();
// Flings use math in pixels (as opposed to math based on the viewport).
Point surfaceSize = computeScrollSurfaceSize();
mScrollerStartViewport.set(mCurrentViewport);
int startX = (int) (surfaceSize.x * (mScrollerStartViewport.left -
AXIS_X_MIN) / (
AXIS_X_MAX - AXIS_X_MIN));
int startY = (int) (surfaceSize.y * (AXIS_Y_MAX -
mScrollerStartViewport.bottom) / (
AXIS_Y_MAX - AXIS_Y_MIN));
// Before flinging, aborts the current animation.
mScroller.forceFinished(true);
// Begins the animation
mScroller.fling(
// Current scroll position
startX,
startY,
velocityX,
velocityY,
/*
* Minimum and maximum scroll positions. The minimum scroll
* position is generally zero and the maximum scroll position
* is generally the content size less the screen size. So if the
* content width is 1000 pixels and the screen width is 200
* pixels, the maximum scroll offset should be 800 pixels.
*/
0, surfaceSize.x - mContentRect.width(),
0, surfaceSize.y - mContentRect.height(),
// The edges of the content. This comes into play when using
// the EdgeEffect class to draw "glow" overlays.
mContentRect.width() / 2,
mContentRect.height() / 2);
// Invalidates to trigger computeScroll()
ViewCompat.postInvalidateOnAnimation(this);
}
当onFling()调用时 postInvalidateOnAnimation(),它会触发 computeScroll()更新x和y的值。这通常是在视图孩子使用滚动器对象来动画滚动时完成的,如本例所示。
大多数视图直接传递滚动条对象的x和y位置 scrollTo()。下面的实现computeScroll() 采用不同的方法 - 它调用 computeScrollOffset()获取x和y的当前位置。当满足显示过度滚动“发光”边缘效果的条件(显示放大,x或y超出范围,并且应用程序尚未显示过度滚动)时,代码将设置超滚动发光效果并调用 postInvalidateOnAnimation() 来触发视图上的无效:
// Edge effect / overscroll tracking objects.
private EdgeEffectCompat mEdgeEffectTop;
private EdgeEffectCompat mEdgeEffectBottom;
private EdgeEffectCompat mEdgeEffectLeft;
private EdgeEffectCompat mEdgeEffectRight;
private boolean mEdgeEffectTopActive;
private boolean mEdgeEffectBottomActive;
private boolean mEdgeEffectLeftActive;
private boolean mEdgeEffectRightActive;
@Override
public void computeScroll() {
super.computeScroll();
boolean needsInvalidate = false;
// The scroller isn't finished, meaning a fling or programmatic pan
// operation is currently active.
if (mScroller.computeScrollOffset()) {
Point surfaceSize = computeScrollSurfaceSize();
int currX = mScroller.getCurrX();
int currY = mScroller.getCurrY();
boolean canScrollX = (mCurrentViewport.left > AXIS_X_MIN
|| mCurrentViewport.right < AXIS_X_MAX);
boolean canScrollY = (mCurrentViewport.top > AXIS_Y_MIN
|| mCurrentViewport.bottom < AXIS_Y_MAX);
/*
* If you are zoomed in and currX or currY is
* outside of bounds and you are not already
* showing overscroll, then render the overscroll
* glow edge effect.
*/
if (canScrollX
&& currX < 0
&& mEdgeEffectLeft.isFinished()
&& !mEdgeEffectLeftActive) {
mEdgeEffectLeft.onAbsorb((int)
OverScrollerCompat.getCurrVelocity(mScroller));
mEdgeEffectLeftActive = true;
needsInvalidate = true;
} else if (canScrollX
&& currX > (surfaceSize.x - mContentRect.width())
&& mEdgeEffectRight.isFinished()
&& !mEdgeEffectRightActive) {
mEdgeEffectRight.onAbsorb((int)
OverScrollerCompat.getCurrVelocity(mScroller));
mEdgeEffectRightActive = true;
needsInvalidate = true;
}
if (canScrollY
&& currY < 0
&& mEdgeEffectTop.isFinished()
&& !mEdgeEffectTopActive) {
mEdgeEffectTop.onAbsorb((int)
OverScrollerCompat.getCurrVelocity(mScroller));
mEdgeEffectTopActive = true;
needsInvalidate = true;
} else if (canScrollY
&& currY > (surfaceSize.y - mContentRect.height())
&& mEdgeEffectBottom.isFinished()
&& !mEdgeEffectBottomActive) {
mEdgeEffectBottom.onAbsorb((int)
OverScrollerCompat.getCurrVelocity(mScroller));
mEdgeEffectBottomActive = true;
needsInvalidate = true;
}
...
}
以下是执行实际缩放的代码部分:
// Custom object that is functionally similar to Scroller
Zoomer mZoomer;
private PointF mZoomFocalPoint = new PointF();
...
// If a zoom is in progress (either programmatically or via double
// touch), performs the zoom.
if (mZoomer.computeZoom()) {
float newWidth = (1f - mZoomer.getCurrZoom()) *
mScrollerStartViewport.width();
float newHeight = (1f - mZoomer.getCurrZoom()) *
mScrollerStartViewport.height();
float pointWithinViewportX = (mZoomFocalPoint.x -
mScrollerStartViewport.left)
/ mScrollerStartViewport.width();
float pointWithinViewportY = (mZoomFocalPoint.y -
mScrollerStartViewport.top)
/ mScrollerStartViewport.height();
mCurrentViewport.set(
mZoomFocalPoint.x - newWidth * pointWithinViewportX,
mZoomFocalPoint.y - newHeight * pointWithinViewportY,
mZoomFocalPoint.x + newWidth * (1 - pointWithinViewportX),
mZoomFocalPoint.y + newHeight * (1 - pointWithinViewportY));
constrainViewport();
needsInvalidate = true;
}
if (needsInvalidate) {
ViewCompat.postInvalidateOnAnimation(this);
}
这是computeScrollSurfaceSize()在上面的代码段中调用的方法。它以像素为单位计算当前可滚动表面大小。例如,如果整个图表区域都可见,则这只是当前的大小mContentRect。如果图表在两个方向都放大了200%,则返回的尺寸将是水平和垂直两倍。
private Point computeScrollSurfaceSize() {
return new Point(
(int) (mContentRect.width() * (AXIS_X_MAX - AXIS_X_MIN)
/ mCurrentViewport.width()),
(int) (mContentRect.height() * (AXIS_Y_MAX - AXIS_Y_MIN)
/ mCurrentViewport.height()));
}
对于卷轴使用方法的另一示例,请参见 源代码的 ViewPager类。它响应甩尾而滚动,并使用滚动来实现“贴合到页面”动画。
Lastest Update:2018.04.25
联系我
QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ
公众号推荐: