在 Android 中,捲動通常是透過 ScrollView
類別達成。將任何可能超出容器邊界的標準版面配置,巢狀結構化至 ScrollView
中,以提供由架構管理的捲動檢視畫面。只有在特殊情況下,才需要實作自訂捲軸。本文說明如何使用 scrollers,在觸控手勢的觸發下顯示捲動效果。
應用程式可以使用捲軸 (Scroller
或 OverScroller
) 收集資料,以便在觸控事件發生時產生捲動動畫。兩者類似,但 OverScroller
也包含在平移或快速滑動手勢後,向使用者指出他們已抵達內容邊緣的方法。
- 從 Android 12 (API 級別 31) 開始,視覺元素會在拖曳事件中延展並彈回,在快速滑動事件中快速滑動並彈回。
- 在 Android 11 (API 級別 30) 以下版本中,拖曳或撥動手勢到邊緣後,邊界會顯示「發光」效果。
本文中的 InteractiveChart
範例會使用 EdgeEffect
類別顯示這些過度捲動效果。
您可以使用捲動器,透過摩擦力、速度和其他品質等平台標準捲動物理特性,隨時間變化捲動動畫。捲軸本身不會繪製任何內容。捲軸會追蹤一段時間內的捲動偏移,但不會自動將這些位置套用至檢視區塊。您必須以適當的速率取得並套用新座標,才能讓捲動動畫看起來流暢。
瞭解捲動相關術語
在 Android 中,「捲動」一詞的涵義會因情境而異。
捲動是指移動檢視區塊的一般程序,也就是您正在查看的內容「視窗」。當捲動發生在 x 軸和 y 軸時,稱為「平移」。本文中的 InteractiveChart
範例應用程式說明瞭兩種不同的捲動、拖曳和撥動類型:
- 拖曳:使用者在觸控螢幕上拖曳手指時,就會發生這類捲動。如要實作拖曳功能,請在
GestureDetector.OnGestureListener
中覆寫onScroll()
。如要進一步瞭解拖曳,請參閱「拖曳和縮放」。 - 甩動:使用者快速拖曳並抬起手指時,就會發生這類捲動。使用者放開手指後,您通常會希望繼續移動檢視區塊,但要減速,直到檢視區塊停止移動為止。如要實作快速滑動功能,請在
GestureDetector.OnGestureListener
中覆寫onFling()
,並使用捲軸物件。 - 平移:沿著 x 軸和 y 軸同時捲動,稱為「平移」。
一般來說,捲動器物件會與快速滑動手勢搭配使用,但您可以在任何情境中使用捲動器物件,讓使用者介面因應觸控事件顯示捲動效果。舉例來說,您可以覆寫 onTouchEvent()
直接處理觸控事件,並產生捲動效果,或回應這些觸控事件的「貼齊頁面」動畫。
內建捲動實作的元件
下列 Android 元件內建捲動和過度捲動行為的支援:
GridView
HorizontalScrollView
ListView
NestedScrollView
RecyclerView
ScrollView
ViewPager
ViewPager2
如果應用程式需要在其他元件內支援捲動和過度捲動,請完成下列步驟:
- 建立自訂觸控式捲動實作項目。
- 如要支援搭載 Android 12 以上版本的裝置,請實作延伸越區捲動效果。
建立自訂觸控式捲動實作項目
如果應用程式使用的元件未內建支援捲動和過度捲動功能,本節將說明如何建立自己的捲動器。
以下程式碼片段來自InteractiveChart
範例。這會使用 GestureDetector
並覆寫 GestureDetector.SimpleOnGestureListener
方法 onFling()
。它會使用 OverScroller
追蹤輕拂手勢。如果使用者在執行快速滑動手勢後抵達內容邊緣,容器會指出使用者何時抵達內容結尾。顯示方式取決於裝置執行的 Android 版本:
- 在 Android 12 以上版本,視覺元素會延展並彈回。
- 在 Android 11 以下版本中,視覺元素會顯示發光效果。
下列程式碼片段的第一部分顯示 onFling()
的實作方式:
Kotlin
// Viewport extremes. See currentViewport for a discussion of the viewport. private val AXIS_X_MIN = -1f private val AXIS_X_MAX = 1f private val AXIS_Y_MIN = -1f private val AXIS_Y_MAX = 1f // The current viewport. This rectangle represents the visible chart // domain and range. The viewport is the part of the app that the // user manipulates via touch gestures. private val currentViewport = 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 must be drawn. private lateinit var contentRect: Rect private lateinit var scroller: OverScroller private lateinit var scrollerStartViewport: RectF ... private val gestureListener = object : GestureDetector.SimpleOnGestureListener() { override fun onDown(e: MotionEvent): Boolean { // Initiates the decay phase of any active edge effects. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects() } scrollerStartViewport.set(currentViewport) // Aborts any active scroll animations and invalidates. scroller.forceFinished(true) ViewCompat.postInvalidateOnAnimation(this@InteractiveLineGraphView) return true } ... override fun onFling( e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float ): Boolean { fling((-velocityX).toInt(), (-velocityY).toInt()) return true } } private fun fling(velocityX: Int, velocityY: Int) { // Initiates the decay phase of any active edge effects. // On Android 12 and later, the edge effect (stretch) must // continue. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects() } // Flings use math in pixels, as opposed to math based on the viewport. val surfaceSize: Point = computeScrollSurfaceSize() val (startX: Int, startY: Int) = scrollerStartViewport.run { set(currentViewport) (surfaceSize.x * (left - AXIS_X_MIN) / (AXIS_X_MAX - AXIS_X_MIN)).toInt() to (surfaceSize.y * (AXIS_Y_MAX - bottom) / (AXIS_Y_MAX - AXIS_Y_MIN)).toInt() } // Before flinging, stops the current animation. scroller.forceFinished(true) // Begins the animation. scroller.fling( // Current scroll position. startX, startY, velocityX, velocityY, /* * Minimum and maximum scroll positions. The minimum scroll * position is generally 0 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 is 800 pixels. */ 0, surfaceSize.x - contentRect.width(), 0, surfaceSize.y - contentRect.height(), // The edges of the content. This comes into play when using // the EdgeEffect class to draw "glow" overlays. contentRect.width() / 2, contentRect.height() / 2 ) // Invalidates to trigger computeScroll(). ViewCompat.postInvalidateOnAnimation(this) }
Java
// Viewport extremes. See currentViewport for a discussion of the viewport. private static final float AXIS_X_MIN = -1f; private static final float AXIS_X_MAX = 1f; private static final float AXIS_Y_MIN = -1f; private static final float AXIS_Y_MAX = 1f; // The current viewport. This rectangle represents the visible chart // domain and range. The viewport is the part of the app that the // user manipulates via touch gestures. private RectF currentViewport = 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 must be drawn. private final Rect contentRect = new Rect(); private final OverScroller scroller; private final RectF scrollerStartViewport = new RectF(); // Used only for zooms and flings. ... private final GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDown(MotionEvent e) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects(); } scrollerStartViewport.set(currentViewport); scroller.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. // On Android 12 and later, the edge effect (stretch) must // continue. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects(); } // Flings use math in pixels, as opposed to math based on the viewport. Point surfaceSize = computeScrollSurfaceSize(); scrollerStartViewport.set(currentViewport); int startX = (int) (surfaceSize.x * (scrollerStartViewport.left - AXIS_X_MIN) / ( AXIS_X_MAX - AXIS_X_MIN)); int startY = (int) (surfaceSize.y * (AXIS_Y_MAX - scrollerStartViewport.bottom) / ( AXIS_Y_MAX - AXIS_Y_MIN)); // Before flinging, stops the current animation. scroller.forceFinished(true); // Begins the animation. scroller.fling( // Current scroll position. startX, startY, velocityX, velocityY, /* * Minimum and maximum scroll positions. The minimum scroll * position is generally 0 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 is 800 pixels. */ 0, surfaceSize.x - contentRect.width(), 0, surfaceSize.y - contentRect.height(), // The edges of the content. This comes into play when using // the EdgeEffect class to draw "glow" overlays. contentRect.width() / 2, contentRect.height() / 2); // Invalidates to trigger computeScroll(). ViewCompat.postInvalidateOnAnimation(this); }
當 onFling()
呼叫 postInvalidateOnAnimation()
時,會觸發 computeScroll()
更新 x 和 y 的值。如先前的範例所示,當檢視區塊子項使用捲動器物件動畫化捲動時,通常會執行這項操作。
大多數檢視區塊會直接將捲動器物件的 x 和 y 位置傳遞至 scrollTo()
。下列 computeScroll()
的實作方式採用不同做法:呼叫 computeScrollOffset()
取得 x 和 y 的目前位置。當符合顯示過度捲動「發光」邊緣效果的條件時 (也就是顯示畫面已縮放、x 或 y 超出界限,且應用程式尚未顯示過度捲動),程式碼會設定過度捲動發光效果,並呼叫 postInvalidateOnAnimation()
,在檢視區塊上觸發失效。
Kotlin
// Edge effect/overscroll tracking objects. private lateinit var edgeEffectTop: EdgeEffect private lateinit var edgeEffectBottom: EdgeEffect private lateinit var edgeEffectLeft: EdgeEffect private lateinit var edgeEffectRight: EdgeEffect private var edgeEffectTopActive: Boolean = false private var edgeEffectBottomActive: Boolean = false private var edgeEffectLeftActive: Boolean = false private var edgeEffectRightActive: Boolean = false override fun computeScroll() { super.computeScroll() var needsInvalidate = false // The scroller isn't finished, meaning a fling or // programmatic pan operation is active. if (scroller.computeScrollOffset()) { val surfaceSize: Point = computeScrollSurfaceSize() val currX: Int = scroller.currX val currY: Int = scroller.currY val (canScrollX: Boolean, canScrollY: Boolean) = currentViewport.run { (left > AXIS_X_MIN || right < AXIS_X_MAX) to (top > AXIS_Y_MIN || bottom < AXIS_Y_MAX) } /* * If you are zoomed in, currX or currY is * outside of bounds, and you aren't already * showing overscroll, then render the overscroll * glow edge effect. */ if (canScrollX && currX < 0 && edgeEffectLeft.isFinished && !edgeEffectLeftActive) { edgeEffectLeft.onAbsorb(scroller.currVelocity.toInt()) edgeEffectLeftActive = true needsInvalidate = true } else if (canScrollX && currX > surfaceSize.x - contentRect.width() && edgeEffectRight.isFinished && !edgeEffectRightActive) { edgeEffectRight.onAbsorb(scroller.currVelocity.toInt()) edgeEffectRightActive = true needsInvalidate = true } if (canScrollY && currY < 0 && edgeEffectTop.isFinished && !edgeEffectTopActive) { edgeEffectTop.onAbsorb(scroller.currVelocity.toInt()) edgeEffectTopActive = true needsInvalidate = true } else if (canScrollY && currY > surfaceSize.y - contentRect.height() && edgeEffectBottom.isFinished && !edgeEffectBottomActive) { edgeEffectBottom.onAbsorb(scroller.currVelocity.toInt()) edgeEffectBottomActive = true needsInvalidate = true } ... } }
Java
// Edge effect/overscroll tracking objects. private EdgeEffectCompat edgeEffectTop; private EdgeEffectCompat edgeEffectBottom; private EdgeEffectCompat edgeEffectLeft; private EdgeEffectCompat edgeEffectRight; private boolean edgeEffectTopActive; private boolean edgeEffectBottomActive; private boolean edgeEffectLeftActive; private boolean edgeEffectRightActive; @Override public void computeScroll() { super.computeScroll(); boolean needsInvalidate = false; // The scroller isn't finished, meaning a fling or // programmatic pan operation is active. if (scroller.computeScrollOffset()) { Point surfaceSize = computeScrollSurfaceSize(); int currX = scroller.getCurrX(); int currY = scroller.getCurrY(); boolean canScrollX = (currentViewport.left > AXIS_X_MIN || currentViewport.right < AXIS_X_MAX); boolean canScrollY = (currentViewport.top > AXIS_Y_MIN || currentViewport.bottom < AXIS_Y_MAX); /* * If you are zoomed in, currX or currY is * outside of bounds, and you aren't already * showing overscroll, then render the overscroll * glow edge effect. */ if (canScrollX && currX < 0 && edgeEffectLeft.isFinished() && !edgeEffectLeftActive) { edgeEffectLeft.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectLeftActive = true; needsInvalidate = true; } else if (canScrollX && currX > (surfaceSize.x - contentRect.width()) && edgeEffectRight.isFinished() && !edgeEffectRightActive) { edgeEffectRight.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectRightActive = true; needsInvalidate = true; } if (canScrollY && currY < 0 && edgeEffectTop.isFinished() && !edgeEffectTopActive) { edgeEffectRight.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectTopActive = true; needsInvalidate = true; } else if (canScrollY && currY > (surfaceSize.y - contentRect.height()) && edgeEffectBottom.isFinished() && !edgeEffectBottomActive) { edgeEffectRight.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectBottomActive = true; needsInvalidate = true; } ... }
以下是執行實際縮放作業的程式碼部分:
Kotlin
lateinit var zoomer: Zoomer val zoomFocalPoint = PointF() ... // If a zoom is in progress—either programmatically // or through double touch—this performs the zoom. if (zoomer.computeZoom()) { val newWidth: Float = (1f - zoomer.currZoom) * scrollerStartViewport.width() val newHeight: Float = (1f - zoomer.currZoom) * scrollerStartViewport.height() val pointWithinViewportX: Float = (zoomFocalPoint.x - scrollerStartViewport.left) / scrollerStartViewport.width() val pointWithinViewportY: Float = (zoomFocalPoint.y - scrollerStartViewport.top) / scrollerStartViewport.height() currentViewport.set( zoomFocalPoint.x - newWidth * pointWithinViewportX, zoomFocalPoint.y - newHeight * pointWithinViewportY, zoomFocalPoint.x + newWidth * (1 - pointWithinViewportX), zoomFocalPoint.y + newHeight * (1 - pointWithinViewportY) ) constrainViewport() needsInvalidate = true } if (needsInvalidate) { ViewCompat.postInvalidateOnAnimation(this) }
Java
// Custom object that is functionally similar to Scroller. Zoomer zoomer; private PointF zoomFocalPoint = new PointF(); ... // If a zoom is in progress—either programmatically // or through double touch—this performs the zoom. if (zoomer.computeZoom()) { float newWidth = (1f - zoomer.getCurrZoom()) * scrollerStartViewport.width(); float newHeight = (1f - zoomer.getCurrZoom()) * scrollerStartViewport.height(); float pointWithinViewportX = (zoomFocalPoint.x - scrollerStartViewport.left) / scrollerStartViewport.width(); float pointWithinViewportY = (zoomFocalPoint.y - scrollerStartViewport.top) / scrollerStartViewport.height(); currentViewport.set( zoomFocalPoint.x - newWidth * pointWithinViewportX, zoomFocalPoint.y - newHeight * pointWithinViewportY, zoomFocalPoint.x + newWidth * (1 - pointWithinViewportX), zoomFocalPoint.y + newHeight * (1 - pointWithinViewportY)); constrainViewport(); needsInvalidate = true; } if (needsInvalidate) { ViewCompat.postInvalidateOnAnimation(this); }
這是先前程式碼片段中呼叫的 computeScrollSurfaceSize()
方法。以像素為單位計算目前可捲動的表面大小。舉例來說,如果整個圖表區域都可見,這就是 mContentRect
的目前大小。如果圖表在兩個方向都放大 200%,傳回的大小在水平和垂直方向都會是原來的兩倍。
Kotlin
private fun computeScrollSurfaceSize(): Point { return Point( (contentRect.width() * (AXIS_X_MAX - AXIS_X_MIN) / currentViewport.width()).toInt(), (contentRect.height() * (AXIS_Y_MAX - AXIS_Y_MIN) / currentViewport.height()).toInt() ) }
Java
private Point computeScrollSurfaceSize() { return new Point( (int) (contentRect.width() * (AXIS_X_MAX - AXIS_X_MIN) / currentViewport.width()), (int) (contentRect.height() * (AXIS_Y_MAX - AXIS_Y_MIN) / currentViewport.height())); }
如需捲軸使用方式的其他範例,請參閱 ViewPager
類別的原始碼。這個檢視區塊會因應快速滑動動作而捲動,並使用捲動動作實作「貼齊頁面」動畫。
實作延展過度捲動效果
自 Android 12 起,EdgeEffect
新增下列 API,用於實作拉伸過度捲動效果:
getDistance()
onPullDistance()
如要提供最佳的延展過度捲動使用者體驗,請採取下列做法:
- 當使用者觸控內容時,如果延展動畫正在生效,請將觸控動作註冊為「捕捉」。使用者停止播放動畫,並再次開始操控延展效果。
- 使用者將手指移往延展的相反方向時,請放開延展部分,直到完全消失,然後開始捲動。
- 使用者在延展期間撥動時,請撥動
EdgeEffect
,以加強延展效果。
捕捉動畫
使用者擷取作用中的延展動畫時,EdgeEffect.getDistance()
會傳回 0
。這個條件表示必須透過觸控動作操控延展。在大多數容器中,系統會在 onInterceptTouchEvent()
中偵測到 catch,如下列程式碼片段所示:
Kotlin
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { ... when (action and MotionEvent.ACTION_MASK) { MotionEvent.ACTION_DOWN -> ... isBeingDragged = EdgeEffectCompat.getDistance(edgeEffectBottom) > 0f || EdgeEffectCompat.getDistance(edgeEffectTop) > 0f ... } return isBeingDragged }
Java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { ... switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: ... isBeingDragged = EdgeEffectCompat.getDistance(edgeEffectBottom) > 0 || EdgeEffectCompat.getDistance(edgeEffectTop) > 0; ... } }
在上例中,當 mIsBeingDragged
為 true
時,onInterceptTouchEvent()
會傳回 true
,因此在子項有機會使用事件之前,使用事件就已足夠。
釋放過度捲動效果
請務必在捲動前釋放延展效果,以免延展效果套用至捲動內容。下列程式碼範例會套用這項最佳做法:
Kotlin
override fun onTouchEvent(ev: MotionEvent): Boolean { val activePointerIndex = ev.actionIndex when (ev.getActionMasked()) { MotionEvent.ACTION_MOVE -> val x = ev.getX(activePointerIndex) val y = ev.getY(activePointerIndex) var deltaY = y - lastMotionY val pullDistance = deltaY / height val displacement = x / width if (deltaY < 0f && EdgeEffectCompat.getDistance(edgeEffectTop) > 0f) { deltaY -= height * EdgeEffectCompat.onPullDistance(edgeEffectTop, pullDistance, displacement); } if (deltaY > 0f && EdgeEffectCompat.getDistance(edgeEffectBottom) > 0f) { deltaY += height * EdgeEffectCompat.onPullDistance(edgeEffectBottom, -pullDistance, 1 - displacement); } ... }
Java
@Override public boolean onTouchEvent(MotionEvent ev) { final int actionMasked = ev.getActionMasked(); switch (actionMasked) { case MotionEvent.ACTION_MOVE: final float x = ev.getX(activePointerIndex); final float y = ev.getY(activePointerIndex); float deltaY = y - lastMotionY; float pullDistance = deltaY / getHeight(); float displacement = x / getWidth(); if (deltaY < 0 && EdgeEffectCompat.getDistance(edgeEffectTop) > 0) { deltaY -= getHeight() * EdgeEffectCompat.onPullDistance(edgeEffectTop, pullDistance, displacement); } if (deltaY > 0 && EdgeEffectCompat.getDistance(edgeEffectBottom) > 0) { deltaY += getHeight() * EdgeEffectCompat.onPullDistance(edgeEffectBottom, -pullDistance, 1 - displacement); } ...
使用者拖曳時,請先消耗 EdgeEffect
拉動距離,再將觸控事件傳遞至巢狀捲動容器或拖曳捲動。在上述程式碼範例中,如果系統顯示邊緣效果,且可透過動作釋放,getDistance()
會傳回正值。當觸控事件釋放延展時,會先由 EdgeEffect
消耗,以便在顯示其他效果 (例如巢狀捲動) 之前完全釋放。你可以使用 getDistance()
瞭解釋放目前效果所需的拉動距離。
與 onPull()
不同的是,onPullDistance()
會傳回已耗用的傳遞增量。從 Android 12 開始,如果 getDistance()
為 0
,且傳遞負數 deltaDistance
值給 onPull()
或 onPullDistance()
,延展效果不會改變。在 Android 11 以前的版本中,onPull()
可讓總距離的負值顯示發光效果。
停用過度捲動
您可以在版面配置檔案中選擇停用過度捲動,也可以透過程式輔助方式停用。
如要在版面配置檔案中停用這項功能,請設定 android:overScrollMode
,如以下範例所示:
<MyCustomView android:overScrollMode="never"> ... </MyCustomView>
如要透過程式停用,請使用類似下列內容的程式碼:
Kotlin
customView.overScrollMode = View.OVER_SCROLL_NEVER
Java
customView.setOverScrollMode(View.OVER_SCROLL_NEVER);
其他資源
請參閱下列相關資源: