scrollTo、scrollBy、smoothScrollTo和smoothScrollBy
程序员文章站
2022-06-07 13:13:28
...
下面是 scrollTo、scrollBy、smoothScrollTo 和 smoothScrollBy 效果图:
按钮添加的代码如下:
scrollTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.scrollTo(0, 100);
}
});
scrollBy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.scrollBy(0, 100);
}
});
smoothScrollTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.smoothScrollTo(0, 1000);
}
});
smoothScrollBy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.smoothScrollBy(0, 200);
}
});
scrollTo :滑动到指定位置,如上面代码为:
scrollView.scrollTo(0, 100);
X 坐标不动,Y 轴滑动到100 坐标;即向下滑动 100 坐标;如果当前 Y 轴已经位于 100 坐标,则不滑动。
下面是源码描述 :
/**
* {@inheritDoc}
*
* <p>This version also clamps the scrolling to the bounds of our child.
*/
@Override
public void scrollTo(int x, int y) {
// we rely on the fact the View.scrollBy calls scrollTo.
if (getChildCount() > 0) {
View child = getChildAt(0);
x = clamp(x, getWidth() - mPaddingRight - mPaddingLeft, child.getWidth());
y = clamp(y, getHeight() - mPaddingBottom - mPaddingTop, child.getHeight());
if (x != mScrollX || y != mScrollY) {
super.scrollTo(x, y);
}
}
}
scrollBy :相对于之前坐标,滑动指定坐标,如上面的代码为:
scrollView.scrollBy(0, 100);
X 坐标不动,Y 轴滑动至(当前坐标 +100)坐标处;里面其实就是调用 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);
}
smoothScrollTo :平滑的滑动到指定坐标,类似于 scrollTo 方法,如上面代码为:
scrollView.smoothScrollTo(0, 1000);
X 轴坐标不动,Y 轴坐标滑动至 1000 处;
下面是源码描述:
/**
* Like {@link #scrollTo}, but scroll smoothly instead of immediately.
*
* @param x the position where to scroll on the X axis
* @param y the position where to scroll on the Y axis
*/
public final void smoothScrollTo(int x, int y) {
smoothScrollBy(x - mScrollX, y - mScrollY);
}
smoothScrollBy :相对于之前坐标,平滑的滑动指定坐标,类似于 scrollBy 方法,如上面的代码为:
scrollView.smoothScrollBy(0, 200);
X 轴坐标不动,Y 轴滑动至(当前坐标 +200)坐标处。
下面是源码描述:
/**
* Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
*
* @param dx the number of pixels to scroll by on the X axis
* @param dy the number of pixels to scroll by on the Y axis
*/
public final void smoothScrollBy(int dx, int dy) {
if (getChildCount() == 0) {
// Nothing to do.
return;
}
long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
if (duration > ANIMATED_SCROLL_GAP) {
final int height = getHeight() - mPaddingBottom - mPaddingTop;
final int bottom = getChildAt(0).getHeight();
final int maxY = Math.max(0, bottom - height);
final int scrollY = mScrollY;
dy = Math.max(0, Math.min(scrollY + dy, maxY)) - scrollY;
mScroller.startScroll(mScrollX, scrollY, 0, dy);
postInvalidateOnAnimation();
} else {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
if (mFlingStrictSpan != null) {
mFlingStrictSpan.finish();
mFlingStrictSpan = null;
}
}
scrollBy(dx, dy);
}
mLastScroll = AnimationUtils.currentAnimationTimeMillis();
}
上一篇: mysql查询时间段内的数据
下一篇: layuiDate 动态设置时间选择范围