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

详解Android的Splash启动图的两种动态切换方式,第1张

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

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

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

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

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,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启动图的两种动态切换方式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存