PopupWindow彈出動態計算展示的位置,左、右、上、下對齊

PopupWindow彈出動態計算展示的位置,左、右、上、下對齊

  • 附上完整代碼

public class PopupWindowUtil {
    /**
     * 計算出來的位置,y方向就在anchorView的上面和下面對齊顯示,x方向就是與屏幕右邊對齊顯示
     * 如果anchorView的位置有變化,就可以適當自己額外加入偏移來修正
     *
     * @param anchorView  呼出window的view
     * @param contentView window的內容佈局
     * @return window顯示的左上角的xOff, yOff座標
     */
    public static int[] calculatePopWindowPos(final View anchorView, final View contentView) {
        final int windowPos[] = new int[2];
        final int anchorLoc[] = new int[2];
        // 獲取錨點View在屏幕上的左上角座標位置
        anchorView.getLocationOnScreen(anchorLoc);
        final int anchorHeight = anchorView.getHeight();
        final int anchorPaddingTop = anchorView.getPaddingTop();
        // 獲取屏幕的高寬
        final int screenHeight = UIUtil.getScreenHeight();
        final int screenWidth = UIUtil.getScreenWidth();
        // 測量contentView
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 計算contentView的高寬
        final int windowHeight = contentView.getMeasuredHeight();
        final int windowWidth = contentView.getMeasuredWidth();
        // 判斷需要向上彈出還是向下彈出顯示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth;//向下
            windowPos[1] = anchorLoc[1] - windowHeight + anchorHeight - anchorPaddingTop;
        } else {
            windowPos[0] = screenWidth - windowWidth;//向上
            windowPos[1] = anchorLoc[1] + anchorHeight - anchorHeight + anchorPaddingTop;
        }
        return windowPos;
    }

    /**
     * pop自動調整位置顯示在左右兩側
     *
     * @param anchorView  呼出window的view
     * @param contentView window的內容佈局
     * @return window顯示的左上角的xOff, yOff座標
     */
    public static int[] calculatePopWindow(final View anchorView, final View contentView) {
        final int windowPos[] = new int[2];
        final int anchorLoc[] = new int[2];
        // 獲取錨點View在屏幕上的左上角座標位置
        anchorView.getLocationOnScreen(anchorLoc);
        final int anchorHeight = anchorView.getHeight();
        final int anchorWidth = anchorView.getWidth();
        final int anchorPaddingTop = anchorView.getPaddingTop();
        final int anchorPaddingLeft = anchorView.getPaddingLeft();
        // 獲取屏幕的高寬
        final int screenHeight = UIUtil.getScreenHeight();
        final int screenWidth = UIUtil.getScreenWidth();
        // 測量contentView
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 計算contentView的高寬
        final int windowHeight = contentView.getMeasuredHeight();
        final int windowWidth = contentView.getMeasuredWidth();
        // 判斷需要向上彈出還是向下彈出顯示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] - windowHeight + anchorHeight - anchorPaddingTop;
        } else {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] + anchorHeight - anchorHeight + anchorPaddingTop;
        }
        boolean isRight = (screenWidth - anchorLoc[0] - anchorWidth > windowWidth);
        int xOff = UIUtil.dip2px(20);//偏移量
        if (isRight) {
            windowPos[0] = anchorLoc[0] + anchorWidth + anchorPaddingLeft + xOff;//右
        } else {
            windowPos[0] = anchorLoc[0] - windowWidth - anchorPaddingLeft - xOff;//左
        }
        return windowPos;
    }
}

初始化PopupWindow

int windowPos[] = PopupWindowUtil.calculatePopWindowPos(iv, view);
int xOff = UIUtil.dip2px(50);
windowPos[0] -= xOff;
popWindow.showAtLocation(iv, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章