安卓图片缓存通过Glide

最近项目中要做启动图缓存图片,刚开始想的缓存文件,后来尝试用缓存来做找到这种方式来缓存

这种方式不需要指定控件可以多缓存非常好用

废话不多说直接上干货

for (int i = 0; i < imgs.size(); i++) {
            int finalI = i;
            SimpleTarget<GlideDrawable> simpleTarget = new SimpleTarget<GlideDrawable>() {
                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) {
//                            imageView.setImageDrawable(resource);
                    Log.i("测试缓存图片", "-------" + finalI);
                }
            };
            Glide.with(
ThinksnsActivity.this.getApplicationContext()).load(imgs.get(i)).diskCacheStrategy(DiskCacheStrategy.ALL).into(simpleTarget); }

这种方式来进行多缓存值得注意的是上下文需要传getApplicationContext()否则在activity finish的时候会出错

下面看加载的方法、

                        Glide.getPhotoCacheDir(ThinksnsActivity.this);
                        Log.i("inflaAdsData", "有图片缓存加载");
                        //图片加载完成之后监听
                        int finalPostionRandomCptPlanIdeas = postionRandomCptPlanIdeas;
                        Glide.with(ThinksnsActivity.this.getApplicationContext()).load(planIdeasBeans.get(postionRandomCptPlanIdeas).getAttach_info().getAttach_middle()).diskCacheStrategy(DiskCacheStrategy.ALL)
                               .crossFade(1000)
                                .into(new GlideDrawableImageViewTarget(iv_launcher) {
                                    @Override
                                    public void onLoadStarted(Drawable placeholder) {
                                        // 开始加载图片
                                        Log.i("inflaAdsData", "开始加载图片");
                                    }

                                    @Override
                                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                        Log.i("inflaAdsData", "加载图片失败");
                                        jumpToMain();
                                    }

                                    @Override
                                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                                        super.onResourceReady(resource, animation);
                                        //投放成功
                                        displayCallback(modelStartAds.getPlan_id(), planIdeasBeans.get(finalPostionRandomCptPlanIdeas).getIdea_id());
                                        Log.i("inflaAdsData", "加载完开始倒计时");
//                                    iv_launcher.setImageDrawable(resource);
                                        countDown(5100);//5秒倒计时方法
                                        iv_launcher.setOnClickListener(new OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
             
                                                countdowntimer.cancel();
                                             
                                                //ad视频跳转
                                               
                                            }
                                        });
                                    }
                                });

这里面获取缓存然后在加载之后要监听 来实现倒计时 看注释就很明白

by 磊磊tua

 

posted @ 2020-03-30 10:50  WidgetBox  阅读(300)  评论(0编辑  收藏  举报