国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

詳解Android的Splash啟動圖的兩種動態(tài)切換方式

2019-12-12 02:35:30
字體:
來源:轉載
供稿:網友

冷啟動的時候因為要考慮網路原因,默認顯示一張本地圖片。

熱啟動的時候會根據(jù)獲取的啟動圖是否是新動態(tài)替換。

以下是實現(xiàn)動態(tài)替換的兩種方式:

Glide的緩存下載

Glide中的downloadOnly方法可實現(xiàn)圖片的下載功能

圖片下載

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并統(tǒng)一了Interceptor攔截器,在用到下載圖片,所以就單獨提出來了。

  1. 創(chuàng)建一個service,并在配置文件AndroidManifest.xml中注冊
  2. 在獲取到圖片地址之后startService(),并傳遞到service中
  3. 在service的onStartCommand()方法中獲取到圖片地址,并創(chuàng)建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();        }      });

獲取到的圖片重新命名再進行顯示。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥化市| 泸州市| 亚东县| 富锦市| 汉源县| 阿尔山市| 武安市| 嵊泗县| 巩义市| 白水县| 长宁区| 北流市| 康保县| 安平县| 武山县| 江达县| 长兴县| 宁国市| 越西县| 广河县| 都兰县| 陈巴尔虎旗| 丰顺县| 谷城县| 嘉祥县| 绵竹市| 余姚市| 南江县| 兴和县| 贵州省| 加查县| 株洲市| 茶陵县| 迭部县| 文成县| 静安区| 伊吾县| 宣化县| 兴国县| 潢川县| 左云县|