栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

Glide的缓存下载

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

图片下载

Observable.just(RetrofitHelper.API_base_URL + img)     
   .subscribeOn(Schedulers.newThread()) 
   .subscribe(new Action1() {  
     @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() {
 @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() {
 @Override
 public void call(Throwable throwable) {
   stopSelf();
 }
      });

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158297.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号