转载请注明出处: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认证,获取最新安卓开发资讯。
关注科技评论家,领略科技、创新、教育以及最大化人类智慧与想象力!
时间: 2024-12-24 22:57:36