Android View(一)-View坐标以及方法说明

本文深入解析Android坐标系的概念及其在实际开发中的应用,详细讲解了与坐标系相关的系统方法,包括获取View坐标的具体实现及MotionEvent中的关键属性。同时,文章还总结了这些系统方法在开发过程中的重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在实际开发中,我们总是会和View打交道,例如View滑动、获取View坐标等等,那么就会牵扯到许多系统提供的Api方法,那么今天,我们就来详细了解一下,与之有关的系统方法。

一. 坐标系

    首页,我们先需要了解一下Android里面的坐标系(二维坐标系)。Android中存在两种坐标系,Android坐标系(屏幕坐标系)和视图坐标系(View坐标系)。先上一张图,如下:

通过上面这张图,我们可以得知:

   1.Android坐标系,是以手机屏幕左上角为原点,以水平向右为X轴正方向,以竖直向下为y轴正方向;

   2.视图坐标系,原点是该View的父View的左上角,以水平向右为X轴正方向,以竖直向下为y轴正方向;

需要补充的一点是,View视图可以是没有边界的,换句话说,就是View视图的大小可以比Android的手机屏幕大,甚至还大很多。还是依旧上图说明,如下:


这张图中,黑色的线框是View的大小,黄色线框是手机屏幕的大小,我们可以看出,View的大小比手机屏幕还大,并且只有在手机屏幕里面,我们才能看到。目前手机屏幕只显示Buttton2按钮,不在黄色线框中的视图,是隐藏(不可见)状态,当我们手指在手机屏幕上左右滑动时,可能才显示其他隐藏的视图。所以,我们在布局中可能会遇到,有的View只显示了一部分,就是这个原因!

二.Android提供的Api方法解释说明

1.View常用到的方法。

(1).getLeft(),当前View的左边缘与它父View的左边缘的距离(视图坐标);

(2).getRight(),当前View的右边缘与它父View的左边缘的距离(视图坐标);

(3).getTop(),当前View的上边缘与它父View的上边缘(顶部)的距离(视图坐标);

(4).getBottom(),当前View的下边缘与它父View的上边缘(顶部)的距离(视图坐标);

(5).getWidth(),获取当前View的宽度;

(6).getHeight(),获取当前View的高度;

我们可以看看View的源码

[java]  view plain copy
  1. /** 
  2.      * The distance in pixels from the left edge of this view's parent 
  3.      * to the left edge of this view. 
  4.      * {@hide} 
  5.      */  
  6.     @ViewDebug.ExportedProperty(category = "layout")  
  7.     protected int mLeft;  
  8.     /** 
  9.      * The distance in pixels from the left edge of this view's parent 
  10.      * to the right edge of this view. 
  11.      * {@hide} 
  12.      */  
  13.     @ViewDebug.ExportedProperty(category = "layout")  
  14.     protected int mRight;  
  15.     /** 
  16.      * The distance in pixels from the top edge of this view's parent 
  17.      * to the top edge of this view. 
  18.      * {@hide} 
  19.      */  
  20.     @ViewDebug.ExportedProperty(category = "layout")  
  21.     protected int mTop;  
  22.     /** 
  23.      * The distance in pixels from the top edge of this view's parent 
  24.      * to the bottom edge of this view. 
  25.      * {@hide} 
  26.      */  
  27.     @ViewDebug.ExportedProperty(category = "layout")  
  28.     protected int mBottom;  
  29.   /** 
  30.      * Left position of this view relative to its parent. 
  31.      * 
  32.      * @return The left edge of this view, in pixels. 
  33.      */  
  34.     @ViewDebug.CapturedViewProperty  
  35.     public final int getLeft() {  
  36.         return mLeft;  
  37.     }  
  38.   /** 
  39.      * Right position of this view relative to its parent. 
  40.      * 
  41.      * @return The right edge of this view, in pixels. 
  42.      */  
  43.     @ViewDebug.CapturedViewProperty  
  44.     public final int getRight() {  
  45.         return mRight;  
  46.     }  
  47.  /** 
  48.      * Top position of this view relative to its parent. 
  49.      * 
  50.      * @return The top of this view, in pixels. 
  51.      */  
  52.     @ViewDebug.CapturedViewProperty  
  53.     public final int getTop() {  
  54.         return mTop;  
  55.     }  
  56.   /** 
  57.      * Bottom position of this view relative to its parent. 
  58.      * 
  59.      * @return The bottom of this view, in pixels. 
  60.      */  
  61.     @ViewDebug.CapturedViewProperty  
  62.     public final int getBottom() {  
  63.         return mBottom;  
  64.     }  
  65.    /** 
  66.      * Return the width of the your view. 
  67.      * 
  68.      * @return The width of your view, in pixels. 
  69.      */  
  70.     @ViewDebug.ExportedProperty(category = "layout")  
  71.     public final int getWidth() {  
  72.         return mRight - mLeft;  
  73.     }  
  74.  /** 
  75.      * Return the height of your view. 
  76.      * 
  77.      * @return The height of your view, in pixels. 
  78.      */  
  79.     @ViewDebug.ExportedProperty(category = "layout")  
  80.     public final int getHeight() {  
  81.         return mBottom - mTop;  
  82.     }  

