您將如何從左向右執行無限平滑的水平滾動(反之亦然)?HorizontalScrollView的無限平滑scrollX
HorizontalScrollView
包含只有一個TextView
元素與很長的文本里面,因此我想滾動該文本而不是用戶。我發現ObjectAnimator
只是一個實現平滑滾動。現在問題是正確地循環它。我發現了兩個緊密的解決方案,雖然如預期他們不工作:
使用
CycleInterpolator()
。無法實現順暢的行爲:ObjectAnimator animRight = ObjectAnimator.ofInt(hScrollVIew, "scrollX", hScrollVIew.getRight()); animRight.setRepeatCount(ValueAnimator.INFINITE); animRight.setInterpolator(new CycleInterpolator(1f)); animRight.setDuration(4000); animRight.setStartDelay(0); animRight.start();
創建2個動畫(左,右),這將調用一個接一個。這個解決方案在第一次動畫之後有奇怪的延遲效果
final ObjectAnimator animRight = ObjectAnimator.ofInt(hScrollVIew, "scrollX", holder.hsvTitleHolder.getRight()); animRight.setDuration(SCROLL_DURATION); final ObjectAnimator animLeft = ObjectAnimator.ofInt(hScrollVIew, "scrollX", 0); animLeft.setDuration(SCROLL_DURATION); animRight.addListener(new Animator.AnimatorListener() { Override public void onAnimationStart(Animator animation) {} @Override public void onAnimationEnd(Animator animation) { hScrollVIew.clearAnimation(); animLeft.start(); } @Override public void onAnimationCancel(Animator animation) {} @Override public void onAnimationRepeat(Animator animation) {} }); animLeft.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) {} @Override public void onAnimationEnd(Animator animation) { hScrollVIew.clearAnimation(); animRight.start(); } @Override public void onAnimationCancel(Animator animation) {} @Override public void onAnimationRepeat(Animator animation) {} }); animRight.start();
如果有人遇到了類似的問題,請分享你的經驗。
參見[這](http://pastebin.com/4xXMPTnq) – pskink
@pskink哇!這就像一個魅力!非常感謝。我一直與它鬥爭了好幾天。您可能需要將此代碼發佈爲答案,以便我可以接受爲答案。 – AnZ