移动 translationX、translationY
- float translationX :表示在 X 轴上的平移距离,以当前控件为原点,向右为正方向,参数 translationX 表示移动的距离。
- float translationY :表示在 Y 轴上的平移距离,以当前控件为原点,向下为正方向,参数 translationY 表示移动的距离。
例如:
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationY", 200);
animator.setDuration(2000);
animator.start();
指定的移动距离是200,所以控件会从自身所有位置向下移动 200 像素
但是如果再调用
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationY", -200);
animator.setDuration(2000);
animator.start();
不会回到原来的位置,而是从最开始的位置,也就是第一次移动之前的位置向上移动200像素
另外,移动之后调用getLocationOnScreen获取到的位置和原始位置是不一样的,但是ObjectAnimator计算的原点位置仍然是移动之前的位置