Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : How to change animation speed dynamically?

i have a transition animation xml

<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0%p"
        android:duration="400"/>
</set>

I want to apply this animation on several items but with varying duration. I'm trying to dynamically change duration by calling setDuration and then calling startAnimation.

mSlideLeft.setDuration(400);
view1.startAnimation(mSlideLeft);

mSlideLeft.setDuration(500);
view2.startAnimation(mSlideLeft);

mSlideLeft.setDuration(600);
view3.startAnimation(mSlideLeft);

but all view animation in same duration. How do I dynamically change duration of animation?

like image 827
crysis Avatar asked Dec 09 '15 19:12

crysis


1 Answers

You're using the same Animation object for all three Views. So you're calling setDuration on the same object 3 times.

You need to create three separate Animation objects if you want to use three different durations.

Animation firstSlideLeft = AnimationUtils.loadAnimation(context, R.anim.slide_left);
firstSlideLeft.setDuration(400);
view1.startAnimation(firstSlideLeft);

Animation secondSlideLeft = AnimationUtils.loadAnimation(context, R.anim.slide_left);
secondSlideLeft.setDuration(500);
view2.startAnimation(secondSlideLeft);

Animation thirdSlideLeft = AnimationUtils.loadAnimation(context, R.anim.slide_left);
thirdSlideLeft.setDuration(600);
view3.startAnimation(thirdSlideLeft);
like image 135
Bartek Lipinski Avatar answered Sep 27 '22 19:09

Bartek Lipinski