Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标

Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标

你好!

Android 屏幕坐标系

我们都知道,Android的View的位置可以说是在一个看不见的平面直角坐标系上摆放着:
然后,屏幕的左上角,是(0,0),然后往右依次递加X,往下依次递加Y
在这里插入图片描述

View中关于X,Y坐标的方法

The horizontal location of this view relative to its left position. This position is post-layout, in addition to wherever the object’s layout placed it.

getTranslationX()  // View左上角相对于父容器的横向偏移量
      public float getTranslationX ()

The vertical location of this view relative to its top position. This position is post-layout, in addition to wherever the object’s layout placed it.

getTranslationY()   //View左上角相对于父容器纵向偏移量
    public float getTranslationY ()

Top position of this view relative to its parent.

getTop ()   //View原始左上角的x坐标的值,不管View怎么移动,这个值不会 发生改变
     public final int getTop ()

Left position of this view relative to its parent.

getLeft()    //View原始左上角Y坐标的值,同上,也是不会改变
     public final int getLeft ()

Bottom position of this view relative to its parent.

getBottom()  // View原始右下角Y坐标的值,同上,也是不会改变
    public final int getBottom ()

Right position of this view relative to its parent.

getRight()  //View原始右下角X坐标的值,同上,也是不会发生改变
     public final int getRight ()

The visual x position of this view, in pixels. This is equivalent to the translationX property plus the current left property.

 getX()   //获取View左上角的X坐标,相当于getLeft()+getTranslationX()
	public float getX ()

The visual y position of this view, in pixels. This is equivalent to the translationY property plus the current top property.

 getY()  //获取View左上角的Y坐标,相当于getTop+getTranslationY()
     public float getY()

The x location of the point around which the view is rotated and scaled.

  getPivotX()	//view围绕其旋转和缩放的点的X位置
       public float getPivotX ()

The y location of the point around which the view is rotated and scaled.

	getPivotY()   //view围绕其旋转和缩放的点的Y位置
		public float getPivotY ()

获取View的中心点坐标

如果想要获取View中心点的坐标,首先你需要知道它某个角的坐标和他的长和宽, 所以,我们首先要获取View一个角的坐标,

                 //左上角坐标    (left,top)
			    int top = view.getTop();
                int left = view.getLeft();

然后,我们还需要知道View的长和宽,想要知道View的 长和宽,除了测量,这里还有一个办法就是拿右下角的横坐标,减去左上角的横坐标,就得到了View的宽,拿右下角的纵坐标,减去左上角的纵坐标,就得到了View的高

                //右下角坐标
                int right = view.getRight();
                int bottom = view.getBottom();
				
				//View的 宽
				int viewWidth = right - left;
				//View的 高
				int viewHeight = bottom - top;

在之后,我们知道了View的左上角坐标以及View的长和宽,那么,View的中心点坐标就可以用 左上角的x加上View的宽除以2,和左上角的Y加上View的高除以2

				//中心点的Y坐标
      			int centerY = top+viewHeight/2;
      			//中心点的X坐标
      			int centerX = left+viewWidth/2;

检测View是否绘制成功

如果你已经直接在代码中获取了X,Y的坐标,你会发现,结果是(0,0),这是因为View还没有绘制成功,所以你获取他的坐标就是(0,0),所以,你需要在View绘制成功的回调内进获取坐标

//view绘制完成时回调
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
  		  // 已经完成绘制,干你要干的事情
             
    }
});

感谢观看

我是入梦,谢谢你的观看我的博客,如果有什么错误,请随时联系我,QQ:897589417

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值