Android View - 位置参数

27 篇文章 0 订阅
11 篇文章 0 订阅

View的位置参数非常重要,Android系统只有知道View的位置参数,才能确定View所在的位置和大小。

View位置参数相关的属性:
[left,right,top,bottom,elevation]
[translationX,translationY,translationZ]
[x,y,z]

下面对着3组参数进行讲解。

left,right,top,bottom

left,right,top,bottom

通过这张图,应该很清晰知道:
left: View的最左边离ViewGroup(父控件)最左边的距离,通过View的getLeft()方法获取。
right: View的最右边离ViewGroup(父控件)最左边的距离,通过View的getRight()方法获取。。
top: View的最上边离ViewGroup(父控件)最上边的距离,通过View的getTop()方法获取。。
bottom: View的最下边离ViewGroup(父控件)最上边的距离,通过View的getBottom()方法获取。。
还有一个属性在这张图体验不到的:
elevation: View所在的Z轴高度离ViewGroup(父控件)Z轴高度的距离,通过View的getElevation()方法获取(Android 5.0之后的新属性)。

这组属性是相对ViewGroup(父控件)的,也是View的原始属性,当View使用动画(不改变View的LayoutParams)之后,这组值是不会改变的,改变的是下面一组值。

translationX,translationY,translationZ

这组是View使用动画(滑动)(不改变View的LayoutParams)之后才有的,默认为0。

translationX: View动画(滑动)之后位置的最左边离View原始位置的最左边的距离,通过View的getTranslationX()方法获取。
translationY: View动画(滑动)之后位置的最上边离View原始位置的最上边的距离,通过View的getTranslationY()方法获取。
translationZ: View动画之后位置Z轴高度距离View原始位置Z轴高度的距离,通过view.getTranslationZ()方法获取(Android 5.0之后的新属性)。

这组属性动画(滑动)之后,相对View的原始位置的属性。

x,y,z

x: View 当前位置 的最左边离ViewGroup(父控件)最左边的距离,通过View的getX()方法获取。
y: View 当前位置 的最上边离ViewGroup(父控件)最上边的距离,通过View的getY()方法获取。
z: View 当前位置 的Z轴位置离ViewGroup(父控件)Z轴位置的距离,通过View的getZ()方法获取(Android 5.0之后的新属性)。

这组属性是相对ViewGroup(父控件)的距离。

3组属性关系

x = left + translationX;
y = top + translationY;
z = elevation + translationZ;

例子说明

上面的解析估计有点懵,下面举个例子来体验上面3组属性:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.johan.testviewlocation.MainActivity" >
    <View 
        android:id="@+id/my_view"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="100dp"
        android:background="@android:color/holo_orange_light"
        />
</RelativeLayout>
public class MainActivity extends Activity {
    private View myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myView = findViewById(R.id.my_view);
        myView.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e(getClass().getName(), "myView 动画之前属性 :");
                Log.e(getClass().getName(), 
                "left : " + myView.getLeft() + 
                " , right : " + myView.getRight() + 
                ", top : " + myView.getTop() + 
                " , bottom : " + myView.getBottom());
                Log.e(getClass().getName(), 
                "translationX : " + myView.getTranslationX()+ 
                " , translationY : " + myView.getTranslationY());
                Log.e(getClass().getName(), 
                "x : " + myView.getX() + 
                " , y: " + myView.getY());
                ObjectAnimator animator = ObjectAnimator.ofFloat(myView, "translationY", 0, 100);
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Log.e(getClass().getName(), "myView 动画之后属性 :");
                        Log.e(getClass().getName(), 
                        "left : " + myView.getLeft() + 
                        " , right : " + myView.getRight() + 
                        ", top : " + myView.getTop() + 
                        " , bottom : " + myView.getBottom());
                        Log.e(getClass().getName(), 
                        "translationX : " + myView.getTranslationX()+ 
                        " , translationY : " + myView.getTranslationY());
                        Log.e(getClass().getName(), 
                        "x : " + myView.getX() + 
                        " , y: " + myView.getY());
                        RelativeLayout.LayoutParams params = 
                        (RelativeLayout.LayoutParams) myView.getLayoutParams();
                        params.leftMargin = 1000;
                        params.topMargin = 1000;
                        myView.requestLayout();
                        myView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Log.e(getClass().getName(), "myView 改变LayoutParams之后属性 :");
                                Log.e(getClass().getName(), 
                                "left : " + myView.getLeft() + 
                                " , right : " + myView.getRight() + 
                                ", top : " + myView.getTop() + 
                                " , bottom : " + myView.getBottom());
                                Log.e(getClass().getName(), 
                                "translationX : " + myView.getTranslationX()+ 
                                " , translationY : " + myView.getTranslationY());
                                Log.e(getClass().getName(), 
                                "x : " + myView.getX() + 
                                " , y: " + myView.getY());
                            }
                        }, 1000);
                    }
                });
                animator.setDuration(500);
                animator.start();
            }
        }, 200);
    }
}

上面的打印结果如下图:

从上面结果我们可以看出,我们执行平移动画之后,translationY发生了变化,而left,right,top,bottom并没有改变。当我们改变LayoutParams时,left,right,top,bottom改变了,x和y也变了,而translationX和translationY没有变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值