スクロール操作のアニメーション化

Compose を試す
Jetpack Compose は Android で推奨される UI ツールキットです。Compose でタップと入力を使用する方法について説明します。

Android では通常、スクロールを実現するには ScrollView クラスを使用します。コンテナの境界を超えて拡張される可能性がある標準レイアウトを ScrollView にネストして、フレームワークで管理されるスクロール可能なビューを提供します。カスタム スクローラーの実装は、特別なシナリオの場合にのみ必要です。このドキュメントでは、スクローラーを使用したタッチ操作に対するスクロール効果を表示する方法について説明します。

アプリは、スクローラー(Scroller または OverScroller)を使用して、タッチイベントへの応答としてスクロール アニメーションを生成するために必要なデータを収集できます。これらはよく似ていますが、OverScroller には、パン操作やフリング操作の後にユーザーがコンテンツの端に到達したことを示すメソッドも含まれています。

  • Android 12(API レベル 31)以降では、ドラッグ イベントでは視覚要素が引き伸ばされて戻り、フリング イベントではフリングとバウンドが返ります。
  • Android 11(API レベル 30)以前では、エッジにドラッグまたはフリング ジェスチャーを行った後、境界が「グロー」効果を表示します。

このドキュメントの InteractiveChart サンプルでは、EdgeEffect クラスを使用して、これらのオーバースクロール効果を表示します。

スクローラーを使用すると、摩擦や速度などのプラットフォーム標準のスクロール物理学を使用して、時間の経過とともにスクロールをアニメーション化できます。スクローラー自体は何も描画しません。スクローラーは、スクロール オフセットを時間の経過とともに追跡しますが、その位置をビューに自動的に適用することはありません。スクロール アニメーションが滑らかに見える速度で新しい座標を取得して適用する必要があります。

スクロールの用語について

スクロールという単語は、文脈に応じて Android では異なる意味を持つことがあります。

スクロールは、ビューポート、つまり見ているコンテンツの「ウィンドウ」を移動する一般的なプロセスです。x 軸と y 軸の両方でスクロールする場合は、パンといいます。このドキュメントの InteractiveChart サンプルアプリでは、スクロール、ドラッグ、フリングの 2 種類の方法を説明しています。

  • ドラッグ: ユーザーがタッチスクリーン上で指をドラッグしたときに発生するスクロールの一種です。ドラッグを実装するには、GestureDetector.OnGestureListeneronScroll() をオーバーライドします。 ドラッグについて詳しくは、ドラッグして拡大縮小するをご覧ください。
  • フリング: ユーザーが指をすばやくドラッグして離したときに発生するスクロールです。ユーザーが指を離した後、通常はビューポートを移動し続けますが、ビューポートの移動が停止するまで減速します。フリングを実装するには、GestureDetector.OnGestureListeneronFling() をオーバーライドし、スクローラー オブジェクトを使用します。
  • パン: x 軸と y 軸の両方に沿って同時にスクロールすることをパンといいます。

スクローラー オブジェクトはフリング操作と組み合わせて使用するのが一般的ですが、タップイベントに応じて UI にスクロールを表示するあらゆるコンテキストで使用できます。たとえば、onTouchEvent() をオーバーライドしてタッチイベントを直接処理し、そのタッチイベントに応じてスクロール効果や「ページにスナップ」アニメーションを生成できます。

組み込みのスクロール実装を含むコンポーネント

次の Android コンポーネントには、スクロール動作とオーバースクロール動作の組み込みサポートが含まれています。

アプリで別のコンポーネント内でのスクロールとオーバースクロールをサポートする必要がある場合は、次の手順を実施します。

  1. タップベースのスクロールのカスタム実装を作成します
  2. 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() がトリガーされ、xy の値が更新されます。これは通常、前の例に示すように、ビューの子がスクローラー オブジェクトを使用してスクロールをアニメーション化しているときに行われます。

ほとんどのビューは、スクローラー オブジェクトの x 位置と y 位置を直接 scrollTo() に渡します。次の computeScroll() の実装では、別のアプローチを採用しています。computeScrollOffset() を呼び出して、xy の現在位置を取得します。オーバースクロールの「グロー」エッジ効果を表示する条件が満たされる場合(つまり、ディスプレイがズームインされ、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% ズームすると、返されるサイズは水平方向と垂直方向の 2 倍になります。

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()

ストレッチ オーバースクロールで最適なユーザー エクスペリエンスを提供するには、次の手順を行います。

  1. ユーザーがコンテンツに触れたときにストレッチ アニメーションが有効な状態で、そのタップを「キャッチ」として登録します。ユーザーがアニメーションを停止し、ストレッチの操作を再開します。
  2. ユーザーがストレッチの反対方向に指を動かしたら、ストレッチが完全に終わるまで離し、スクロールを開始します。
  3. ストレッチ中にユーザーがフリングするときに EdgeEffect をフリングすると、ストレッチ効果が強化されます。

アニメーションをキャッチする

ユーザーがアクティブなストレッチ アニメーションをキャッチすると、EdgeEffect.getDistance()0 を返します。この条件は、ストレッチがタップ モーションによって操作される必要があることを示します。ほとんどのコンテナでは、次のコード スニペットに示すように、キャッチは onInterceptTouchEvent() で検出されます。

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;
      ...
  }
}

上記の例では、mIsBeingDraggedtrue の場合、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 の pull 距離を消費してから、ネストされたスクロール コンテナにタッチイベントを渡すか、スクロールをドラッグします。上記のコードサンプルでは、エッジ効果が表示されていて、モーションによって解放できる場合、getDistance() は正の値を返しています。タッチイベントがストレッチを解放すると、まず EdgeEffect によって消費されるため、ネスト スクロールなどの他の効果が表示される前に、完全に解放されます。getDistance() を使用すると、現在の効果を解除するために必要な pull 距離を確認できます。

onPull() とは異なり、onPullDistance() は渡されたデルタの消費額を返します。Android 12 以降では、getDistance()0 のときに onPull() または onPullDistance() に負の deltaDistance 値が渡されても、ストレッチ効果は変更されません。Android 11 以前では、onPull() を使用すると、合計距離を負の値にするとグロー効果を表示できます。

オーバースクロールを無効にする

オーバースクロールは、レイアウト ファイル内で、またはプログラムでオプトアウトできます。

レイアウト ファイルでオプトアウトするには、次の例のように android:overScrollMode を設定します。

<MyCustomView android:overScrollMode="never">
    ...
</MyCustomView>

プログラムでオプトアウトするには、次のようなコードを使用します。

Kotlin

customView.overScrollMode = View.OVER_SCROLL_NEVER

Java

customView.setOverScrollMode(View.OVER_SCROLL_NEVER);

参考情報

以下の関連リソースもご覧ください。