Android滚动工具类——Scroller
滚动的基础
任何一个控件都可以通过View类当中的scrollTo()和scrollBy()这两个方法实现滚动。
scrollTo:滚动的基础。
/**
* Set the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the x position to scroll to
* @param y the y position to scroll to
*/
//视图滚动到的位置
public void scrollTo(int x, int y) {
// 判断x和y轴是否发生滚动
if (mScrollX != x || mScrollY != y) {
// 记录起始x轴位置
int oldX = mScrollX;
// 记录起始y轴位置
int oldY = mScrollY;
// 要滚动到的x轴位置
mScrollX = x;
// 要滚动到的y轴位置
mScrollY = y;
// 强制清空父View缓存,并不会调用invalidate()
invalidateParentCaches();
// 调用此放法位置发生变化
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
// 判断滚动动画是否启动,是true,否false
// 如果动画启动会调用invalidate()
if (!awakenScrollBars()) {
// 动画发生无效
postInvalidateOnAnimation();
}
}
}
scrollBy:网上有一句话叫做万物基于MIUI,通过scrollBy中的scrollTo可以看到Android所有滚动基于scrollTo。
/**
* Move the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the amount of pixels to scroll by horizontally
* @param y the amount of pixels to scroll by vertically
*/
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}
Scroller是什么?
Scroller是一个专门用于处理滚动效果的工具类,ViewPager、ListView等控件在内部都是使用Scroller来实现的,下面有一段来自官方的介绍...
This class encapsulates scrolling. You can use scrollers ({@link Scroller}or {@link OverScroller}) to collect the data you need to produce a scrolling animation—for example, in response to a fling gesture. Scrollers track scroll offsets for you over time, but they don't automatically apply those positions to your view. It's your responsibility to get and apply new coordinates at a rate that will make the scrolling animation look smooth.
使用案例
官方案例
private Scroller mScroller = new Scroller(context);
...
public void zoomIn() {
// 还原正在进行的任何动画
mScroller.forceFinished(true);
// 提供X和Y轴的起点和滑动距离开始滚动
// startScroll有一个5个参数的重载方法,最后一个参数是滚动持续时间
mScroller.startScroll(0, 0, 100, 0);
// 请求重绘
invalidate();
}
基本用法
private Scroller mScroller = new Scroller(mContext);
private void smoothScroll(int destX, int destY) {
// 获取当前x轴位置
int scrollX = getScrollX();
// 计算x轴相对于当前位置发生的偏移量
int deltaX = destX - scrollX;
// 发生滚动,x轴从当前位置滚动的位移量为deltaX
mScroller.startScroll(scrollX, 0, deltaX, 0, 500);
// 强制重绘
invalidate();
}
@Override
public void computeScroll() {
super.computeScroll();
// 滑动是否终止
if (mScroller.computeScrollOffset()) {
// 重置x和y轴位置
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
// 强制重绘
invalidate();
}
}
computeScroll:准备滑动在View中是空实现,需要我们自己去重写这个方法重置x和y坐标
/**
* 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() {
}
当我们要跟踪X/y轴偏移量时
To track the changing positions of the x/y coordinates, use
{@link #computeScrollOffset}. The method returns a boolean to indicate
whether the scroller is finished. If it isn't, it means that a fling or
programmatic pan operation is still in progress. You can use this method to
find the current offsets of the x and y coordinates, for example:
// 判断滚动是否完成,完成返回true
if (mScroller.computeScrollOffset()) {
// 获取当前的X坐标
int currX = mScroller.getCurrX();
// 获取当前的Y坐标
int currY = mScroller.getCurrY();
...
}