Skip to content

Commit

Permalink
accept merge request #195
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Sep 4, 2017
1 parent d3e7f12 commit c62cedf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,21 @@
import android.view.View;
import android.view.ViewTreeObserver;


/**
* Created by z-chu on 2017/9/4
* 用于监听导航栏的显示和隐藏,主要用于适配华为EMUI系统上虚拟导航栏可随时收起和展开的情况
*/

public class NavigationBarChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {


/**
* 监听竖屏模式导航栏的显示和隐藏
*/
public static final int ORIENTATION_VERTICAL = 1;

/**
* 监听横屏模式导航栏的显示和隐藏
*/
public static final int ORIENTATION_HORIZONTAL = 2;
public static final int ORIENTATION_VERTICAL = 1; //监听竖屏模式导航栏的显示和隐藏
public static final int ORIENTATION_HORIZONTAL = 2; //监听横屏模式导航栏的显示和隐藏

private Rect rect;

private View rootView;

private boolean isShowNavigationBar = false;

private int orientation;

private OnSoftInputStateChangeListener listener;


public NavigationBarChangeListener(View rootView, int orientation) {
this.rootView = rootView;
this.orientation = orientation;
Expand All @@ -51,15 +36,13 @@ public void onGlobalLayout() {
} else if (orientation == ORIENTATION_HORIZONTAL) {
heightDiff = rootView.getWidth() - (rect.right - rect.left);
}
int navigationBarHeight = Utils.hasVirtualNavigationBar(rootView.getContext()) ?
Utils.getNavigationBarHeight(rootView.getContext()) : 0;
int navigationBarHeight = Utils.hasVirtualNavigationBar(rootView.getContext()) ? Utils.getNavigationBarHeight(rootView.getContext()) : 0;
if (heightDiff >= navigationBarHeight && heightDiff < navigationBarHeight * 2) {
if (!isShowNavigationBar && listener != null) {
listener.onNavigationBarShow(orientation, heightDiff);
}
isShowNavigationBar = true;
} else {

if (isShowNavigationBar && listener != null) {
listener.onNavigationBarHide(orientation);
}
Expand All @@ -86,9 +69,9 @@ public static NavigationBarChangeListener with(Activity activity) {
}

public static NavigationBarChangeListener with(View rootView, int orientation) {
NavigationBarChangeListener softInputHeightListener = new NavigationBarChangeListener(rootView, orientation);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(softInputHeightListener);
return softInputHeightListener;
NavigationBarChangeListener changeListener = new NavigationBarChangeListener(rootView, orientation);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(changeListener);
return changeListener;
}

public static NavigationBarChangeListener with(Activity activity, int orientation) {
Expand Down
9 changes: 2 additions & 7 deletions imagepicker/src/main/java/com/lzy/imagepicker/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ public static DisplayMetrics getScreenPix(Activity activity) {

/** dp转px */
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
}

/**
* 判断手机是否含有虚拟按键 99%
*
*/
public static boolean hasVirtualNavigationBar(Context context) {
boolean hasSoftwareKeys = true;
Expand Down Expand Up @@ -106,9 +104,6 @@ public static boolean hasVirtualNavigationBar(Context context) {
*/
public static int getNavigationBarHeight(Context context) {
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
return resourceId > 0 ?
context.getResources().getDimensionPixelSize(resourceId) :
0;
return resourceId > 0 ? context.getResources().getDimensionPixelSize(resourceId) : 0;
}

}

0 comments on commit c62cedf

Please sign in to comment.