Android 自定义View之位置参数

来源:互联网 发布:js动态绑定type 编辑:程序博客网 时间:2024/05/18 03:50

本文主要介绍 : top, left, right, bottom, x, y, translationX, translationY.

View的位置主要由四个顶点的位置确定 对应着View的四个属性值:top, left, right, bottom.

注意 : 这些坐标都是相对于View的父控件来说的,是相对位置

View 相对位置.png

View的宽高和View坐标的关系:

// 宽度width = right - left;// 高度height = bottom - top;

获取View的位置参数:

// 相当简单吧, getXXX()系列方法.left = getLeft();right = getRight();top = getTop();bottom = getBottom();

Android 3.0 增加了几个额外的参数: x, y, translationX, translationY
它们都是相对于父控件而言的位置.

  • (x, y) : 是View左上角坐标.注意:此时的坐标系是父控件的左上顶点为原点
  • translationX : View相对于父控件在X轴上的位移量.初始值为 0.
  • translationY : View相对于父控件在Y轴上的位移量.初始值为 0.

注意 : 对于translationX 和 translationY来说如果控件没有发生移动他们就都是0,只有View发生了位移translationX和translationY的值才会有变化.

三种位置参数关系:

x = left + translationX;y = top + translationY;

在上面的公式中,left 和 top 的值是始终不变的. 在平移过程中x, y ,translationX, translationY 变化如下 :

初始位置.

注意1 : 以下变化都是通过属性动画改变View的位置
注意2 : 移动过程中top, left, bottom, right 是不会不变化的.

  • 当View向 x轴负方向移动时(向左):
    • x 减小.
    • translationX 减小.
  • 当View向 x轴正方向移动(向右):
    • x 增大.
    • translationX 增大

Paste_Image.png

  • 当View向 y轴负方向移动时(向上):
    • y 减小.
    • translationY 减小.
  • 当View向 y轴正方向移动(向下):
    • y 增大.
    • translationY 增大

Paste_Image.png

总结 :

  • 上述位置参数都是相对于父控件的而言的.
  • 只有在程序运行过程中发生位移x , y, translationX , translationY才会变化.
  • 移动过程中 top, left bottom, right不会发生变化.
0 0
原创粉丝点击