Get the height of the soft keyboard and generate 9-patch

Recently, we are doing a chat-like function, so we need to get the height of the soft keyboard to control the display of the interface. At the same time, some mobile phones have virtual buttons on them. You need to pay attention to the interface layout. The following code is a way to get the height of the mobile phone's soft keyboard.

    Rect r = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    //屏幕的高度,不包含虚拟按键
    int screenHeight = metrics.heightPixels;
    //屏幕真实的高度,包含虚拟按键
    getWindowManager().getDefaultDisplay().getRealMetrics(metrics); 
    //虚拟按键的高度
    int virtualHight = metrics.heightPixels - screenHeight;
    int statusBarHeight=getStatusBarHeight(context);
    // 在不显示软键盘时,heightDiff等于状态栏的高度
    // 在显示软键盘时,heightDiff会变大,等于软键盘加状态栏的高度。
    // 所以heightDiff大于状态栏高度时表示软键盘出现了,
    // 这时可算出软键盘的高度,即heightDiff减去状态栏的高度
    if (heightDiff > statusBarHeight) {
        keyboardHeight = heightDiff - statusBarHeight;
    }

    if (isShowKeyboard) {
        // 如果软键盘是弹出的状态,并且heightDiff小于等于状态栏高度,
        if (heightDiff <= statusBarHeight) {
            // 说明这时软键盘已经收起
            isShowKeyboard=false;
        }
    } else {
        // 如果软键盘是收起的状态,并且heightDiff大于状态栏高度,
        if (heightDiff > statusBarHeight) {
         // 说明这时软键盘已经弹出
         isShowKeyboard=true;
        }
    }

There is a ViewTreeObserver class that can be used to monitor changes in view state, including changes in focus, start drawing, touch events, etc. If you want to control the height of a certain control, you can use this class to achieve, as shown below:

    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
             /**
             * Callback method to be invoked when the global layout state or the visibility of views
             * within the view tree changes
             */
            @Override
            public void onGlobalLayout() {
                //这里放你要实现的效果。
            }
        });

Here is how to get the height of the mobile phone status bar

    // 获取手机状态栏高度
    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x=Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

For the chat box, I first thought about the way to customize the control. Later, I found 9-patch on the Internet , which can automatically stretch the picture. What you need is a .png picture. You can find an artist to cut the picture. In Android studio Right-click and select the picture to automatically generate a xx.9.png file, as shown in the figure below:
xx.9.png

In the \sdk\tools\below draw9patch command, if you configure a path, you can directly from the command line using draw9patch open. As for how to configure the path, I won't go into details here. After opening, import the picture to generate 9-Patch , as shown below:
draw9patch

The following is the final result. It should be noted that the generated xx.9.png file needs to be saved in the drawable directory, and then it can be called directly.
On the tool, you can drag directly if there is a sideline, try a few more times to find the most suitable one. As shown in the figure, the black shadow is the margin of the area to be displayed, and the intersection of the two green areas will be stretched at the end Place, on the right is the final stretched rendering.
effect

Guess you like

Origin blog.csdn.net/Ser_Bad/article/details/52302716