Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll (top to bottom) the ScrollView using animation with adjustable speed?

I'm writing an Activity for reading song lyrics. When the user clicks the play button, the vertical scrolling animation needs to be started with the user's desired speed. The user might be playing a guitar while reading the lyrics on the phone screen. So, he must be able to adjust the scrolling speed according to the tempo. I put the lyrics TextView inside a ScrollView thinking to play an animation with the scrolling. My code:

<ScrollView
     android:id="@+id/scrollView_activity_song"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_above="@+id/footer_activity_song"
     android:layout_below="@id/title_activity_song">

     <TextView
         android:id="@+id/lyrics_activity_song"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginBottom="5dp"
         android:layout_marginEnd="10dp"
         android:layout_marginStart="10dp"
         android:elevation="5dp"
         android:fontFamily="sans-serif"
         android:paddingBottom="5dp"
         android:paddingStart="15dp"
         android:paddingTop="5dp"
         android:textColor="@android:color/black"
         android:textSize="14sp" />
</ScrollView>

I've already tried using the following ways but I didn't get my requirement.

(1) The problem with this one is that it doesn't scroll till the end of the lyrics. It only works within its duration:

btnPlay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        lyrics.startAnimation((Animation) AnimationUtils.loadAnimation(SongActivity.this,R.anim.scroll_animation));
    }
}

The scroll_animation.xml:

<?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromYDelta="100"
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toYDelta="-100" />

(2) With this one, I can't set any speed:

btnPlay.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          scrollView.postDelayed(new Runnable() {
              @Override
              public void run() {
                  scrollView.fullScroll(View.FOCUS_DOWN);
              }
          },100);
      }
});

(3) This one only works within the duration too:

btnPlay.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         ObjectAnimator anim = ObjectAnimator.ofInt(scrollView,"scrollY", scrollView.getBottom());
         anim.setDuration(9000);
         anim.start();
     }
});

If duration is the problem, is there any other way? The user should be able to see the whole lyrics. Only the animation speed must be adjustable.

like image 857
Zin Win Htet Avatar asked Dec 14 '25 15:12

Zin Win Htet


1 Answers

You can use the following code for your purpose:

YOUR_BUTTON.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         ObjectAnimator.ofInt(YOUR_SCROLLVIEW, "scrollY", YOUR_SCROLLVIEW.getBottom()).setDuration(SONG_DURATION).start();
     }
});

You need to find the duration of your song and then scroll the scrollView for as long as the song plays. So, you can replace SONG_DURATION with the duration of your song in milliseconds.

like image 82
Dinux Avatar answered Dec 16 '25 06:12

Dinux