android - How do I play an animation through a button that is already set up to play music? (eclipse) -


as said in title, have set button play specific music, want activate animation existing imageview play along (for case, sheet music piece of music) , revert original imageview if button repressed (stopping music) or when song finished.

the code activity button here:

package net.zachapps.sheetmusicapp;  import android.app.activity; import android.media.mediaplayer; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imageview;  public class eldoradoscore extends activity {  button btneldorado; imageview imgscore; mediaplayer songeldorado; int playing;      @override     protected void oncreate(bundle savedinstancestate) {         // todo auto-generated method stub         super.oncreate(savedinstancestate);         setcontentview(r.layout.eldoradoscore);         btneldorado = (button)findviewbyid(r.id.btneldorado);         btneldorado.setonclicklistener(beldorado);         songeldorado = new mediaplayer();         songeldorado = mediaplayer.create(this, r.raw.eldoradoscore);         playing = 0;     }     button.onclicklistener beldorado = new button.onclicklistener(){          @override         public void onclick(view arg0) {             // todo auto-generated method stub         switch(playing){         case 0:             songeldorado.start();             playing=1;             btneldorado.settext("stop el dorado song");              break;         case 1:             songeldorado.stop();             playing = 0;             btneldorado.settext("start el dorado song");              break;         }          }      }; } 

i have animation saved res/anim/score.xml

<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false">     <item android:drawable="@drawable/eldoradop1" android:duration="2000" />     <item android:drawable="@drawable/eldoradop2" android:duration="2000" />     <item android:drawable="@drawable/eldoradop3" android:duration="2000" />     <item android:drawable="@drawable/eldoradop4" android:duration="2000" />     <item android:drawable="@drawable/eldoradop5" android:duration="2000" />     <item android:drawable="@drawable/eldoradop6" android:duration="2000" />     <item android:drawable="@drawable/eldoradop7" android:duration="2000" />     <item android:drawable="@drawable/eldoradop8" android:duration="2000" />     <item android:drawable="@drawable/eldoradop9" android:duration="2000" />     <item android:drawable="@drawable/eldoradop10" android:duration="2000" />     <item android:drawable="@drawable/eldoradop11" android:duration="2000" /> </animation-list> 

i have tried way activate animation button (with cases), haven't found helpful information when have searched. should here? know there way set want know if possible point of view.

add member variable:

animationdrawable animation; 

add in oncreate method:

imgscore = (imageview)findviewbyid(r.id.imgscore); // assuming that's name imgscore.setbackgroundresource(     r.drawable.your_animation_resource); // don't know resource name animation = (animationdrawable)imgscore.getbackground(); 

add under case 0 in onclicklistener's switch statement:

animation.start(); 

and under case 1:

animation.stop(); 

depending on behavior desire, may want set visibility of imageview in onclicklistener.

edit: side note, should sure call release on mediaplayer @ appropriate time (at least, in ondestroy), , depending on intentions, may need little more sophisticated treatment produce desired behavior.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -