詳解Android的Splash啟動(dòng)圖的兩種動(dòng)態(tài)切換方式
冷啟動(dòng)的時(shí)候因?yàn)橐紤]網(wǎng)路原因,默認(rèn)顯示一張本地圖片。
熱啟動(dòng)的時(shí)候會(huì)根據(jù)獲取的啟動(dòng)圖是否是新動(dòng)態(tài)替換。
以下是實(shí)現(xiàn)動(dòng)態(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();
}
}
});
每次啟動(dòng)的時(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í)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)之splash界面下詳解及實(shí)例
- Android中Splash應(yīng)用啟動(dòng)白屏問題的解決方法
- Android Splash界面白屏、黑屏問題的解決方法
- android實(shí)現(xiàn)Splash閃屏效果示例
- 詳解Android中App的啟動(dòng)界面Splash的編寫方法
- Android開發(fā)基礎(chǔ)之創(chuàng)建啟動(dòng)界面Splash Screen的方法
- Android開發(fā)筆記之:Splash的實(shí)現(xiàn)詳解
- Android筆記之:App應(yīng)用之啟動(dòng)界面SplashActivity的使用
相關(guān)文章
Android開發(fā)中Intent.Action各種常見的作用匯總
今天小編就為大家分享一篇關(guān)于Android開發(fā)中Intent.Action各種常見的作用匯總,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android 仿微信發(fā)動(dòng)態(tài)九宮格拖拽、刪除功能
這篇文章主要介紹了Android 仿微信發(fā)動(dòng)態(tài)九宮格拖拽、刪除功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android實(shí)現(xiàn)平滑翻動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)平滑翻動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
phonegap教程使用jspdf庫在應(yīng)用中生成pdf文件(pdf生成方法)
在PhoneGap應(yīng)用中生成pdf文件,實(shí)現(xiàn)起來很簡單,使用JSPDF這個(gè)標(biāo)準(zhǔn)的JavaScript類庫來實(shí)現(xiàn)這個(gè)功能2014-01-01
Android開發(fā)自定義短信驗(yàn)證碼實(shí)現(xiàn)過程詳解
這篇文章主要為大家介紹了Android開發(fā)自定義短信驗(yàn)證碼實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

