[英]How to animate scroll position? How to scroll smoothly?
I wish to move smoothly to next scroll position in my custom view (on button press or event). 我希望在我的自定义视图(按下按钮或事件)中顺利移动到下一个滚动位置。 How to implement this?
怎么实现这个? First of all I can't see scroll animation class (only alpha, rotate, scale and translate).
首先,我看不到滚动动画类(只有alpha,旋转,缩放和平移)。 Secondly, having animation class, I can't see iterative one (say to scroll 100 pixels rights whatever position we have) only absolute ones (ie to animate from one constant value to another).
其次,有动画类,我看不到迭代的(比如滚动100像素权利,无论我们有什么位置)只有绝对的(即从一个常数值动画到另一个常数值)。
Assuming you are using a ScrollView, does smoothScrollTo(...) work for you? 假设您使用的是ScrollView,smoothScrollTo(...)是否适合您?
http://developer.android.com/reference/android/widget/ScrollView.html#smoothScrollTo%28int,%20int%29 http://developer.android.com/reference/android/widget/ScrollView.html#smoothScrollTo%28int,%20int%29
Using ObjectAnimator
, this is a sample for scrolling to top: 使用
ObjectAnimator
,这是滚动到顶部的示例:
public void scrollToTop() {
int x = 0;
int y = 0;
ObjectAnimator xTranslate = ObjectAnimator.ofInt(mScrollView, "scrollX", x);
ObjectAnimator yTranslate = ObjectAnimator.ofInt(mScrollView, "scrollY", y);
AnimatorSet animators = new AnimatorSet();
animators.setDuration(1000L);
animators.playTogether(xTranslate, yTranslate);
animators.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
animators.start();
}
Animating scroll is done through a combination of using Scroller/OverScroller (to compute the time interpolated values of your scroll offsets), GestureDetectors (to start the scroller object) and the onComputeScroll method of a View (which implicitly is your animation loop). 动画滚动是通过使用Scroller / OverScroller (计算滚动偏移的时间插值), GestureDetectors (启动滚动对象)和View的onComputeScroll方法(隐式地是动画循环)的组合完成的。
The official android docs now have a detailed tutorial on precisely this topic. 官方的android文档现在有一个关于这个主题的详细教程。 http://developer.android.com/training/gestures/scroll.html
http://developer.android.com/training/gestures/scroll.html
See the view_cache_demo sample code to see how to do animated scrolling. 请参阅view_cache_demo示例代码以了解如何进行动画滚动。 It works in 2D, caches complex drawing and also handles fling gestures, but you can simplify all that as necessary.
它适用于2D,缓存复杂绘图并处理拖动手势,但您可以根据需要简化所有操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.