getLeft , getTop, getBottom, getRight表示控件在parent中的相对位置。
getX()是表示Widget相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值
getX()的不同点:
若是view用了自己的onTouch事件,即view.setOnTouchListener(),那么getX()和getY()获得的永远是view的触摸位置坐标(这两个值不会超过view的长度和宽度);
若是重写了Activity的onTouchEvent方法,这个时候获得的是屏幕点击位置的坐标。event.getX() 与 event.getRawX()获取的值是一样的。此时能获取触摸标题栏时的坐标位置,但不能获得触摸状态栏时的坐标位置。
在GridView中进行拖拽操作是,拖动Item时获取的getX不是相对于Item的坐标,而是相对于GridView的坐标。
上一张参考的图片,引用自:http://www.360doc.com/content/12/1109/14/7857928_246812407.shtml
这里附上MotionEvent中getRawX()的源码及View中getLeft()的源码:
/**
* Returns the original raw X coordinate of this event. For touch
* events on the screen, this is the original location of the event
* on the screen, before it had been adjusted for the containing window
* and views.
*
* @see #getX(int)
* @see #AXIS_X
*/
public final float getRawX() {
return nativeGetRawAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);
}
/**
* Left position of this view relative to its parent.
*
* @return The left edge of this view, in pixels.
*/
@ViewDebug.CapturedViewProperty
public final int getLeft() {
return mLeft;
}
首先需要注意的是,scrollTo()和scrollBy()方法的调用不是改变View的位置,而是滑动View中的内容。
先看下View中scrollTo()的定义:
/**
* Set the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the x position to scroll to
* @param y the y position to scroll to
*/
public void scrollTo(int x, int y) {
if (mScrollX != x || mScrollY != y) {
int oldX = mScrollX;
int oldY = mScrollY;
mScrollX = x;
mScrollY = y;
invalidateParentCaches();
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
if (!awakenScrollBars()) {
postInvalidateOnAnimation();
}
}
}
看注释可知道,scrollTo()方法表示设置自己的view滚动的位置,即移动内容到(x,y)坐标点,移动的是绝对距离。
再看View中scrollBy()的定义:
/**
* Move the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the amount of pixels to scroll by horizontally
* @param y the amount of pixels to scroll by vertically
*/
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}
由注释可知,scrollBy()方法表示移动自己的view滚动的位置,即在视图的X、Y方向上将内容各自移动x、y的距离,移动的是相对距离。
scrollTo(x,y):
x > 0表示内容从右往左移动到x坐标,反之从左往右移动到x坐标。
scrollBy(x,y):
x > 0表示内容从下往上移动x距离,反之从上往下移动x距离。
比如:scrollTo(100,0)表示内容向左移动到100的位置,为何传入的是100却要向左移呢?scrollTo(0,100)表示内容向上移动到100的位置呢?
因为,移动时,从我们用户角度(即以内容为参照物)来说,我们确实是向右移动到了x坐标为100的位置,但是系统是以内容所在的view为参照系的,即scrollTo(100,0)表示内容向左移动到(100,0)的位置,此时,获取的getScrollX为-100。scrollTo(0,100)表示内容向上移动到(0,100)的位置,此时,获取的getScrollY为-100
最后再附上View中getScrollX()和getScrollY,mScrollX和mScrollY的源码:
/**
* Return the scrolled left position of this view. This is the left edge of
* the displayed part of your view. You do not need to draw any pixels
* farther left, since those are outside of the frame of your view on
* screen.
*
* @return The left edge of the displayed part of your view, in pixels.
*/
public final int getScrollX() {
return mScrollX;
}
/**
* Return the scrolled top position of this view. This is the top edge of
* the displayed part of your view. You do not need to draw any pixels above
* it, since those are outside of the frame of your view on screen.
*
* @return The top edge of the displayed part of your view, in pixels.
*/
public final int getScrollY() {
return mScrollY;
}
/**
* The offset, in pixels, by which the content of this view is scrolled
* horizontally.
* {@hide}
*/
@ViewDebug.ExportedProperty(category = "scrolling")
protected int mScrollX;
/**
* The offset, in pixels, by which the content of this view is scrolled
* vertically.
* {@hide}
*/
@ViewDebug.ExportedProperty(category = "scrolling")
protected int mScrollY;