Code Bye

Glide加载GIF

glide加载gif时,怎么监听gif动画播放完成?
本人想要在播放gif动画完成后 添加一些事件
解决方案

8

http://www.jianshu.com/p/0705b74e87a9
你看一下能否可以实现

12

使用最少的框架写了个Demo, 希望能帮到你。假如使用事件总线框架的话,代码会更优雅些~
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.gif_btn)
    Button gifBtn;
    @BindView(R.id.gif_image)
    ImageView gifImage;
    private String gifUrl = "http://www.vaikan" +
            ".com/wordpress/wp-content/uploads/2014/math-gifs/UM4iYce.gif";
    private int duration = 0;
    private Handler handler;
    private static final int WHAT_GIF_FINISH = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        handler = new Handler(getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case WHAT_GIF_FINISH:
                        showToast("动画结束");
                        break;
                }
            }
        };
    }
    @OnClick({R.id.gif_btn, R.id.gif_image})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.gif_btn:
                Glide.with(MainActivity.this)
                        .load(gifUrl)
//                        .asGif()
                        .listener(new RequestListener<String, GlideDrawable>() {
                            @Override
                            public boolean onException(Exception e, String model,
                                                       Target<GlideDrawable> target, boolean
                                                               isFirstResource) {
                                return false;
                            }
                            @Override
                            public boolean onResourceReady(GlideDrawable resource, String model,
                                                           Target<GlideDrawable> target, boolean
                                                                   isFromMemoryCache, boolean
                                                                   isFirstResource) {
                                //计算动画时长
                                GifDrawable drawable = (GifDrawable) resource;
                                GifDecoder decoder = drawable.getDecoder();
                                for (int i = 0; i < drawable.getFrameCount(); i++) {
                                    duration += decoder.getDelay(i);
                                }
                                //发送延时消息,通知动画结束
                                handler.sendEmptyMessageDelayed(WHAT_GIF_FINISH, duration);
                                return false;
                            }
                        })
                        //仅仅加载一次gif动画
                        .into(new GlideDrawableImageViewTarget(gifImage, 1));
                break;
            case R.id.gif_image:
                break;
        }
    }
    public void showToast(String str) {
        Toast.makeText(MainActivity.this, "" + str, Toast.LENGTH_SHORT).show();
    }
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Glide加载GIF!