Android 坐标系相关

1.getLocationInWindow和getLocationOnScreen

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

View.getLocationInWindow(int[] location)

一个控件在其整个屏幕上的坐标位置,注意这个值要从屏幕顶端算起,也就是包括了通知栏的高度。

View.getLocationOnScreen(int[] location)

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5a2f6Iqz6Iqz,size_10,color_FFFFFF,t_70,g_se,x_16

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

getLocationOnScreen以A为原点。

 

注意:在Activity的onCreate()方法里获取这些值时,全为0,要等UI控件都加载完了才能获取到。因此在onWindowFocusChanged(boolean hasFocus)中获取为好。

 

②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):

/** 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. */  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值