There is a question, in fact, this is just a special case in order to clarify my question. Recently I am studying Studio and have repeatedly come across a situation where I need to change a parameter from xml. I didn’t find anything sensible on the forums, so I somehow solved this case by dancing with a tambourine, because there is little knowledge yet. And so it came to my animations, on the forum of 2012 I read that it is better to do frame-by-frame animation via xml, although, perhaps, the situation has changed. That's what's in my xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/left0" android:duration="250"/> <item android:drawable="@drawable/left1" android:duration="250"/> <item android:drawable="@drawable/left2" android:duration="250"/> <item android:drawable="@drawable/left3" android:duration="250"/> <item android:drawable="@drawable/left4" android:duration="250"/> <item android:drawable="@drawable/left_full" android:duration="250"/> 

This is a simple looped image, in the code I get my animation like this:

 ImageView animLeft = (ImageView)findViewById(R.id.animLeft); animLeft.setBackgroundResource(R.drawable.animleft); mAnimationDrawable = (AnimationDrawable) animLeft.getBackground(); mAnimationDrawable.start(); 

Everything works, everything is beautiful. And here's the question: how can I change the duration of the animation. In xml it is equal to 250ms. Can I somehow change these values ​​from the code? If so, how? If not, then tell me, how is it better to create a frame-by-frame animation?

UPD Rewrote everything, did it so that you can change it in the code where the duration is declared earlier. But still interested in the ability to edit xml directly from the code, if possible

 private void startFrameAnimation() { BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable( R.drawable.left0); BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable( R.drawable.left1); BitmapDrawable frame3 = (BitmapDrawable) getResources().getDrawable( R.drawable.left2); BitmapDrawable frame4 = (BitmapDrawable) getResources().getDrawable( R.drawable.left3); BitmapDrawable frame5 = (BitmapDrawable) getResources().getDrawable( R.drawable.left4); BitmapDrawable frame6 = (BitmapDrawable) getResources().getDrawable( R.drawable.left_full); mAnimationDrawable = new AnimationDrawable(); mAnimationDrawable.setOneShot(false); mAnimationDrawable.addFrame(frame1, DURATION); mAnimationDrawable.addFrame(frame2, DURATION); mAnimationDrawable.addFrame(frame3, DURATION); mAnimationDrawable.addFrame(frame4, DURATION); mAnimationDrawable.addFrame(frame5, DURATION); mAnimationDrawable.addFrame(frame6, DURATION); manimLeft.setBackground(mAnimationDrawable); if (!mAnimationDrawable.isRunning()) { mAnimationDrawable.setVisible(true, true); mAnimationDrawable.start(); } } 

    1 answer 1

    It is impossible to change the duration of displaying an animation frame already created from resources.
    But you can create such animations not in XML, but in code.

    UPD: the code has already appeared in the question.