iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >详解Android的Splash启动图的两种动态切换方式
  • 800
分享到

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

动态Android 2022-06-06 11:06:22 800人浏览 泡泡鱼
摘要

冷启动的时候因为要考虑网路原因,默认显示一张本地图片。 热启动的时候会根据获取的启动图是否是新动态替换。 以下是实现动态替换的两种方式: Glide的缓存下载 Glide中的

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

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

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

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

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

您可能感兴趣的文章:Android开发之splash界面下详解及实例Android中Splash应用启动白屏问题的解决方法Android Splash界面白屏、黑屏问题的解决方法android实现Splash闪屏效果示例详解Android中App的启动界面Splash的编写方法Android开发基础之创建启动界面Splash Screen的方法Android开发笔记之:Splash的实现详解Android笔记之:App应用之启动界面SplashActivity的使用


--结束END--

本文标题: 详解Android的Splash启动图的两种动态切换方式

本文链接: https://www.lsjlt.com/news/28206.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作