Android里Scroller类是为了实现View平滑滚动的一个Helper类。通常在自定义的View时使用,在View中定义一个私有成员mScroller = new Scroller(context)。设置mScroller滚动的位置时,并不会导致View的滚动,通常是用mScroller记录/计算View滚动的位置,再重写View的computeScroll(),完成实际的滚动。
相关API介绍如下
相关API介绍如下
以下是个人写的示例代码
public class CustomView extends ImageView{
public Context mContext;
public Scroller mScroller = null;
public CustomView(Context context) {
super(context);
this.mContext = context;
// TODO Auto-generated constructor stub
}
public CustomView(Context context,AttributeSet set){
super(context, set);
this.mContext = context;
}
@Override
public void computeScroll() {
// TODO Auto-generated method stub
super.computeScroll();
if (mScroller == null) {
mScroller = new Scroller(mContext);
}
if (mScroller.computeScrollOffset()) {
this.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
public void move(int startX,int startY){
mScroller.startScroll(startX, startY, -100, startY);
postInvalidate();
}
}