当前位置: 移动技术网 > IT编程>移动开发>Android > 详解Android的Splash启动图的两种动态切换方式

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

那一刻 我明白了,天浴李小璐,航空运输协会

冷启动的时候因为要考虑网路原因,默认显示一张本地图片。

热启动的时候会根据获取的启动图是否是新动态替换。

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

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拦截器,在用到下载图片,所以就单独提出来了。

  1. 创建一个service,并在配置文件androidmanifest.xml中注册
  2. 在获取到图片地址之后startservice(),并传递到service中
  3. 在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();
        }
      });

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网