View中有关位置的属性--getX、getScrollX、getLeft、getWidth等,基于sdk20

View中的属性也是挺多的,搞得初学者都有点懵逼,今天就来捋一捋。

1、mLeft、mTop、mRight、mBottom定义了控件的区域(左上角和右下角的坐标),都有对应的get、set方法,其中mLeft表示控件左边距离父控件左边的距离,mRight表示控件右边距离父控件左边的距离,mTop和mBottom类似;

2、getWidth和getHeight,源码如下:

public final int getWidth() {
    return mRight - mLeft;
}
public final int getHeight() {
    return mBottom - mTop;
}
由上面的源代码可以发现宽高其实是由mRight-mLeft以及mBottom-mTop计算出来的,看到这里估计大家就知道为什么 这两个方法在layout方法之后就可以正常获取到控件的宽高了吧,原因很简单: 因为layout之后mRight,mLeft,mTop,mBottom就确定了。

3、getMeasureWidth和getmeasureHeight,获取控件的测量值,在onMeasure方法之后就可以获取正常值,其中onMeasure方法中的核心方法是setOnMeasureDimension。

4、getX、setX、getY和setY方法,源码如下:

/**
 * The visual x position of this view, in pixels. This is equivalent to the
 * {
   @link #setTranslationX(float) translationX} property plus the current
 * {
   @link #getLeft() left} property.
 *
 * @return The visual x position of this view, in pixels.
 */

public float getX() {
    return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
}
/**
 * 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 pixel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值