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

冷啟動的時候因?yàn)橐紤]網(wǎng)路原因,默認(rèn)顯示一張本地圖片。
熱啟動的時候會根據(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();                                 
                }                                                        
            }                                                            
        });                                                              
  • 每次啟動的時候去獲取
    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)建一個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)行顯示。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,593評論 25 708
  • 一、簡介 在泰國舉行的谷歌開發(fā)者論壇上,谷歌為我們介紹了一個名叫Glide的圖片加載庫,作者是bumptech。這...
    天天大保建閱讀 7,593評論 2 28
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,993評論 19 139
  • 今天的故事來自一位大連的朋友。 時光飛逝,轉(zhuǎn)眼大學(xué)已經(jīng)過了二分之一。 在盛懷眼里,日子似乎沒變:每周有逃不完的課、...
    念星說閱讀 370評論 0 1
  • 最近看到微信群、企鵝群、各種渠道都說移動開發(fā)現(xiàn)在找工作都很難,前端、后臺相對需求量大一點(diǎn),其實(shí)移動開發(fā)還是需要很多...
    Karma1026閱讀 818評論 0 0