Android中怎么动态切换Splash启动图

发布时间:2021-07-12 11:34:32 作者:Leah
来源:亿速云 阅读:224

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

  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();
        }
      });

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

关于Android中怎么动态切换Splash启动图问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. Android 主题切换
  2. 高效启动页(Splash)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android splash

上一篇:php的phpinfo()中Loaded Configuration File(none)怎么办

下一篇:VS2017添加EF的MVC控制器报错怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》