詳解Android的Splash啟動圖的兩種動態(tài)切換方式
冷啟動的時(shí)候因?yàn)橐紤]網(wǎng)路原因,默認(rèn)顯示一張本地圖片。
熱啟動的時(shí)候會根據(jù)獲取的啟動圖是否是新動態(tài)替換。
以下是實(shí)現(xiàn)動態(tài)替換的兩種方式:
Glide的緩存下載
Glide中的downloadOnly方法可實(shí)現(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(); } } });
每次啟動的時(shí)候去獲取
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的本地下載
考慮到項(xiàng)目中用到的client是okhttp并統(tǒng)一了Interceptor攔截器,在用到下載圖片,所以就單獨(dú)提出來了。
- 創(chuàng)建一個(gè)service,并在配置文件AndroidManifest.xml中注冊
- 在獲取到圖片地址之后startService(),并傳遞到service中
- 在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(); //增加下載進(jìn)度的獲取 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(); } });
獲取到的圖片重新命名再進(jìn)行顯示。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式(在service和activity中)
這篇文章主要介紹了Android:在service和activity之中,實(shí)現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式,MediaPlayer做音樂播放器采坑以及解決辦法,需要的朋友可以參考下2020-05-05不依賴于Activity的Android全局懸浮窗的實(shí)現(xiàn)
在Android應(yīng)用開發(fā)中,經(jīng)常要遇到做全局懸浮窗的效果,本文的內(nèi)容主要是如何不依賴于Activity的全局懸浮窗的實(shí)現(xiàn)及原理,有需要的可以參考。2016-07-07Android用PopupWindow實(shí)現(xiàn)自定義overflow
這篇文章主要介紹了Android用PopupWindow實(shí)現(xiàn)自定義overflow的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)現(xiàn)隱私政策彈窗與鏈接功能
現(xiàn)在幾乎所有的應(yīng)用市場都要求應(yīng)用上架需要用戶協(xié)議/隱私政策,本篇內(nèi)容將介紹如何在APP內(nèi)植入一個(gè)隱私政策彈窗與鏈接,對Android隱私政策彈窗實(shí)現(xiàn)代碼感興趣的朋友跟隨小編一起看看吧2021-07-07Android 實(shí)現(xiàn)單線程輪循機(jī)制批量下載圖片
這篇文章主要介紹了Android 單線程輪循機(jī)制批量下載圖片的相關(guān)資料,這里對實(shí)現(xiàn)步驟做了詳細(xì)介紹,需要的朋友可以參考下2017-07-07Android Studio實(shí)現(xiàn)登錄功能案例講解
這篇文章主要介紹了Android Studio實(shí)現(xiàn)登錄功能案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08android中ViewPager結(jié)合Fragment進(jìn)行無限滑動
本篇文章中主要介紹了android中ViewPager結(jié)合Fragment進(jìn)行無限滑動,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android Studio下載、安裝和配置+SDK+tools下載(無敵超級詳細(xì)版本)
這篇文章主要介紹了Android Studio下載、安裝和配置+SDK+tools下載(無敵超級詳細(xì)版本),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11