android 监听键盘弹起和隐藏,动态更改布局高度

4 篇文章 0 订阅
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();
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值