控制软件键盘并为其添加动画效果

借助 WindowInsetsCompat,应用可以查询和控制屏幕键盘(也称为 IME),具体方式与其与系统栏的交互方式类似。在软件键盘打开或关闭时,应用还可以使用 WindowInsetsAnimationCompat 创建无缝转换。

图 1.软件键盘开放式/闭合过渡的两个示例。

前提条件

在为软件键盘设置控件和动画之前,请将应用配置为无边框显示。这样一来,它可以处理系统窗口边衬区,例如系统栏和屏幕键盘。

检查键盘软件可见性

使用 WindowInsets 检查软件键盘的可见性。

Kotlin

val insets = ViewCompat.getRootWindowInsets(view) ?: return
val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom

Java

WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(view);
boolean imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime());
int imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;

或者,您也可以使用 ViewCompat.setOnApplyWindowInsetsListener 观察软件键盘可见性的变化。

Kotlin

ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets ->
  val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
  val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom
  insets
}

Java

ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
  boolean imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime());
  int imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
  return insets;
});

使用软件键盘同步动画

用户点按文本输入字段会导致键盘从屏幕底部滑动到位,如以下示例所示:

图 2.同步键盘动画。
  • 图 2 中标签为“Unsynchronousd”的示例显示了 Android 10(API 级别 29)中的默认行为,即应用的文本字段和内容会卡入到位,而不是与键盘的动画同步,这种行为在视觉上可能非常令人反感。

  • 在 Android 11(API 级别 30)及更高版本中,您可以使用 WindowInsetsAnimationCompat 使应用的转换与键盘从屏幕底部上下滑动同步。这样看起来比较流畅,如图 2 中标记为“已同步”的示例所示。

使用要与键盘动画同步的视图配置 WindowInsetsAnimationCompat.Callback

Kotlin

ViewCompat.setWindowInsetsAnimationCallback(
  view,
  object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_STOP) {
    // Override methods.
  }
)

Java

ViewCompat.setWindowInsetsAnimationCallback(
    view,
    new WindowInsetsAnimationCompat.Callback(
        WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_STOP
    ) {
      // Override methods.
    });

WindowInsetsAnimationCompat.Callback 中有多个可替换的方法,即 onPrepare()onStart()onProgress()onEnd()。首先,在任何布局更改之前调用 onPrepare()

当边衬区动画开始播放时,以及在视图因动画而重新布局之前,系统会调用 onPrepare。您可以使用它保存起始状态,在本例中为视图的底部坐标。

显示根视图的起始状态底部坐标的图片。
图 3. 使用 onPrepare() 记录启动状态。

以下代码段展示了对 onPrepare 的调用示例:

Kotlin

var startBottom = 0f

override fun onPrepare(
  animation: WindowInsetsAnimationCompat
) {
  startBottom = view.bottom.toFloat()
}

Java

float startBottom;

@Override
public void onPrepare(
    @NonNull WindowInsetsAnimationCompat animation
) {
  startBottom = view.getBottom();
}

当边衬区动画启动时,系统会调用 onStart。您可以使用它将所有视图属性设置为布局更改的最终状态。如果您将 OnApplyWindowInsetsListener 回调设置为任何视图,则此时已对该回调进行调用。这时有必要保存视图属性的最终状态。

显示结束状态视图底部坐标的图片
图 4. 使用 onStart() 记录结束状态。

以下代码段展示了对 onStart 的调用示例:

Kotlin

var endBottom = 0f

override fun onStart(
  animation: WindowInsetsAnimationCompat,
  bounds: WindowInsetsAnimationCompat.BoundsCompat
): WindowInsetsAnimationCompat.BoundsCompat {
  // Record the position of the view after the IME transition.
  endBottom = view.bottom.toFloat()

  return bounds
}

Java

float endBottom;

@NonNull
@Override
public WindowInsetsAnimationCompat.BoundsCompat onStart(
    @NonNull WindowInsetsAnimationCompat animation,
    @NonNull WindowInsetsAnimationCompat.BoundsCompat bounds
) {
  endBottom = view.getBottom();
  return bounds;
}

当边衬区在运行动画的过程中发生更改时,系统会调用 onProgress,以便您在键盘动画播放期间替换它并在每一帧上收到通知。更新视图属性,使视图动画与键盘同步。

此时,所有布局更改均已完成。例如,如果您使用 View.translationY 来移动视图,则每次调用此方法时该值会逐渐减小,并最终达到 0 到原始布局位置。

图 5.使用 onProgress() 同步动画。

以下代码段展示了对 onProgress 的调用示例:

Kotlin

override fun onProgress(
  insets: WindowInsetsCompat,
  runningAnimations: MutableList<WindowInsetsAnimationCompat>
): WindowInsetsCompat {
  // Find an IME animation.
  val imeAnimation = runningAnimations.find {
    it.typeMask and WindowInsetsCompat.Type.ime() != 0
  } ?: return insets

  // Offset the view based on the interpolated fraction of the IME animation.
  view.translationY =
    (startBottom - endBottom) * (1 - imeAnimation.interpolatedFraction)

  return insets
}

Java

@NonNull
@Override
public WindowInsetsCompat onProgress(
    @NonNull WindowInsetsCompat insets,
    @NonNull List<WindowInsetsAnimationCompat> runningAnimations
) {
  // Find an IME animation.
  WindowInsetsAnimationCompat imeAnimation = null;
  for (WindowInsetsAnimationCompat animation : runningAnimations) {
    if ((animation.getTypeMask() & WindowInsetsCompat.Type.ime()) != 0) {
      imeAnimation = animation;
      break;
    }
  }
  if (imeAnimation != null) {
    // Offset the view based on the interpolated fraction of the IME animation.
    view.setTranslationY((startBottom - endBottom)

        *   (1 - imeAnimation.getInterpolatedFraction()));
  }
  return insets;
}

您可以选择替换 onEnd。系统会在动画结束后调用此方法。这时有必要清理任何临时更改。

其他资源