zoukankan      html  css  js  c++  java
  • Android View各类获取坐标的方式

    Android View获取坐标的方式:

    1. 获取View相对于父View的坐标:View view.getLeft()、view.getTop()、view.getRight()、view.getBottom()

    2.获取点击事件的点击位置相对于其点击控件的坐标,以及相对于屏幕的坐标。 motionEvent event.getX()、event.getY()、event.getRawX()、event.getRawY()

    3. 获取控件 相对 窗口Window 的位置:getLocationInWindow(),获取不到的时候可能因为要在onWindowFocusChanged()里获取,即等window窗口发生变化后才可以

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

    4.获得 View 相对 屏幕 的绝对坐标:getLocationOnScreen(),要在view.post(Runable)里获取,即等布局变化后

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

    5.View可见部分 相对于 屏幕的坐标:getGlobalVisibleRect()

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

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

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

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

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

  • 相关阅读:
    IntelliJ IDEA ESLint autofix/prettier
    常见电脑屏幕分辨率
    在Vue中使用echarts的两种方式
    升级npm和vue-cli
    挖坑指南:module namespace not found in mapGetters()
    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式
    如何在Vue项目中调试单个组件
    使用jquery的load方法只加载body之间的内容
    固化分组
    占有优先量词
  • 原文地址:https://www.cnblogs.com/mengdao/p/15158481.html
Copyright © 2011-2022 走看看