- 参考博客写在前面:
- http://blog.csdn.net/St_Chan/article/details/73801281#reply
- http://blog.csdn.net/chdjj/article/details/42293753
- http://www.gcssloop.com/customview/CoordinateSystem
- http://blog.csdn.net/wxv111/article/details/50901199
看了上面几个大神的博客后,发现结论有点出入,于是自己动手做下测试。
一、View的自身方法:
**
* 测试View的坐标系
*
* 结论:
*
* View中自身方法:
view.getTop(); //获取子View左上角距父View顶部的距离
view.getLeft(); //获取子View左上角距父View左侧的距离
view.getBottom(); //获取子View右下角距父View顶部的距离
view.getRight(); //获取子View右下角距父View左侧的距离
view.getTranslationX():计算的是该View在X轴的偏移量。初始值为0,向左偏移值为负,向右偏移值为正。
view.getTranslationY():计算的是该View在Y轴的偏移量。初始值为0,向上偏移为负,向下偏移为正。
view.getX=view.getTranslationX()+view.getLeft() 相当于该view距离父容器左边缘的距离
view.getY=view.getTranslationY()+view.getTop()
前面四个值 和getX 、getY 随
1、父控件的padding变化,前面四个值 和getX 、getY 变化。而不受父控件的margin值的影响。
2、自身的margin值变化,前面四个值 和getX 、getY 变化。而不受自身padding值的影响。
(1)、测试代码布局如下:
<com.hlx.view.evanhlxcustomview.view.CustomLinearLayout android:id="@+id/c_ll" android:layout_width="match_parent" android:background="#f5f102" android:layout_height="match_parent" android:layout_marginTop="20dp"> <com.hlx.view.evanhlxcustomview.view.CustomView android:id="@+id/c_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50px" android:src="@mipmap/mayun01" /> </com.hlx.view.evanhlxcustomview.view.CustomLinearLayout>
(2)、测试自身margin:
测试代码:
public void addMarginLeft(View view) {
Log.i("ScrollTestActivity", "addMarginLeft before --> left : " + mCustomView.getLeft() + " getX : " + mCustomView.getX() + " getTranslationX: " + mCustomView.getTranslationX());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = mCustomView.getLeft() + 100;
mCustomView.setLayoutParams(params);
mCustom