Skip to content

Instantly share code, notes, and snippets.

@ixiyang
Last active August 29, 2015 14:03
Show Gist options
  • Save ixiyang/4cb264a2bc89cf1797bb to your computer and use it in GitHub Desktop.
Save ixiyang/4cb264a2bc89cf1797bb to your computer and use it in GitHub Desktop.
Scroll View 涉及到的类
OverScroller
startScroll() 开始滚动
computeScrollOffset() 方法不断计算滚动到的最新的位置,当滚动动画结束时,方法返回false。所以通常使用以下方式不断根据最新的位置绘制view来达到滚动的效果
if (mScroller.computeScrollOffset()) {
// This is called at drawing time by ViewGroup. We use
// this method to keep the fling animation going through
// to completion.
int oldX = getScrollX();
int oldY = getScrollY();
int x = mScroller.getCurrX();
int y = mScroller.getCurrY();
if (getChildCount() > 0) {
View child = getChildAt(0);
x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
if (x != oldX || y != oldY) {
scrollTo(x, y);
}
}
// Keep on drawing until the animation has finished.
postInvalidate();
}
VelocityTracker
该类用来获取touch动作的瞬时速度,根据瞬时速度可以来实现fling效果
ViewConfiguration
获取系统定义的一些touch常量
//bounds check ScrollView中使用的方法
/*
* Utility method to assist in doing bounds checking
*/
private int clamp(int n, int my, int child) {
if (my >= child || n < 0) {
/* my >= child is this case:
* |--------------- me ---------------|
* |------ child ------|
* or
* |--------------- me ---------------|
* |------ child ------|
* or
* |--------------- me ---------------|
* |------ child ------|
*
* n < 0 is this case:
* |------ me ------|
* |-------- child --------|
* |-- mScrollX --|
*/
return 0;
}
if ((my+n) > child) {
/* this case:
* |------ me ------|
* |------ child ------|
* |-- mScrollX --|
*/
return child-my;
}
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment