示例代码采用 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 {
LifecycleTransformer bindToLife();
}
② 创建一个Helper类,用来进行倒计时操作
public final class RxHelper {
private RxHelper() {
throw new AssertionError();
}
/**
* 倒计时
*/
public static Observable countdown(int time) {
if (time < 0) {
time = 0;
}
final int countTime = time;
return Observable.interval(0, 1, TimeUnit.SECONDS)
.map(new Func1() {
@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.bindToLife())
.subscribe(new Subscriber() {
@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);
}
}
⑤ 设置主题样式为全屏
@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
true
关注我的新浪微博,请认准黄V认证,获取最新安卓开发资讯。
关注科技评论家,领略科技、创新、教育以及最大化人类智慧与想象力!
App启动页设计实例与技巧
App启动页,也称闪屏页,最初是为缓解用户等待Web/iOS/Android App数据加载的焦虑情绪而出现,后被设计师巧妙用于品牌文化展示,服务特色介绍以及功能界面熟悉等平台进行设计,被赋予了更加丰 ...
用代码获取APP启动页图片
用代码获取APP启动页图片 源码 - swift // // AppleSystemService.swift // Swift-Animations // // Created by YouXian ...
React-Native App启动页制作(安卓端)