移動 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計算的原點位置仍然是移動之前的位置