Reputation: 85
Im trying to animate my horiontal scrollView. At the moment its going from left to right perfectly with:
HorizontalScrollView headband = findViewById(R.id.scroll);
ObjectAnimator.ofInt(headband, "scrollX", 2000).setDuration(10000).start();
Now I want two more things:
Upvotes: 0
Views: 953
Reputation: 245
You can use ObjectAnimator
's repeatMode
to get it to repeat in the opposite direction and repeatCount
to get it to continue indefinitely. You can get the scrollable size of your ScrollView
by calculating scrollView[0].width - scrollView.width
. Here's how it would look in Kotlin with your variables:
ObjectAnimator.ofInt(headband, "scrollX", 0, headband[0].width - headband.width).apply {
repeatMode = ObjectAnimator.REVERSE
repeatCount = Animation.INFINITE
duration = 10000
start()
}
If you're executing this somewhere where the width of the views is not known yet, like in onCreate()
, them wrap it all in headband.doOnLayout { <code> }
.
Upvotes: 0
Reputation: 85
After weeks of struggle here is the solution ! Hope it'll be useful to someone.
HorizontalScrollView headband = findViewById(R.id.scroll);
animator1 = ObjectAnimator.ofInt(headband, "scrollX", 1700).setDuration(10000);
animator2 = ObjectAnimator.ofInt(headband, "scrollX", 0).setDuration(10000);
animator1.start();
animator1.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
animator2.start();
}
});
animator2.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
animator1.start();
}
});
Upvotes: 1