Android动画View的setX跟setTranslationX区别结合源码解析

以TextView为例我们看看,先看一下我的xml:

    <TextView
        android:id="@+id/tv_test"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:text="Test"/>

看一下原图:

然后我们调一下setX看看:

        tv_test.setX(100);

看一下效果:

再看一下setTranslationX:

        tv_test.setTranslationX(100);

看一看效果:

接下来我们看一下setX源码:

    /**
     * Sets the visual x position of this view, in pixels. This is equivalent to setting the
     * {@link #setTranslationX(float) translationX} property to be the difference between
     * the x value passed in and the current {@link #getLeft() left} property.
     *
     * @param x The visual x position of this view, in pixels.
     */
    public void setX(float x) {
        setTranslationX(x - mLeft);
    }

这里mLeft就是marginLeft的值由此可以看出setX与setTranslationX区别在于setX减去了marginLeft的值后的偏移量,setTranslationX是直接设置偏移量。

欢迎关注微信公众号!你的每个赞和在看,都是对我的支持!👍在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值