Android-区分 View 的 translationX、Y , X、Y 和 Left、Top,Right、Bottom
来源:互联网 发布:单片机采样频率 编辑:程序博客网 时间:2024/05/17 08:59
Android - 区分 View 的 translationX/Y , X/Y 和 Left/Top,Right/Bottom
1. left,top,right,bottom
View 的位置主要由它的四个顶点的位置来决定,分别对应 View 的四个属性:left top right bottom。
- left: View 左上顶点相对于父容器的横坐标
- top: View 左上顶点相对于父容器的纵坐标
- right: View 右下顶点相对于父容器的横坐标
- bottom: View 右下顶点相对于父容器的纵坐标
注意这几个属性值的决定都是参照父容器的坐标。其值的设置和获取可通过对应的 getter/setter 方法。
2. X,Y
从 Android 3.0 开始,View 增加了 x,y,translationX 和 translationY。
x,y 同样是 View 左上角相对父容器的坐标,但不同于 left 和 top ,这两个坐标点的值并一定都是相等的。而不相等的情况是由 translationX 和 translationY 值的设置引起的。
3. translationX,translationY
translationX 和 translationY 是 View 左上角相对父容器左上角的偏移量,translationX 和 translationY 默认值都为 0。
几个参数有如下换算关系:
x = left + translationX
4.总结
left/top,right/bottom 的值都是相对父容器而言的,具体为父容器的左上角,而 translationX/Y 可以移动这个参照点(父容器左上角),通过改变参照点的位置来改变 View 的位置。
这里要注意的是 translationX/Y 改变参照点的位置是理论上的改变,只是子 View 参照的位置变了,父容器左上角的实际坐标是没有变化的。
在这种情况下(translationX/Y 均不为 0,即子 View 的参照点位置变了),x/y 和 left/top 的值就不相等了,此时 x/y 的值是参照父容器左上角实际位置的坐标,而 left/top 是参照 translationX/Y 变化后的坐标点的值。
通过上面的分析可以分析这样一种情况:
改变 View 的 translationX/Y,此时 View 的 x/y 变化,而 left/top 不变。
写个 demo 验证下:
布局
... <FrameLayout android:layout_margin="30dp" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text_test" android:textColor="@color/colorAccent" android:padding="20dp" android:background="@color/colorPrimaryDark" android:text="sdafasd" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>...
代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); TextView test = (TextView) findViewById(R.id.text_test); test.setTop(10); test.setLeft(30); out(test); test.setTranslationX(50); test.setTranslationY(80); out(test); } private void out(View view) { int top = view.getTop(); int left = view.getLeft(); int x = (int) view.getX(); int y = (int) view.getY(); int translationX = (int) view.getTranslationX(); int translationY = (int) view.getTranslationY(); StringBuilder builder = new StringBuilder(); builder.append(" top=" + top) .append(" left=" + left) .append(" x=" + x) .append(" y=" + y) .append(" translationX=" + translationX) .append(" translationY=" + translationY); Log.i("main", builder.toString()); }
日志输出
07-17 01:54:33.836 9212-9212/com.duan.myapplication I/main: top=10 left=30 x=30 y=10 translationX=0 translationY=007-17 01:54:33.840 9212-9212/com.duan.myapplication I/main: top=10 left=30 x=80 y=90 translationX=50 translationY=80
分析:
- Android-区分 View 的 translationX、Y , X、Y 和 Left、Top,Right、Bottom
- Android view 的参数x y left right top bottom translationX translationY
- Android 获取View的 left、right、top、buttom、以及x、y坐标的方法
- android View TranslationX/Y
- View的位置--x,y,translationX,translationY
- 面试总结(4):top、left、right、bottom 和 translationX、translationY 和 scrollX,scrollY的区别
- 一张图看懂TranslationX,Y和X,Y的区别
- View的位置--top, left, bottom, right
- Android View Touch ScrollX/Y TransLationX/Y ScaleX/Y
- View[2] left、top、right、bottom
- View的位置参数mLeft,mRight,mTop,mBottom,mScrollX,mScrollY,x,y,translationX,translationY
- margin: left right /top bottom的怪异
- Android scrollBy(x,y)和scrollTo(x,y)的区别
- android中View的scrollBy(int x,int y)和scrollTo(int x,int y)的区别
- 已知left和top,求right和bottom
- view中的 translationx、x、rawx、scrollx、left
- android的实际加速度x 和 y
- View中的left和getx和translationx之间的区别
- 第五章 引用类型(上)
- 面向对象的三个基本特征(封装)
- 弹性布局时div被内容撑开问题
- apk加固原理之解密壳工程
- python GC(Garbage collection )、属性访问拦截器、getattribute()
- Android-区分 View 的 translationX、Y , X、Y 和 Left、Top,Right、Bottom
- js轮播图
- 448. Find All Numbers Disappeared in an Array
- SwitchHosts—hosts管理利器
- Redis简介
- 深入理解计算机系统 1
- BZOJ3343 教主的魔法 解题报告【数据结构】【分块】
- 一道有趣的面试题:为一款手机app开发后台程序,手机app怎么和服务器端保持会话状态?
- zTree默认展开树节点