2016-05-11 72 views
-1

您將如何從左向右執行無限平滑的水平滾動(反之亦然)?Horizo​​ntalScrollView的無限平滑scrollX

HorizontalScrollView包含只有一個TextView元素與很長的文本里面,因此我想滾動該文本而不是用戶。我發現ObjectAnimator只是一個實現平滑滾動。現在問題是正確地循環它。我發現了兩個緊密的解決方案,雖然如預期他們不工作:

  1. 使用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. 創建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(); 
    

如果有人遇到了類似的問題,請分享你的經驗。

+1

參見[這](http://pastebin.com/4xXMPTnq) – pskink

+0

@pskink哇!這就像一個魅力!非常感謝。我一直與它鬥爭了好幾天。您可能需要將此代碼發佈爲答案,以便我可以接受爲答案。 – AnZ

回答

1

使用這樣的代碼:

final HorizontalScrollView hsv = new HorizontalScrollView(this); 
    final TextView tv = new TextView(this); 
    tv.setTextSize(48); 
    tv.setText("Our evil heaven for living is to yearn others agreeable."); 
    hsv.addView(tv); 
    setContentView(hsv); 

    View.OnClickListener ocl = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ObjectAnimator animRight = ObjectAnimator.ofInt(hsv, "scrollX", 0, tv.getWidth() - hsv.getWidth()); 
      animRight.setRepeatCount(3); 
      // you could use CycleInterpolator(0.5f) but the 
      // effect with CycleInterpolator is not so smooth 
      // so use that custom Interpolator 
      animRight.setInterpolator(new Interpolator() { 
       @Override 
       public float getInterpolation(float input) { 
        return (float) Math.pow(Math.sin(Math.PI * input), 2); 
// you could also use similar interpolation: 
//     return (float) (1 - Math.cos(2 * Math.PI * input))/2; 
       } 
      }); 
      animRight.setDuration(4000); 
      animRight.start(); 
     } 
    }; 
    tv.setOnClickListener(ocl);