ScrollTo(int x, int y)
相对源位置进行移动,移动的是view里面的内容,如果要想让TextView整体移动,可以在TextView外面套一层ViewGroup,然后调用layout的scrollTo方法,就可以实现TextView的移动了。
ScrollBy(int x,int y)
同scrollTo,移动时移动的是内部的视图,scrollBy是相对移动之前的位置进行移动。
自定义布局的平滑滚动效果
public class MyTextView extends FrameLayout {
static final int ANIMATED_SCROLL_GAP = 250;
private OverScroller mScroller;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new OverScroller(context);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
//当view重绘时会调用此方法,invalidate->onDraw->computeScroll
@Override
public void computeScroll() {
super.computeScroll();
// 判断Scroller是否执行完毕
if (mScroller.computeScrollOffset()) {
scrollTo(
mScroller.getCurrX(),
mScroller.getCurrY());
// 通过重绘来不断调用computeScroll
invalidate();
}
}
public final void smoothScrollToOrigin() {
mScroller.startScroll(
getScrollX(),
getScrollY(),
-getScrollX(),
-getScrollY(),1000);
invalidate();
}
}
滑动手势解析类
ViewDragHelper