1. scrollBy/scrollTo
public void scrollTo(int x, int y)
public void scrollBy(int x, int y)
动画会比较生硬。不会像属性动画一样柔和,就像直接将View搬到指定的坐标点一样。to是移动到(x,y)坐标。by是基于本身位置方向上移动(x,y)的距离。
2.Scroller
public boolean computeScrollOffset() :Scroller类中的方法
public void startScroll(int startX, int startY, int dx, int dy, int duration) :Scroller类中的方法
public void computeScroll() : View中的方法
---
/**
* 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() {}
使用Scroller类可以实现很多滚动效果,类似于ViewPager,ListView等的滚动效果。
通过实例化Scroller后,调用startScroll来传入参数,这时并不能执行滑动,通过调用invalidate()去刷新View。而后Scroller执行后会触发computeScroll()回调。通过在回调中执行:
@Override
public void computeScroll() {
if(mScroller.computeScrollOffset()){
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
postInvalidate();
}
}
来实现弹性动画