Android - 如何在显示gif中的某个帧时收到通知?

时间:2016-07-26 19:22:39

标签: android

我在ImageView中显示动画gif,并在其上显示透明背景TextView。我希望能够根据动画中显示的当前帧动态更改TextView中的文本。有机制可以做到吗?

1 个答案:

答案 0 :(得分: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;
    }