看一张图,我们可能就了然了。


2.MotionEvent中有这几个常用的方法getX(),getY(),getRawX(),getRawY()。(MotionEvent是该View的onTouchEvent()方法中的)

(1).getX(),触摸中心点与该View左边缘的距离(视图坐标);

(2).getY(),触摸中心点与该View上边缘(顶部)的距离(视图坐标);

(3).getRawX(),触摸中心点与屏幕左边缘的距离(绝对坐标);

(4).getRawY(),触摸中心点与屏幕上边缘(顶部)的距离(绝对坐标);

请留意,这几个是MotionEvent中的方法,调用的时候MotionEvent.getX()...。看一张图,如下:


源码如下所示:

[java]  view plain copy
  1. /** 
  2.      * {@link #getX(int)} for the first pointer index (may be an 
  3.      * arbitrary pointer identifier). 
  4.      * 
  5.      * @see #AXIS_X 
  6.      */  
  7.     public final float getX() {  
  8.         return nativeGetAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);  
  9.     }  
  10.   
  11.     /** 
  12.      * {@link #getY(int)} for the first pointer index (may be an 
  13.      * arbitrary pointer identifier). 
  14.      * 
  15.      * @see #AXIS_Y 
  16.      */  
  17.     public final float getY() {  
  18.         return nativeGetAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT);  
  19.     }  
  20.  /** 
  21.      * Returns the original raw X coordinate of this event.  For touch 
  22.      * events on the screen, this is the original location of the event 
  23.      * on the screen, before it had been adjusted for the containing window 
  24.      * and views. 
  25.      * 
  26.      * @see #getX(int) 
  27.      * @see #AXIS_X 
  28.      */  
  29.     public final float getRawX() {  
  30.         return nativeGetRawAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);  
  31.     }  
  32.   
  33.     /** 
  34.      * Returns the original raw Y coordinate of this event.  For touch 
  35.      * events on the screen, this is the original location of the event 
  36.      * on the screen, before it had been adjusted for the containing window 
  37.      * and views. 
  38.      * 
  39.      * @see #getY(int) 
  40.      * @see #AXIS_Y 
  41.      */  
  42.     public final float getRawY() {  
  43.         return nativeGetRawAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT);  
  44.     }  

三. 总结 

    文章主要是讲解了Android的两个坐标系,以及系统提供给我们的一些Api方法的使用说明。

    相信大家对这些系统提供的Api已经有所了解了吧!(本人水平有限,有错误的地方,欢迎大家指出)

    如果你还想了解View的scrollTo()和scrollBy()的话,那么请看这篇文章Android View(二)-View的scrollTo()以及scrollBy()说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值