首先View源码中没有Y这个成员变量。
View源码中的成员变量只有 mTop和mTransformationInfo(和translationY有关)。
Y只是作为一个抽象概念存在,它的计算关系如以下代码所示。
/**
* The visual y position of this view, in pixels. This is equivalent to the
* {@link #setTranslationY(float) translationY} property plus the current
* {@link #getTop() top} property.
*
* @return The visual y position of this view, in pixels.
*/
@ViewDebug.ExportedProperty(category = "drawing")
public float getY() {
return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
}
/**
* Sets the visual y position of this view, in pixels. This is equivalent to setting the
* {@link #setTranslationY(float) translationY} property to be the difference between
* the y value passed in and the current {@link #getTop() top} property.
*
* @param y The visual y position of this view, in pixels.
*/
public void setY(float y) {
setTranslationY(y - mTop);
}
/**
* Sets the vertical location of this view relative to its {@link #getTop() top} position.
* This effectively positions the object post-layout, in addition to wherever the object's
* layout placed it.
*
* @param translationY The vertical position of this view relative to its top position,
* in pixels.
*
* @attr ref android.R.styleable#View_translationY
*/
public void setTranslationY(float translationY) {
ensureTransformationInfo();
final TransformationInfo info = mTransformationInfo;
if (info.mTranslationY != translationY) {
invalidateViewProperty(true, false);
info.mTranslationY = translationY;
info.mMatrixDirty = true;
invalidateViewProperty(false, true);
if (mDisplayList != null) {
mDisplayList.setTranslationY(translationY);
}
if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
// View was rejected last time it was drawn by its parent; this may have changed
invalidateParentIfNeeded();
}
}
}
源码基于Android 4.3