Android中怎么動態切換Splash啟動圖,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
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, 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();
}
});獲取到的圖片重新命名再進行顯示。
關于Android中怎么動態切換Splash啟動圖問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。