我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的区别

简介: View.getLocationInWindow(int[] location) 一个控件在其父窗口中的坐标位置 View.getLocationOnScreen(int[] location) 一个控件在其整个屏幕上的坐标位置 getLocationInWindow是以B为原点的C的坐标 getLocationOnScreen以A为原点。
View.getLocationInWindow(int[] location)

一个控件在其父窗口中的坐标位置

View.getLocationOnScreen(int[] location)

一个控件在其整个屏幕上的坐标位置




getLocationInWindow是以B为原点的C的坐标

getLocationOnScreen以A为原点。


下面是getLocationOnScreen示例

start = (Button) findViewById(R.id.start);
		int []location=new int[2];
		start.getLocationOnScreen(location);
		int x=location[0];//获取当前位置的横坐标
		int y=location[1];//获取当前位置的纵坐标


下面是getLocationInWindow示例

start = (Button) findViewById(R.id.start);
		int []location=new int[2];
		start.getLocationInWindow(location);
		int x=location[0];//获取当前位置的横坐标
		int y=location[1];//获取当前位置的纵坐标

==================================================================================================

 附上源代码

==================================================================================================

View.getLocationInWindow(int[] location)

/**
     * <p>Computes the coordinates of this view in its window. The argument
     * must be an array of two integers. After the method returns, the array
     * contains the x and y location in that order.</p>
     *
     * @param location an array of two integers in which to hold the coordinates
     */
    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)

  /**
     * <p>Computes the coordinates of this view on the screen. The argument
     * must be an array of two integers. After the method returns, the array
     * contains the x and y location in that order.</p>
     *
     * @param location an array of two integers in which to hold the coordinates
     */
    public void getLocationOnScreen(int[] location) {
        getLocationInWindow(location);

        final AttachInfo info = mAttachInfo;
        if (info != null) {
            location[0] += info.mWindowLeft;
            location[1] += info.mWindowTop;
        }
    }

 



                            ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

====================================================================================

 



 
相关文章
|
1天前
|
安全 Android开发 数据安全/隐私保护
请说明鸿蒙操作系统与其他操作系统(如Android和iOS)的主要区别。
请说明鸿蒙操作系统与其他操作系统(如Android和iOS)的主要区别。
64 1
|
1天前
|
Android开发
Android基础知识:请解释Service是什么,它与IntentService的区别是什么?
Android基础知识:请解释Service是什么,它与IntentService的区别是什么?
44 0
|
8月前
|
编解码 物联网 开发工具
Android平台内网RTSP网关和轻量级RTSP服务的区别和联系
我们在对接轻量级RTSP服务的时候,遇到客户这样的使用场景:客户是用于车载自组网环境,确保多辆车之间可以相互看到对方的实时视频,以期可以了解到前方路况等关注的信息。
106 0
|
1天前
|
API 开发工具 Android开发
iOS 和 Android 平台的开发有哪些主要区别?
iOS与Android开发区别:iOS用Objective-C/Swift,App Store唯一下载渠道;Android用Java/Kotlin,多商店发布(如Google Play、华为市场)。设计上,iOS简洁一致,Android灵活可定制。开发工具,iOS用Xcode,Android用Android Studio。硬件和系统多样性,iOS统一,Android复杂。权限管理、审核流程及API各有特点,开发者需依据目标平台特性进行选择。
39 3
|
1天前
|
API Android开发
Android高手进阶教程(十五)之---通过Location获取Address的使用!
Android高手进阶教程(十五)之---通过Location获取Address的使用!
11 1
|
1天前
|
XML 存储 编解码
android 目录结构中 drawable(hdpi,ldpi,mdpi) 的区别
android 目录结构中 drawable(hdpi,ldpi,mdpi) 的区别
11 1
|
16小时前
|
消息中间件 缓存 架构师
2024年阿里Android高级面试题分享,附学习笔记+面试整理+进阶书籍
2024年阿里Android高级面试题分享,附学习笔记+面试整理+进阶书籍
|
1天前
|
Java 开发工具 Android开发
鸿蒙HarmonyOS 与 Android 的NDK有什么区别?
鸿蒙(HarmonyOS)和Android的NDK(Native Development Kit)是两个不同的概念,它们在设计理念、架构、开发方式和目标平台等方面存在着一些显著的不同。
8 0
|
1天前
|
Android开发
Android Makefile中inherit-product函数和include的区别
Android Makefile中inherit-product函数和include的区别
12 0
|
1天前
|
数据可视化 Android开发 容器
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
103 5