# 前言

a.度娘一下什么都知道了，整理这些东西有什么用？还费时费力的！ 如何才能完全掌握一个知识？当你能把它讲清楚的时候，你才算掌握了他，所以这也是我整理这些知识点最大的原因。

# 目录

• mScrollX和mScrollY
• scrollTo()和scrollBy()
• smoothScrollBy和smoothScrollTo
• view和viewgroup

### 一.mScrollX和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;

/**
* 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;
}```

mScrollX：表示离视图起始位置的x水平方向的偏移量

mScrollY：表示离视图起始位置的y垂直方向的偏移量

### 二.scrollTo()和scrollBy()

```<span style="font-family:SimSun;font-size:14px;"> /**
* 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();
}
}
}

/**
* 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);
}</span>```

scrollTo（int x,int y）：

scrollBy（int x,int y）：

### 三.smoothScrollTo和smoothScrollBy

ScrollView.smoothScrollBy和smoothScrollTo的源码

```/**
* Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
*
* @param dx the number of pixels to scroll by on the X axis
* @param dy the number of pixels to scroll by on the Y axis
*/
public final void smoothScrollBy(int dx, int dy) {
if (getChildCount() == 0) {
// Nothing to do.
return;
}
long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
if (duration > ANIMATED_SCROLL_GAP) {
final int height = getHeight() - mPaddingBottom - mPaddingTop;  //获取部件高度
final int bottom = getChildAt(0).getHeight();   //获取当前能看到的item高度
final int maxY = Math.max(0, bottom - height);
final int scrollY = mScrollY;
dy = Math.max(0, Math.min(scrollY + dy, maxY)) - scrollY;

mScroller.startScroll(mScrollX, scrollY, 0, dy);
postInvalidateOnAnimation();
} else {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
if (mFlingStrictSpan != null) {
mFlingStrictSpan.finish();
mFlingStrictSpan = null;
}
}
scrollBy(dx, dy);
}
mLastScroll = AnimationUtils.currentAnimationTimeMillis();
}

/**
* Like {@link #scrollTo}, but scroll smoothly instead of immediately.
*
* @param x the position where to scroll on the X axis
* @param y the position where to scroll on the Y axis
*/
public final void smoothScrollTo(int x, int y) {
smoothScrollBy(x - mScrollX, y - mScrollY);
}```

moothScrollBy(int dx, int dy) ：

dy = Math.max(0, Math.min(scrollY + dy, maxY)) - scrollY;为方法的核心，比较绕，大致为计算当前滑动状态下可划动距离；

if(duration > ANIMATEDSCROLLGAP) 这句是判断当前是否在滚动，当还在滚动状态下，执行mScroller.startScroll(mScrollX, scrollY, 0, dy)；当不在滚动，就立马打断。

smoothScrollTo(int x, int y)：

### 四.view和viewgroup

View派生出的直接子类有：AnalogClock,ImageView,KeyboardView, ProgressBar,SurfaceView,TextView,ViewGroup,ViewStub

View派生出的间接子类有：AbsListView,AbsSeekBar, AbsSpinner, AbsoluteLayout, AdapterView,AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, AutoCompleteTextView,Button,CalendarView, CheckBox, CheckedTextView, Chronometer, CompoundButton

ViewGroup派生出的间接子类有：AbsListView,AbsSpinner, AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, CalendarView, DatePicker, DialerFilter, ExpandableListView, Gallery, GestureOverlayView,GridView,HorizontalScrollView, ImageSwitcher,ListView,

END

0 条评论

## 相关文章

1544

### GUI编程

AWT（Abstract Window Toolkit）包含了很多类和接口，用于Java Application的GUI（Graphics User Inte...

922

762

1343

1152

### Markdown-认识与使用1.简介2.使用一级标题3.设置Markdown编辑

Markdown 是一种轻量级的「标记语言」，它的优点很多，目前也被越来越多的写作爱好者，撰稿者广泛使用。看到这里请不要被「标记」、「语言」所迷惑，Markdo...

721

### 可视化（番外篇）——SWT总结

本篇主要介绍如何在SWT下构建一个应用，如何安装SWT Designer并破解已进行SWT的可视化编程，Display以及Shell为何物、有何用，SWT中...

19510

3925

2269

2189