标签:滑动 int scrollto rri llb cal com ISE class
任何一个控件都是可以滚动的,因为在View类当中有scrollTo()和scrollBy()这两个方法,但使用这两个方法完成的滚动效果是跳跃式的,没有任何平滑滚动的效果.而Scroller正是实现平滑滚动的利器
对于Scroller的startScroll 方法一直不太理解
startScroll
added in API level 9
public void startScroll (int startX,
int startY,
int dx,
int dy)
Start scrolling by providing a starting point and the distance to travel. The scroll will use the default value of 250 milliseconds for the duration.
Parameters
startX int: Starting horizontal scroll offset in pixels. Positive numbers will scroll the content to the left.
startY int: Starting vertical scroll offset in pixels. Positive numbers will scroll the content up.
dx int: Horizontal distance to travel. Positive numbers will scroll the content to the left.
dy int: Vertical distance to travel. Positive numbers will scroll the content up.
例子: 点击Button实现平滑滚动
public class ScrollerButton extends Button {
private OverScroller mScroller;
public ScrollerButton(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new OverScroller(context);
}
@Override
public void computeScroll() {
super.computeScroll();
if (mScroller.computeScrollOffset()) {
((View) getParent()).scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
invalidate();
}
}
public void smoothScrollTo(int destX, int destY) {
int scrollX = getScrollX();
int delta = destX - scrollX;
mScroller.startScroll(scrollX, 0, delta, 0, 2000);
invalidate();
}
}
----
调用部分:
scrollToBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.scrollTo(-60, 0);
}
});
smoothScrollToBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
smoothScrollToBtn.smoothScrollTo(-60, 0);
}
});
由于滚动都是针对内容,要想实现Button整体移动, computeScroll中要调用其父控件的scrollTo方法
为什么会这样呢, 原来是getScrollX()在捣鬼, Button始终没有执行过ScroolTo和ScroolBy方法, 所以getScrollX始终为0. 所以一直重复 0 到 -60
论startScroll的正确用法
public void smoothScrollTo(int destX, int destY) {
int scrollX = ((View) getParent()).getScrollX();
Log.i(TAG, "smoothScrollTo: " + scrollX);
mScroller.startScroll(scrollX, 0, destX - scrollX, 0, 2000);
invalidate();
}
void startScroll(int start, int distance, int duration) {
mFinished = false;
mCurrentPosition = mStart = start;
mFinal = start + distance;
mStartTime = AnimationUtils.currentAnimationTimeMillis();
mDuration = duration;
// Unused
mDeceleration = 0.0f;
mVelocity = 0;
}
标签:滑动 int scrollto rri llb cal com ISE class
原文地址:https://www.cnblogs.com/hixin/p/9491910.html