package com.iflyrec.sdk.lib_app.jzapp.utils.sys;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
/**
* Created by on 21-3-23.
*/
public class SoftKeyBoardListener {
private View rootView;//activity 的根式图
private int rootViewVisibleHeight;//记录根式图的显示高度
private OnSoftkeyBoardChangeListener onSoftkeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity){
//huoqu activity 的根式图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根式图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
if (rootViewVisibleHeight == 0){
rootViewVisibleHeight = visibleHeight;
return;
}
//根式图显示高度没有变化,可以看做软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight){
return;
}
//根视图显示高度变小超过200,可以看做软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200){
if (onSoftkeyBoardChangeListener != null){
onSoftkeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度变大超过200,可以看做软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200){
if (onSoftkeyBoardChangeListener != null){
onSoftkeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
}
}
});
}
private void setOnSoftkeyBoardChangeListener(OnSoftkeyBoardChangeListener onSoftkeyBoardChangeListener){
this.onSoftkeyBoardChangeListener = onSoftkeyBoardChangeListener;
}
public interface OnSoftkeyBoardChangeListener{
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public void setListener(Activity activity,OnSoftkeyBoardChangeListener onSoftkeyBoardChangeListener){
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftkeyBoardChangeListener(onSoftkeyBoardChangeListener);
}
}
private void initLoginAnim(){
new SoftKeyBoardListener(this).setListener(this, new SoftKeyBoardListener.OnSoftkeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
hideLogoLayout(ll_anim,true);
}
@Override
public void keyBoardHide(int height) {
hideLogoLayout(ll_anim,false);
}
});
}
// int height;
private void hideLogoLayout(View animView ,boolean hide){
if (animView == null) return;
ObjectAnimator a1;
if (hide)
a1 = ObjectAnimator.ofFloat(animView,"alpha",1.0f,0f);
else
a1 = ObjectAnimator.ofFloat(animView,"alpha",0f,1.0f);
final ViewGroup.LayoutParams lp = animView.getLayoutParams();
// if (lp.height != 0) {
// height = lp.height;
// }
a1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float cVal = (float)valueAnimator.getAnimatedValue();
lp.height = (int)(DeviceUtil.dip2px(UserLoginActivity.this,105)*cVal);
animView.setLayoutParams(lp);
}
});
a1.start();
}