App啟動頁倒計時功能,app啟動倒計時

來源:互聯網
上載者:User

App啟動頁倒計時功能,app啟動倒計時

轉載請註明出處:http://www.cnblogs.com/cnwutianhao/p/6753418.html 

 

範例程式碼採用 RxJava + RxLifecycle + Data-Binding 模式編寫

樣本圖:

話不多說,實現方式如下:

1.匯入依賴庫

① RxJava: Reactive Extensions for the JVM

compile 'io.reactivex:rxjava:1.2.9'compile 'io.reactivex:rxandroid:1.2.1'

② RxLifecycle

compile 'com.trello:rxlifecycle:1.0'compile 'com.trello:rxlifecycle-components:1.0'

③ Data-Binding

dataBinding {    enabled = true}

 

2.代碼編寫(關鍵代碼)

① 自訂介面View

public interface BaseView {    <T> LifecycleTransformer<T> bindToLife();}

 

② 建立一個Helper類,用來進行倒計時操作

public final class RxHelper {    private RxHelper() {        throw new AssertionError();    }    /**     * 倒計時     */    public static Observable<Integer> countdown(int time) {        if (time < 0) {            time = 0;        }        final int countTime = time;        return Observable.interval(0, 1, TimeUnit.SECONDS)                .map(new Func1<Long, Integer>() {                    @Override                    public Integer call(Long increaseTime) {                        return countTime - increaseTime.intValue();                    }                })                .take(countTime + 1)                .subscribeOn(Schedulers.io())                .unsubscribeOn(Schedulers.io())                .subscribeOn(AndroidSchedulers.mainThread())                .observeOn(AndroidSchedulers.mainThread());    }}

 

③ 自訂方法:實現非同步載入

private void init() {    RxHelper.countdown(5)            .compose(this.<Integer>bindToLife())            .subscribe(new Subscriber<Integer>() {                @Override                public void onCompleted() {                    doSkip();                }                @Override                public void onError(Throwable e) {                    doSkip();                }                @Override                public void onNext(Integer integer) {                    mBinding.sbSkip.setText("跳過 " + integer);                }            });}

 

④ 自訂方法:實現跳轉

private void doSkip() {    if (!mIsSkip) {        mIsSkip = true;        finish();        startActivity(new Intent(SplashActivity.this, MainActivity.class));        overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);    }}

 

⑤ 設定主題樣式為全屏

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">    <item name="colorPrimary">@color/colorPrimary</item>    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    <item name="colorAccent">@color/colorAccent</item>    <item name="android:windowFullscreen">true</item></style>

 

樣本Demo下載:App啟動頁倒計時功能

 

關注我的新浪微博,請認準黃V認證,擷取最新安卓開發資訊。

關注科技評論家,領略科技、創新、教育以及最大化人類智慧與想象力!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.