Android的Splash启动图的两种动态切换方式

冷启动的时候因为要考虑网路原因,默认显示一张本地图片。
热启动的时候会根据获取的启动图是否是新动态替换。

以下是实现动态替换的两种方式:

Glide的缓存下载

Glide中的downloadOnly方法可实现图片的下载功能

  • 图片下载
Observable.just(RetrofitHelper.API_BASE_URL + img)                       
        .subscribeOn(Schedulers.newThread())                             
        .subscribe(new Action1<String>() {                               
            @Override                                                    
            public void call(String s) {                                 
                try {                                                    
                    Glide.with(getApplicationContext())                  
                            .load(s)                                     
                            .downloadOnly(720, 1280)                     
                            .get();                                      
                } catch (InterruptedException | ExecutionException e) {  
                    e.printStackTrace();                                 
                }                                                        
            }                                                            
        });                                                              
  • 每次启动的时候去获取
    File file = new File(sp_splash_logo);
    if (file.exists()) {
        Glide.with(getApplicationContext()).load(file).into(mIvSplash);
    } else {
        mIvSplash.setImageResource(R.mipmap.splash);
    }

Retofit+RxJava的本地下载

考虑到项目中用到的client是okhttp并统一了Interceptor拦截器,在用到下载图片,所以就单独提出来了。

  • 创建一个service,并在配置文件AndroidManifest.xml中注册
  • 在获取到图片地址之后startService(),并传递到service
  • 在service的onStartCommand()方法中获取到图片地址,并创建ImgServise开始下载

下载的代码如下

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RetrofitHelper.API_BASE_URL)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    ImgServise imgServise = retrofit.create(ImgServise.class);
    imgServise.downloadPicFromNet(img)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Action1<ResponseBody>() {
                @Override
                public void call(ResponseBody responseBody) {
                    try {
                        long contentLength = responseBody.contentLength();
                        InputStream is = responseBody.byteStream();
                        File file = new File(Environment.getExternalStorageDirectory(), BuildConfig.APPLICATION_ID + "splash.png");
                        FileOutputStream fos = new FileOutputStream(file);
                        BufferedInputStream bis = new BufferedInputStream(is);
                        byte[] buffer = new byte[1024];
                        int len;
                        long sum = 0L;
                        while ((len = bis.read(buffer)) != -1) {
                            fos.write(buffer, 0, len);
                            sum += len;
                            fos.flush();
                            //增加下载进度的获取
                            Log.d("TAG---", sum + "/" + contentLength);
                       }
                      fos.close();
                      bis.close();
                      is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        stopSelf();
                    }
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    stopSelf();
                }
            });

获取到的图片重新命名再进行显示。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,956评论 25 709
  • 一、简介 在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫Glide的图片加载库,作者是bumptech。这...
    天天大保建阅读 12,231评论 2 28
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,259评论 19 139
  • 今天的故事来自一位大连的朋友。 时光飞逝,转眼大学已经过了二分之一。 在盛怀眼里,日子似乎没变:每周有逃不完的课、...
    念星说阅读 2,994评论 0 1
  • 最近看到微信群、企鹅群、各种渠道都说移动开发现在找工作都很难,前端、后台相对需求量大一点,其实移动开发还是需要很多...
    Karma1026阅读 4,212评论 0 0

友情链接更多精彩内容