开发者

Programmatically pressing a imageButton doesn't execute immediately

playTune is called whenever a user selects a button. The problem is the line myButton.setPressed(true); is only called after the tune is played. This defies logic, so I'm wondering whether Android lines up events in an queue? I would like the line myButton.setPressed(true); to be called as it appears in the code. i.e. before a note is played.

private void playTune() {

  isTunePlaying = true;

  //Get no of notes user selected
  String selectedValue = noteCountValues[noteCountIdx];
  Log.v(LOG_TAG, "selectedValue:"+selectedValue+" noteCountIdx:"+noteCountIdx);
  int noOfNotes;
  if ("ALL".equals(selectedValue)){
   noOfNotes = 50;
  }
  else{
   noOfNotes = Integer.parseInt(selectedValue);
  }

  TuneManager tuneManager = TuneManager.getInstance(this);
  Log.v(LOG_TAG, "tuneNamesIdx:"+tuneNamesIdx);   
  Tune tune = tuneManager.getTune(tuneNamesIdx+1);

  Log.v(LOG_TAG, " tuneTitle:"+tune.getTitle()+" tuneNoOfNotes:"+tune.getNotes().size());

  //Initialise expectedNotes
  expectedNotes = new StringBuffer();

  //Get notes and play
  List<Note> notes = tune.getNotes();  
  //for (Note note:tune.getNotes()){
  for (int i=1; i<=notes.size() && i<=noOfNotes; i++){

   Log.v(LOG_TAG, "i:"+i+" notesSize:"+notes.size()+" noOfNotes:"+noOfNotes);   

   //Highlight note
   if (isHighlightNotesOn){    
    final View myButton = (ImageButton) findViewById(R.id.middle_c);    
    myButton.setPressed(true);       
   }    

   Note note = notes.get(i-1);
   int notePos = soundManager.getNotePosition(note);
   Log.v(LOG_TAG, "current note:"+note.getName()+" playing notePos:"+notePos+" setting duration:"+note.getDurationMS());   
   soundManager.playSound(notePos);

   //Add to expectedNotes
   expectedNotes.append(" "+note.getName()+",");   

   //Sleep for the duration
   try{
    Thread.currentThread().sleep(note.getDurationMS());//sleep for 1000 ms
   }
  开发者_C百科 catch(InterruptedException ie){

   }
  }

  isTunePlaying = false;  

  //Initialise actualNotesPlayed i.e. start from after the tine has finished
  actualNotesPlayed = new StringBuffer();     
} 


Not sure if this is what you are running into, but if you cause an event from something event driven (happening on the UI thread) it of course cannot be processed on the UI thread until after your current event handler returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