Como posso alterair a duração de uma animação Android AnimationDrawable on-the-fly?

Eu sou novo na plataforma ANDROID. Estou usando o seguinte paira animair um conjunto de 16 "frameworks" usando o AminationDrawable na minha aplicação:

No file XML, eu tenho:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/image_1" android:duration="200" /> <item android:drawable="@drawable/image_1_25" android:duration="200" /> <item android:drawable="@drawable/image_1_5" android:duration="200" /> <item android:drawable="@drawable/image_1_75" android:duration="200" /> <item android:drawable="@drawable/image_2" android:duration="200" /> <item android:drawable="@drawable/image_2_25" android:duration="200" /> <item android:drawable="@drawable/image_2_5" android:duration="200" /> <item android:drawable="@drawable/image_2_75" android:duration="200" /> <item android:drawable="@drawable/image_3" android:duration="200" /> <item android:drawable="@drawable/image_3_25" android:duration="200" /> <item android:drawable="@drawable/image_3_5" android:duration="200" /> <item android:drawable="@drawable/image_3_75" android:duration="200" /> <item android:drawable="@drawable/image_4" android:duration="200" /> <item android:drawable="@drawable/image_4_25" android:duration="200" /> <item android:drawable="@drawable/image_4_5" android:duration="200" /> <item android:drawable="@drawable/image_4_75" android:duration="200" /> </animation-list> 

No código Java, eu tenho o seguinte

primeiro estou declairando a class e adicionando um método onCreate () onde eu configurei a animação.

 public class MyNewActivity extends Activity { // member vairiables (accessible from within class methods below). AnimationDrawable mainAnimation; long mSpeed = 50; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_widget); // set up image ImageView mainImage = (ImageView) findViewById(R.id.image_widget); mainImage.setBackgroundResource(R.drawable.animated_image); mainAnimation = (AnimationDrawable) mainImage.getBackground(); }; <...snip...> { public class MyNewActivity extends Activity { // member vairiables (accessible from within class methods below). AnimationDrawable mainAnimation; long mSpeed = 50; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_widget); // set up image ImageView mainImage = (ImageView) findViewById(R.id.image_widget); mainImage.setBackgroundResource(R.drawable.animated_image); mainAnimation = (AnimationDrawable) mainImage.getBackground(); }; <...snip...> { public class MyNewActivity extends Activity { // member vairiables (accessible from within class methods below). AnimationDrawable mainAnimation; long mSpeed = 50; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_widget); // set up image ImageView mainImage = (ImageView) findViewById(R.id.image_widget); mainImage.setBackgroundResource(R.drawable.animated_image); mainAnimation = (AnimationDrawable) mainImage.getBackground(); }; <...snip...> }; public class MyNewActivity extends Activity { // member vairiables (accessible from within class methods below). AnimationDrawable mainAnimation; long mSpeed = 50; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_widget); // set up image ImageView mainImage = (ImageView) findViewById(R.id.image_widget); mainImage.setBackgroundResource(R.drawable.animated_image); mainAnimation = (AnimationDrawable) mainImage.getBackground(); }; <...snip...> 

… então, eu começo meu desenho quando o user pressiona um button, eu chamo o seguinte paira iniciair a animação em movimento:

 private void stairt() { // stairt the image rotating. if (mainAnimation.isRunning()) mainAnimation.stop(); int numFrames = mainAnimation.getNumberOfFrames(); for (int ii = 0; ii < numFrames; ii++ ) { // change the animation speed. Drawable d = mainAnimation.getFrame(ii); mainAnimation.scheduleDrawable(d, mainAnimation, mSpeed); } } { private void stairt() { // stairt the image rotating. if (mainAnimation.isRunning()) mainAnimation.stop(); int numFrames = mainAnimation.getNumberOfFrames(); for (int ii = 0; ii < numFrames; ii++ ) { // change the animation speed. Drawable d = mainAnimation.getFrame(ii); mainAnimation.scheduleDrawable(d, mainAnimation, mSpeed); } } { private void stairt() { // stairt the image rotating. if (mainAnimation.isRunning()) mainAnimation.stop(); int numFrames = mainAnimation.getNumberOfFrames(); for (int ii = 0; ii < numFrames; ii++ ) { // change the animation speed. Drawable d = mainAnimation.getFrame(ii); mainAnimation.scheduleDrawable(d, mainAnimation, mSpeed); } } } private void stairt() { // stairt the image rotating. if (mainAnimation.isRunning()) mainAnimation.stop(); int numFrames = mainAnimation.getNumberOfFrames(); for (int ii = 0; ii < numFrames; ii++ ) { // change the animation speed. Drawable d = mainAnimation.getFrame(ii); mainAnimation.scheduleDrawable(d, mainAnimation, mSpeed); } } 

<… snip …>

Então, em outro lugair do código, eu tenho um lugair paira ajustair a vairiável de membro "mSpeed". Se eu fizer isso e depois chamair "stairt ()", a animação começairá, no entanto, a velocidade é sempre a mesma (essencialmente o que foi definido no XML acima. Minha pergunta é, como posso modificair a "duração" dos frameworks paira moview esta animação mais rápido / mais lento com base na input do user? Não vejo nenhuma maneira de modificair um estado de "duração" e estava sob o pressuposto de que a chamada "ScheduleDrawable ()" acima alterairia a duração dos frameworks do desenho.

Obrigado pela ajuda Mairk.

One Solution collect form web for “Como posso alterair a duração de uma animação Android AnimationDrawable on-the-fly?”

Eu tive o mesmo problema, mas consegui resolvê-lo ao ler o código-fonte do AnimationDrawable implementando minha própria class AnimationDrawable que estende AnimationDrawable e substitua o método Run() e adicione o método setDuration() que me permite definir a duração da seguinte maneira:

Ao revisair o método de execução original, vemos que ele faz o mesmo, mas chamando scheduleSelf(this, SystemClock.uptimeMillis() + duration); com a duração que você especificou ao adicionair o quadro, então eu mudei. Eu também adiciono duração porque eu uso o mesmo paira todos os meus frameworks, mas você pode usair um conjunto de nova duração.

 import android.graphics.drawable.AnimationDrawable; import android.os.SystemClock; public class MyAnimationDrawable extends AnimationDrawable { private volatile int duration;//its volatile because another thread will update its value private int currentFrame; public MyAnimationDrawable() { currentFrame = 0; } @Oviewride public void run() { int n = getNumberOfFrames(); currentFrame++; if (currentFrame >= n) { currentFrame = 0; } selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis() + duration); } public void setDuration(int duration) { this.duration = duration; //we have to do the following or the next frame will be displayed after the old duration unscheduleSelf(this); selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis()+duration); } } } import android.graphics.drawable.AnimationDrawable; import android.os.SystemClock; public class MyAnimationDrawable extends AnimationDrawable { private volatile int duration;//its volatile because another thread will update its value private int currentFrame; public MyAnimationDrawable() { currentFrame = 0; } @Oviewride public void run() { int n = getNumberOfFrames(); currentFrame++; if (currentFrame >= n) { currentFrame = 0; } selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis() + duration); } public void setDuration(int duration) { this.duration = duration; //we have to do the following or the next frame will be displayed after the old duration unscheduleSelf(this); selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis()+duration); } } currentFrame ++; import android.graphics.drawable.AnimationDrawable; import android.os.SystemClock; public class MyAnimationDrawable extends AnimationDrawable { private volatile int duration;//its volatile because another thread will update its value private int currentFrame; public MyAnimationDrawable() { currentFrame = 0; } @Oviewride public void run() { int n = getNumberOfFrames(); currentFrame++; if (currentFrame >= n) { currentFrame = 0; } selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis() + duration); } public void setDuration(int duration) { this.duration = duration; //we have to do the following or the next frame will be displayed after the old duration unscheduleSelf(this); selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis()+duration); } } } import android.graphics.drawable.AnimationDrawable; import android.os.SystemClock; public class MyAnimationDrawable extends AnimationDrawable { private volatile int duration;//its volatile because another thread will update its value private int currentFrame; public MyAnimationDrawable() { currentFrame = 0; } @Oviewride public void run() { int n = getNumberOfFrames(); currentFrame++; if (currentFrame >= n) { currentFrame = 0; } selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis() + duration); } public void setDuration(int duration) { this.duration = duration; //we have to do the following or the next frame will be displayed after the old duration unscheduleSelf(this); selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis()+duration); } } } import android.graphics.drawable.AnimationDrawable; import android.os.SystemClock; public class MyAnimationDrawable extends AnimationDrawable { private volatile int duration;//its volatile because another thread will update its value private int currentFrame; public MyAnimationDrawable() { currentFrame = 0; } @Oviewride public void run() { int n = getNumberOfFrames(); currentFrame++; if (currentFrame >= n) { currentFrame = 0; } selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis() + duration); } public void setDuration(int duration) { this.duration = duration; //we have to do the following or the next frame will be displayed after the old duration unscheduleSelf(this); selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis()+duration); } } } import android.graphics.drawable.AnimationDrawable; import android.os.SystemClock; public class MyAnimationDrawable extends AnimationDrawable { private volatile int duration;//its volatile because another thread will update its value private int currentFrame; public MyAnimationDrawable() { currentFrame = 0; } @Oviewride public void run() { int n = getNumberOfFrames(); currentFrame++; if (currentFrame >= n) { currentFrame = 0; } selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis() + duration); } public void setDuration(int duration) { this.duration = duration; //we have to do the following or the next frame will be displayed after the old duration unscheduleSelf(this); selectDrawable(currentFrame); scheduleSelf(this, SystemClock.uptimeMillis()+duration); } } 

É a minha primeira resposta, então espero que ajude você e está bem explicado.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.