Scroller这个类理解起来有一定的困难,刚开始接触Scroller类的程序员可能无法理解Scroller和View系统是怎么样联系起来的。我经过自己的学习和实践,对Scroller的用法和工作原理有了一定的理解,在这里和大家分享一下,希望大家多多指教。
首先从源码开始分析:
View.java
[java] [view plain](() [copy](()
-
/**
-
* Called by a parent to request that a child update its values for mScrollX
-
* and mScrollY if necessary. This will typically be done if the child is
-
* animating a scroll using a {@link android.widget.Scroller Scroller}
-
* object.
-
*/
-
public void computeScroll()
-
{
-
}
computeScroll是一个空函数,很明显我们需要去实现它,至于做什么,就由我们自己来决定了。
因为View的子类很多,在下面的例子中,我会在一个自定义的类MyLinearLayout中去实现它。
ViewGroup.java
[java] [view plain](() [copy](()
-
@Override
-
protected void dispatchDraw(Canvas canvas) {
-
…
-
…
-
…
-
…
-
for (int i = 0; i < count; i++) {
-
final View child = children[i];
-
if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)
-
{
-
more |= drawChild(