[英]Android soft keyboard - get the exact length of the animation?
現在我們終於可以很容易地知道 Android 軟鍵盤是什么時候上下動畫了:
yourView.setWindowInsetsAnimationCallback(new WindowInsetsAnimation.Callback(WindowInsetsAnimation.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE ) {
@NonNull
@Override
public WindowInsets onProgress(@NonNull WindowInsets windowInsets, @NonNull List<WindowInsetsAnimation> list) {
return windowInsets;
}
@Override
public void onEnd(@NonNull WindowInsetsAnimation animation) {
super.onEnd(animation);
}
@NonNull
@Override
public WindowInsetsAnimation.Bounds onStart(@NonNull WindowInsetsAnimation animation, @NonNull WindowInsetsAnimation.Bounds bounds) {
boolean showingKeyboard = frame.getRootWindowInsets().isVisible(WindowInsets.Type.ime());
if (showingKeyboard) {
yourLayoutWhenSoftKeyboardShown();
}
else {
yourLayoutWhenSoftKeyboardHidden();
}
return super.onStart(animation, bounds);
}
});
然后,您可以輕松地制作所需的任何類型的動畫,而操作系統正在為窗口的大小設置動畫。 例子,
Integer animDuration = 120;
private void layoutWhenSoftKeyboardShown() {
ValueAnimator m1 = ValueAnimator.ofFloat(15f, 1f);
m1.setDuration(animDuration);
m1.setInterpolator(new LinearInterpolator());
m1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
((LinearLayout.LayoutParams) spacerOrWhatever.getLayoutParams()).weight =
(float) animation.getAnimatedValue();
spacerOrWhatever.requestLayout();
}
});
m1.start();
}
這一切都很棒,但是......
在 iOS 中,相當於“操作系統正在為鍵盤外觀設置動畫”的等效調用,它為您提供了一個參數,即動畫將持續多長時間。
然后,您只需將自己的動畫設置為該值。
在 Android 中,如何獲取上面onStart
中即將發生的動畫的長度?
啊,想通了:
WindowInsetsAnimation.Callback
包括getAnimationDuration()
。
更好的是,因為 droid 勝過 ios,你實際上可以得到插值器! getInterpolator()
。
這意味着您可以將持續時間甚至插播傳遞給您自己的動畫,哇!
@NonNull
@Override
public WindowInsetsAnimation.Bounds onStart(@NonNull WindowInsetsAnimation animation, @NonNull WindowInsetsAnimation.Bounds bounds) {
boolean showingKeyboard = frame.getRootWindowInsets().isVisible(WindowInsets.Type.ime());
if (showingKeyboard) {
layoutWhenSoftKeyboardShown(
animation.getDurationMillis(), animation.getInterpolator());
}
else {
layoutWhenSoftKeyboardHidden(
animation.getDurationMillis(), animation.getInterpolator());
}
return super.onStart(animation, bounds);
}
接着 ...
private void layoutWhenSoftKeyboardShown(long animDuration, Interpolator animIpr) {
Log.d("yoyo", "SHOWING");
ValueAnimator m1 = ValueAnimator.ofFloat(15f, 3f);
m1.setDuration(animDuration);
m1.setInterpolator(animIpr);
m1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
((LinearLayout.LayoutParams) whatever.getLayoutParams()).weight = (float) animation.getAnimatedValue();
whatever.requestLayout();
}
});
m1.start();
}
美麗的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.