Skip to content

Commit 8d0300e

Browse files
feat: animations
1 parent 918adad commit 8d0300e

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
package com.foxdebug.system;
22

33
import android.app.Activity;
4-
import android.graphics.Rect;
54
import android.view.View;
65

6+
import androidx.core.graphics.Insets;
7+
import androidx.core.view.ViewCompat;
8+
import androidx.core.view.WindowInsetsCompat;
9+
import androidx.core.view.WindowInsetsAnimationCompat;
10+
711
public class SoftInputAssist {
8-
private final View rootView;
9-
private final View contentView;
10-
private int baseline = -1;
1112

1213
public SoftInputAssist(Activity activity) {
13-
rootView = activity.getWindow().getDecorView();
14-
contentView = activity.findViewById(android.R.id.content);
14+
View contentView = activity.findViewById(android.R.id.content);
1515

16-
rootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
17-
Rect r = new Rect();
18-
rootView.getWindowVisibleDisplayFrame(r);
16+
ViewCompat.setWindowInsetsAnimationCallback(
17+
contentView,
18+
new WindowInsetsAnimationCompat.Callback(
19+
WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE
20+
) {
21+
@Override
22+
public WindowInsetsCompat onProgress(
23+
WindowInsetsCompat insets,
24+
java.util.List<WindowInsetsAnimationCompat> runningAnimations) {
1925

20-
int heightDiff = rootView.getHeight() - (r.bottom - r.top);
26+
Insets ime = insets.getInsets(WindowInsetsCompat.Type.ime());
27+
Insets nav = insets.getInsets(WindowInsetsCompat.Type.systemBars());
2128

22-
// Save baseline (system bars only)
23-
if (baseline == -1 || heightDiff < baseline) {
24-
baseline = heightDiff;
25-
}
29+
int keyboardHeight = Math.max(0, ime.bottom - nav.bottom);
2630

27-
int keyboardHeight = heightDiff - baseline;
31+
contentView.setPadding(0, 0, 0, keyboardHeight);
2832

29-
if (keyboardHeight > 0) {
30-
contentView.setPadding(0, 0, 0, keyboardHeight);
31-
} else {
32-
contentView.setPadding(0, 0, 0, 0);
33+
return insets;
34+
}
3335
}
34-
});
36+
);
3537
}
3638
}

0 commit comments

Comments
 (0)