2016-07-26 67 views

回答

1

我找到了答案。我實現了Drawable.ICallback,然後使用AnimationDrawable.GetFrame()來設置我需要的每一幀的回調。

private class MyFragment : DialogFragment, Drawable.ICallback 
{ 
    TextView tv; /* this is later defined in OnCreateDialog */ 
    ... 

    /* Must implement */ 
    public void InvalidateDrawable(Drawable who) 
    { 
     /* As you can see in OnCreateDialog(), this gets called for 
      frames 0 and 56 */ 
     if (iVocabIndex == 0) { 
      tv.Text = "Show this text"; 
      iVocabIndex = 1; 
     } 
     else if (iVocabIndex == 1) { 
      tv.Text = "Show this other text"; 
      iVocabIndex = 0; 
     } 
    } 

    /* Must implement */ 
    public void ScheduleDrawable (Drawable who, IRunnable what, long when) 
    { 
    } 

    /* Must implement */ 
    public void UnscheduleDrawable (Drawable who, IRunnable what) 
    { 
    } 

    public override Dialog OnCreateDialog (Bundle savedInstanceState) 
    { 
     ImageView image = view.FindViewById<ImageView> (Resource.Id.confDialogImage); 
     tv = view.FindViewById<TextView> (Resource.Id.confDialogImageText); 
     image.SetImageResource (Resource.Drawable.my_animation_xml); 
     AnimationDrawable b1Anim = (AnimationDrawable)image.Drawable; 
     b1Anim.Start(); 
     Drawable d = b1Anim.GetFrame(0); 
     d.Callback = this; 
     d = b1Anim.GetFrame(56); 
     d.Callback = this; 
    }