2011-12-27 22 views

Respuesta

-1
whichScreen = Math.max(0, Math.min(whichScreen, getBase().getDocumentModel().getPageCount()-1)); 
mNextScreen = whichScreen; 
final int newX = whichScreen * getWidth(); 
final int delta = newX - getScrollX(); 
//scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2); 
scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta)); 
invalidate(); 
10

Pruebe el siguiente código:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
{ 
    ValueAnimator realSmoothScrollAnimation = 
     ValueAnimator.ofInt(parentScrollView.getScrollY(), targetScrollY); 
    realSmoothScrollAnimation.setDuration(500); 
    realSmoothScrollAnimation.addUpdateListener(new AnimatorUpdateListener() 
    { 
     @Override 
     public void onAnimationUpdate(ValueAnimator animation) 
     { 
      int scrollTo = (Integer) animation.getAnimatedValue(); 
      parentScrollView.scrollTo(0, scrollTo); 
     } 
    }); 

    realSmoothScrollAnimation.start(); 
} 
else 
{ 
    parentScrollView.smoothScrollTo(0, targetScrollY); 
} 
+0

Esto se debe elegir como respuesta a la pregunta. +1 –

+0

En lugar de utilizar ValueAnimator, ¿por qué no utilizar ObjectAnimator (una subclase de ValueAnimator)? Lo hace aún más fácil: un trazador de líneas, de hecho: ObjectAnimator.ofInt (scrollView, "scrollY", targetScrollY) .setDuration (500) .start(); –

2

Ésta es la forma en que he logrado un desplazamiento vertical suave (como créditos de películas). Esto también le permite al usuario mover el scroll hacia arriba y hacia abajo y le permite continuar desplazándose cuando lo suelta. En mi XML, encapsulé mi TextView dentro de ScrollView llamado "scrollView1". ¡Disfrutar!

final TextView tv=(TextView)findViewById(R.id.lyrics); 
    final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1); 
    Button start = (Button) findViewById(R.id.button_start); 
    Button stop = (Button) findViewById(R.id.button_stop); 
    final Handler timerHandler = new Handler(); 
    final Runnable timerRunnable = new Runnable() { 
     @Override 
     public void run() { 
      scrollView.smoothScrollBy(0,5);   // 5 is how many pixels you want it to scroll vertically by 
      timerHandler.postDelayed(this, 10);  // 10 is how many milliseconds you want this thread to run 
     } 
    }; 

    start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      timerHandler.postDelayed(timerRunnable, 0); 
     } 
    }); 

    stop.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      timerHandler.removeCallbacks(timerRunnable); 
     } 
    }); 
+0

¿Cómo sabré que el desplazamiento llegó al final y el ejecutable debe estar apagado? ¿Cómo manejar esto? – AlexKost

111

La respuesta simple es sólo para reemplazar

scrollView.smoothScrollTo(0, scrollTo); 

con

ObjectAnimator.ofInt(scrollView, "scrollY", scrollTo).setDuration(duration).start(); 

donde la duración es el tiempo en milisegundos que desea que se lleve

+3

Esta es definitivamente la mejor respuesta, muy agradable. – aikmanr

+0

solución elegante, bestia! –

+0

¡Hermosa solución! – Simas

Cuestiones relacionadas