view的位置参数:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="328dip"
        android:layout_height="96dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dip"
        android:background="@android:color/holo_orange_light"
        android:gravity="center">

        <TextView
            android:id="@+id/tv_hello"
            android:layout_width="228dip"
            android:layout_height="48dip"
            android:layout_gravity="center"
            android:background="@android:color/black"
            android:gravity="center"
            android:textColor="@android:color/background_light"
            android:text="Hello-text"
            android:textSize="24sp" />

    LinearLayout>

    <TextView
        android:id="@+id/tv_review"
        android:layout_width="228dip"
        android:layout_height="48dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="18dip"
        android:clickable="true"
        android:gravity="center"
        android:onClick="view_test"
        android:text="View的参数"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tv_move"
        android:layout_width="228dip"
        android:layout_height="48dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="18dip"
        android:clickable="true"
        android:gravity="center"
        android:onClick="view_move"
        android:text="右移的view动画"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tv_move_property"
        android:layout_width="228dip"
        android:layout_height="48dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="18dip"
        android:clickable="true"
        android:gravity="center"
        android:onClick="view_move_property"
        android:text="右移的属性动画"
        android:textSize="24sp" />

LinearLayout>

布局截图:
view的位置参数:_第1张图片

测试代码:

public class TestActivityTwo extends Activity {

    protected static final String TAG = "MyButton";
    private Button mButton ;

    private TextView tv_hello;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

        tv_hello = (TextView) findViewById(R.id.tv_hello);
    }

    /**
     * 作 者:**
     * 描 述:view的位置参数
     */
    public void view_test(View view) {

        //这几个view的基本参数的坐标都是相对于父控件来说的
        int mLeft = tv_hello.getLeft(); //左上角在x轴上的坐标
        int mTop = tv_hello.getTop(); //左上在y轴上的坐标
        int mRight = tv_hello.getRight(); //右下角在x轴上的坐标
        int mBottom = tv_hello.getBottom(); //右下角在y轴上的坐标
        Log.e("TestActivityTwo:", "mLeft = " + mLeft + "   mRight = " + mRight + "   mTop = " + mTop + "   mBottom = " + mBottom);

        //这几个参数也同样是相对父控件而言的
        int mX = (int) tv_hello.getX(); //左上角在x轴上的坐标
        int mY = (int) tv_hello.getY(); //左上角在y轴上的坐标
        int mTanslationX = (int) tv_hello.getTranslationX(); //左上角在x轴上的偏移量,默认值是0,偏移量是指的运行时的偏移量
        int mTanslationY = (int) tv_hello.getTranslationY(); //左上角在y轴上的偏移量,默认值是0,偏移量是指的运行时的偏移量
        Log.e("TestActivityTwo:", "mX = " + mX + "   mY = " + mY + "   mTanslationX = " + mTanslationX + "   mTanslationY = " + mTanslationY);

    }

    /**
     * 作 者:**
     * 描 述:view动画右移
     */
    public void view_move(View view) {

        float fromX = tv_hello.getX();
        float toX = fromX + 50;

        Animation animation = new TranslateAnimation(fromX, toX, 0, 0);
        animation.setFillAfter(true);
        animation.setDuration(500);

        tv_hello.startAnimation(animation);
        Log.e("TestActivityTwo:", "view动画右移------50px-------");
    }

    /**
     * 作 者:**
     * 描 述:属性动画右移
     */
    public void view_move_property(View view) {

        float fromX = tv_hello.getX();
        float toX = fromX + 50;

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv_hello, "x", fromX, toX)
                .setDuration(500);
        objectAnimator.start();

        Log.e("TestActivityTwo:", "属性动画右移------50px-------");
    }

}

日志输出:
mLeft = 150 mRight = 834 mTop = 72 mBottom = 216
mX = 150 mY = 72 mTanslationX = 0 mTanslationY = 0
view动画右移——50px——-
mLeft = 150 mRight = 834 mTop = 72 mBottom = 216
mX = 150 mY = 72 mTanslationX = 0 mTanslationY = 0
属性动画右移——50px——-
mLeft = 150 mRight = 834 mTop = 72 mBottom = 216
mX = 200 mY = 72 mTanslationX = 50 mTanslationY = 0

结论:
1.view动画:
并没有真正的移动view,焦点和坐标都不发生变化
2.属性动画:
会使目标view的焦点移动;
目标view的getX()和getY()的值会变为动画结束位置左上角的坐标;getTranslationX()也会得到真实的移动距离;
但是getLeft()/getTop()/getRight()和getBottom()这四个基本参数始终不会随着动画的移动而改变的。

你可能感兴趣的:(Android开发)