美文网首页
View工作原理一:坐标系

View工作原理一:坐标系

作者: 水言 | 来源:发表于2017-12-21 15:47 被阅读37次

1.android坐标系

屏幕的左上角顶点作为坐标系原点,向左,向下为正方向。

Android坐标系

View.getLocationInWindow(int[] location)一个控件在其父窗口中的坐标位置

//View#getLocationInWindow
   public void getLocationInWindow(int[] location) {  
        if (location == null || location.length < 2) {  
            throw new IllegalArgumentException("location must be an array of two integers");  
        }  
        if (mAttachInfo == null) {  
            // When the view is not attached to a window, this method does not make sense  
            location[0] = location[1] = 0;  
            return;  
        }  
        float[] position = mAttachInfo.mTmpTransformLocation;  
        position[0] = position[1] = 0.0f;  
        if (!hasIdentityMatrix()) {  
            getMatrix().mapPoints(position);  
        }  
        position[0] += mLeft;  
        position[1] += mTop;  
        ViewParent viewParent = mParent;  
        while (viewParent instanceof View) {  
            final View view = (View) viewParent;  
            position[0] -= view.mScrollX;  
            position[1] -= view.mScrollY;  
            if (!view.hasIdentityMatrix()) {  
                view.getMatrix().mapPoints(position);  
            }  
            position[0] += view.mLeft;  
            position[1] += view.mTop;  
            viewParent = view.mParent;  
         }  
        if (viewParent instanceof ViewRootImpl) {  
            // *cough*  
            final ViewRootImpl vr = (ViewRootImpl) viewParent;  
            position[1] -= vr.mCurScrollY;  
        }  
        location[0] = (int) (position[0] + 0.5f);  
        location[1] = (int) (position[1] + 0.5f);  
    }  

View.getLocationOnScreen(int[] location) 一个控件在其整个屏幕上的坐标位置

//View#getLocationOnScreen
 public void getLocationOnScreen(int[] location) {
    getLocationInWindow(location);
    final AttachInfo info = mAttachInfo;
    if (info != null) {
      location[0] += info.mWindowLeft;
      location[1] += info.mWindowTop;
    }
  }

2.View坐标系

View坐标系与Android 坐标系并不冲突。

View自身坐标

View提供的API
•getTop():获取View顶边到其父布局顶边的距离
•getLeft():获取View左边到其父布局左边的距离
•getRight():获取View右边到其父布局左边的距离
•getBottom():获取View底边到其父布局顶边的距离

根据上图得到的数据,我们可以计算出View的宽高。View源码中getHeight()和getWidth()也是使用相同方法实现的,如下:

    @ViewDebug.ExportedProperty(category = "layout")
    public final int getWidth() {
        return mRight - mLeft;
    }
    @ViewDebug.ExportedProperty(category = "layout")
    public final int getHeight() {
        return mBottom - mTop;
    }

假设圆点是我们的触摸点,它会激活相应的触摸事件


触摸后的坐标

• getX():获取点击位置离View左边的距离
• getY():获取点击位置离View顶边的距离
• getRawX():获取点击位置离屏幕左边的距离
• getRawY():获取点击位置离屏幕顶边的距离

相关文章

  • View工作原理一:坐标系

    1.android坐标系 屏幕的左上角顶点作为坐标系原点,向左,向下为正方向。 View.getLocationI...

  • Android的View坐标系

    Android坐标系: View视图坐标系:

  • ★60.自定义控件 ★01.基础

    View坐标系 简介 子View的坐标系统是相对于父View而言的. 示意图 代码 Event坐标系 示意图 代码...

  • View坐标

    View事件体系 1、Android坐标系 屏幕区域划分 屏幕坐标系 2、View位置详解 View视图结构 对于...

  • Android Scroller分析

    (1)Android view的直角坐标系 需要注意的是,view的直角坐标系和数学的直角坐标系不同,view的x...

  • View编程指南(二)

    接上篇 View编程指南(一) 三、View的坐标系统 1. 坐标系统基础 UIKit默认的坐标系统是从左上角开始...

  • 关于android中的getX,getY,getRawX,get

    总结: view下的方法: getX(): 该view坐标系中,x坐标值 getY():该view坐标系中,y坐标...

  • 自定义View基础① 笔

    资料链接:1.GcsSloop的自定义系列 一、坐标系 1.移动设备坐标系 2.View的坐标系 注意:View的...

  • Android 个人知识体系梳理

    [toc] 自定义View View的坐标系 Camera坐标系 Camera坐标系的旋转方向 第一章 第二章 第...

  • View 的测量

    接着上篇 View 基础 来讲 View 的工作原理,View 的工作原理中最重要的就是测量、布局、绘制三大过程,...

网友评论

      本文标题:View工作原理一:坐标系

      本文链接:https://www.haomeiwen.com/subject/dehdextx.html