private View activityRootView;
//屏幕高度
private int screenHeight = 0;
//软件盘弹起后所占高度阀值
private int keyHeight = 0;
@Override
protected void onDestroy() {
super.onDestroy();
// rootView.getViewTreeObserver().removeGlobalOnLayoutListener(layoutListener);
}
/**
* 添加软键盘状态监听器
*/
protected void setKeyboardStateListener() {
rootView = getWindow().getDecorView().findViewById(android.R.id.content);
// rootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
//获取屏幕高度
screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
//阀值设置为屏幕高度的1/3
keyHeight = screenHeight/3;
spring.setCurrentValue(0);
spring.addListener(new SimpleSpringListener() {
@Override
public void onSpringUpdate(Spring spring) {
// You can observe the updates in the spring
// state by asking its current value in onSpringUpdate.
float value = (float) spring.getCurrentValue();
float scale = 1f - (value * 0.5f);
Log.i("scale", scale + "");
iv_icon.setScaleX(scale);
iv_icon.setScaleY(scale);
}
});
rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){
Toast.makeText(Login_withTitleTC.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();
spring.setEndValue(0.5);
ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 130.0f);
animator.setDuration(200);
animator.start();
//按钮上移
ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", -30.0f);
animator1.setDuration(200);
animator1.start();
}else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
spring.setEndValue(0);
ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", -0f);
animator.setDuration(200);
animator.start();
//按钮下移
ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", 0f);
animator1.setDuration(200);
animator1.start();
Toast.makeText(Login_withTitleTC.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();
}
}
});
}
private ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rootView.postDelayed(new Runnable() {
@Override
public void run() {
if (isKeyboardShown(rootView)) {
// ToastUtil.showToast(Login_withTitleTC.this, "键盘弹出");
spring.setCurrentValue(0);
spring.addListener(new SimpleSpringListener() {
@Override
public void onSpringUpdate(Spring spring) {
// You can observe the updates in the spring
// state by asking its current value in onSpringUpdate.
float value = (float) spring.getCurrentValue();
float scale = 1f - (value * 0.5f);
Log.i("scale", scale + "");
iv_icon.setScaleX(scale);
iv_icon.setScaleY(scale);
}
});
spring.setEndValue(0.5);
ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 130.0f);
animator.setDuration(200);
animator.start();
//按钮上移
ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", -30.0f);
animator1.setDuration(200);
animator1.start();
} else {
// ToastUtil.showToast(Login_withTitleTC.this, "键盘隐藏");
spring.setEndValue(0);
// int childCount = constraintLayout.getChildCount();
// for (int i = 0; i < childCount; i++) {
// final View view = constraintLayout.getChildAt(i);
//
// springChain.addSpring(new SimpleSpringListener() {
// @Override
// public void onSpringUpdate(Spring spring) {
// view.setTranslationY((float) spring.getCurrentValue());
// }
// });
// }
//
// List<Spring> springs = springChain.getAllSprings();
// for (int i = 0; i < springs.size(); i++) {
// springs.get(i).setCurrentValue(1080);
// }
//
// springChain.setControlSpringIndex(0).getControlSpring().setEndValue(0);
ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 0f);
animator.setDuration(200);
animator.start();
//按钮下移
ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", 0f);
animator1.setDuration(200);
animator1.start();
}
}
}, 100);
}
};
/**
* 判断软键盘是否打开
*
* @param rootView 最上层布局
* @return 打开:true,隐藏:false
*/
private boolean isKeyboardShown(View rootView) {
//获取当前屏幕内容的高度
int screenHeight = getWindow().getDecorView().getHeight();
//获取View可见区域的bottom
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
return screenHeight - rect.bottom != 0;
}
监听键盘弹出的方法总结(两个)
最新推荐文章于 2024-06-30 15:55:53 发布