OverScroller
和Scroller类似,都只是根据duration、已过去的时间,start position,final position,根据某种interpolator计算某个时刻的scrollX和scrollY(这里说的scrollX/Y和View的成员mScrollX/Y无关)。AbsListView和ScrollView添加阻尼效果使用的Scroller都是OverScroller,当然,还需其他类配合。
如果需要使用判断边界和绘制某种边界效果,使用View.overScrollBy()方法代替直接使用View.scrollTo()。而是否允许overScrollmode,则可由android:overScrollMode="true|false"来设定。
如果想要制造阻尼效果,可以参考ScrollView的实现,简单明了,AbsListView太复杂了。ScrollView使用computeScroll()驱动scroll过程的(AbsListView使用的其内部的FlingRunnable,通过不断postOnAnimation(Runnable)驱动scroll过程)。因为滚动计算和滚动变化AbsListView更复杂,所以夹杂在其中的阻尼效果代码的流程就不那么明朗,但是ListView也是使用了OverScroller,overScrollBy(),onOverScroll,EdgeEffect来实现阻尼效果的。
下面看看ScrollView#computeScroll():
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
// This is called at drawing time by ViewGroup. We don't want to
// re-show the scrollbars at this point, which scrollTo will do,
// so we replicate most of scrollTo here.
//
// It's a little odd to call onScrollChanged from inside the drawing.
//
// It is, except when you remember that computeScroll() is used to