Scroller使用教程

来源:互联网 发布:阿里妈妈和淘宝联盟 编辑:程序博客网 时间:2024/04/29 21:43

3个步骤轻松使用Scroller实现平滑移动

1.初始化Scroller

mScroller = new Scroller(context);

2.重写computeScroll()

@Override

public void computeScroll(){

// 判断Scroller是否执行完毕

if ( mScroller.computeScrollOffset() ){

((View) getParent()).scrollTo(

mScroller.getCurrX(),

mScroller.getCurrY());

// 通过重绘来不断调用 computeScroll

invalidate();

}

}

3.startScroll开启模拟过程

public void startScroll(int startX,int startY,int dx,int dy,int duration)


Demo:

case MotionEvent.ACTION_UP:

View viewGroup = ((View)getParent());

mScroller.startScroll(

viewGroup.getScrollX(),

viewGroup.getScrollY(),

-viewGroup.getScrollX(),

-viewGroup.getScrollY());

invalidate();

break;

0 0