View在屏幕中的位置

主要就是几个方法:

  1. getLocalVisibleRect(rect); // view自身的宽度和高度
  2. getGlobalVisibleRect(rect)//view在整个屏幕的坐标,也包含状态栏高度
  3. getGlobalVisibleRect(rect, offset)//view在整个屏幕的坐标及偏移坐坐标
  4. getLocationInWindow(location);//view在整个窗口的左上顶点坐标
  5. getLocationOnScreen(location);//view在整个屏幕的左上顶点坐标
  6. getWindowVisibleDisplayFrame(rect);//view在整个屏幕的坐标,也含有通知栏高度
    rect = Rect(0, 75 - 1080, 1812) 状态栏高度 75px 屏幕分辨率:1080x1812 有的手机底部导航占据一定高度

实例代码:

    holder.img_movie_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.e("setOnClickListener", "  Width  = " + v.getWidth() + " height = " + v.getHeight());

                Rect rect = new Rect();
                v.getLocalVisibleRect(rect); // view自身的宽度和高度
                LogUtil.e("setOnClickListener", "  getLocalVisibleRect  rect = " + rect);

                Point offset = new Point();
                v.getGlobalVisibleRect(rect, offset);// 取view在整个屏幕的的坐标,包括状态栏高度 left top right bottom
                LogUtil.e("setOnClickListener", "  getGlobalVisibleRect rect = " + rect + " offset = " + offset);
                rect.offset(-offset.x, -offset.y);// 通过offset转化为getLocalVisibleRect坐标
                LogUtil.e("setOnClickListener", "  getGlobalVisibleRect offset rect = " + rect + " offset = " + offset);

                int[] location = new int[2];
                v.getLocationInWindow(location);//view在整个窗口的左上顶点坐标
                LogUtil.e("setOnClickListener", "  getLocationInWindow x = " + location[0] + " y = " + location[1]);

                v.getLocationOnScreen(location);//view在整个屏幕的左上顶点坐标
                LogUtil.e("setOnClickListener", "  getLocationOnScreen x = " + location[0] + " y = " + location[1]);

                v.getWindowVisibleDisplayFrame(rect);//view在整个屏幕的坐标,也含有通知栏高度
                LogUtil.e("setOnClickListener", "  getWindowVisibleDisplayFrame rect = " + rect);

                v.getGlobalVisibleRect(rect);// view在整个屏幕的坐标,包括状态栏高度
                // left top right bottom
                LogUtil.e("setOnClickListener", "  getGlobalVisibleRect rect = " + rect);        
            }
        });

运行结果:

E/setOnClickListener:   Width  = 312 height = 420
E/setOnClickListener:   getLocalVisibleRect  rect = Rect(0, 0 - 312, 420)
E/setOnClickListener:   getGlobalVisibleRect rect = Rect(720, 363 - 1032, 783) offset = Point(720, 363)
E/setOnClickListener:   getGlobalVisibleRect offset rect = Rect(0, 0 - 312, 420) offset = Point(720, 363)
E/setOnClickListener:   getLocationInWindow x = 720 y = 363
E/setOnClickListener:   getLocationOnScreen x = 720 y = 363
E/setOnClickListener:   getWindowVisibleDisplayFrame rect = Rect(0, 75 - 1080, 1812)
E/setOnClickListener:   getGlobalVisibleRect rect = Rect(720, 363 - 1032, 783)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值