Android:6种方式让你高效 & 正确地获取View的坐标位置。快来看看!

前言

获取 View 坐标在 Android 开发中非常常见。今天carson将详细给大家讲解 获取 View 坐标常用6种方式:

  1. getLeft()、getTop()、getRight()、getBottom()
  2. getX()、getY()、getRawX()、getRawY()
  3. getLocationOnScreen()
  4. getLocationInWindow()
  5. getGlobalVisibleRect()
  6. getLocalVisibleRect()

方式1:getLeft()、getTop()、getRight()、getBottom()

1. 应用场景

获得 View 相对 父View 的坐标

2. 使用

view.getLeft();
view.getTop();
view.getRight();
view.getBottom();

3. 具体描述

View的位置由4个顶点决定的(如下A、B、C、D)

View的顶点

4个顶点的位置描述分别由4个值决定:(请记住:View的位置是相对于父控件而言的


方式2:getX()、getY()、getRawX()、getRawY()

1. 应用场景

获得点击事件处 相对点击控件 & 屏幕的坐标

2. 使用

该方式是通过motionEvent获取的

motionEvent event;

event.getX();       
event.getY();

event.getRawX();    
event.getRawY();

3. 具体介绍


方式3:getLocationInWindow()

1. 应用场景

获取控件 相对 窗口Window 的位置

2. 具体使用

int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0]; // view距离window 左边的距离(即x轴方向)
int y = location[1]; // view距离window 顶边的距离(即y轴方向)

// 注:要在onWindowFocusChanged()里获取,即等window窗口发生变化后

3. 示意图


方式4:getLocationOnScreen()

1. 应用场景

获得 View 相对 屏幕 的绝对坐标

2. 使用

int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0]; // view距离 屏幕左边的距离(即x轴方向)
int y = location[1]; // view距离 屏幕顶边的距离(即y轴方向)

// 注:要在view.post(Runable)里获取,即等布局变化后

3. 示意图


方式5:getGlobalVisibleRect()

1. 应用场景

View可见部分 相对于 屏幕的坐标。

2. 具体使用

Rect globalRect = new Rect();
view.getGlobalVisibleRect(globalRect);

globalRect.getLeft();
globalRect.getRight();
globalRect.getTop();
globalRect.getBottom();

3. 示意图

方式6:getLocalVisibleRect()

1. 应用场景

View可见部分 相对于 自身View位置左上角的坐标。

2. 具体使用

Rect localRect = new Rect();
view.getLocalVisibleRect(localRect);

localRect.getLeft();
localRect.getRight();
localRect.getTop();
localRect.getBottom();

3. 示意图


总结

本文对Android获取View坐标位置的方式进行了全面讲解,总结如下:

漫漫开发之路,我们只是其中的一小部分……只有不断的学习、进阶,才是我们的出路!才跟得上时代的进步!

今年年初我花一个月的时间收录整理了一套知识体系,如果有想法深入的系统化的去学习的,可以私信我【学习】,我会把我收录整理的资料都送给大家,帮助大家更快的进阶。

发布了289 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45365889/article/details/102677679