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]); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值