Home > Enterprise >  How to call one of multiple animations in raw directory
How to call one of multiple animations in raw directory

Time:11-13

I'm trying to use a random number generator to pick between one of 10 different animations to run using a Lottie implementation. I've named the animations animation1 to animation10. When directly inputing one of the animations like this there's no issue:

animationView.setAnimation(R.raw.animation2);

but the app keeps on crashing when inputing it like this:

LottieAnimationView animationView = findViewById(R.id.animationViewer);
randNumber = rand.nextInt(10)   1;

animationView.setAnimation("R.raw.animation"   randNumber);

With the cause being:

Caused by: java.io.FileNotFoundException: R.raw.animation2

CodePudding user response:

You can't do that because setAnimation() requires a ResID and not a string.

Instead, you can use an Array like this

<integer-array name="animations">
    <item>@anim/animation1</item>
    <item>@anim/animation2</item>
</integer-array>

And then, you can use your random number.

CodePudding user response:

you are trying to send a resource id as a string you can create an array of resources or the easiest but not the cleanest way is :

 animID = when(randNumber){

1 -> {R.raw.animation1} 2 -> {R.raw.animation2} . . . . 10 -> {R.raw.animation10} }

animationView.setAnimation(animID);

  • Related