Reprint Please specify source: http://www.cnblogs.com/cnwutianhao/p/6753418.html
The sample code is written in RxJava + rxlifecycle + data-binding mode
Example diagram:
Words do not say much, the implementation of the following way:
1. Importing dependent libraries
①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. Code writing (key code)
① Custom Interface View
Public Interface Baseview { <T> lifecycletransformer<t> bindtolife ();}
② Create a helper class for the countdown operation
Public Final classRxhelper {PrivateRxhelper () {Throw NewAssertionerror (); } /*** Countdown*/ Public StaticObservable<integer> Countdown (intTime ) { if(Time < 0) { time= 0; } Final intCounttime =Time ; returnObservable.interval (0, 1, Timeunit.seconds). Map (NewFunc1<long, integer>() {@Override PublicInteger Call (Long increasetime) {returnCounttime-Increasetime.intvalue (); }}). Take (Counttime+ 1). Subscribeon (Schedulers.io ()). Unsubscribeon (Schedulers.io ()). Subscrib EOn (Androidschedulers.mainthread ()). Observeon (Androidschedulers.mainthread ()); }}
③ Custom Method: Implementing Asynchronous loading
Private voidinit () {Rxhelper.countdown (5). Compose ( This.<integer>Bindtolife ()). Subscribe (NewSubscriber<integer>() {@Override Public voidoncompleted () {doskip (); } @Override Public voidOnError (Throwable e) {doskip (); } @Override Public voidonNext (Integer integer) {mBinding.sbSkip.setText ("Skip" +integer); } });}
④ Custom method: Implement Jump
Private void Doskip () { if (! Misskip) { true; Finish (); StartActivity (new Intent (splashactivity. this, mainactivity. class )); Overridependingtransition (R.anim.hold, r.anim.zoom_in_exit);} }
⑤ set theme styles to fullscreen
<stylename= "Splashtheme"Parent= "Theme.AppCompat.Light.NoActionBar"> <Itemname= "Colorprimary">@color/colorprimary</Item> <Itemname= "Colorprimarydark">@color/colorprimarydark</Item> <Itemname= "Coloraccent">@color/coloraccent</Item> <Itemname= "Android:windowfullscreen">True</Item></style>
Sample demo Download: App launch page countdown function
Follow me on Sina Weibo, please look for the Yellow v certification, get the latest Android development information.
Focus on technology critics, learn about technology, innovation, education and maximizing human intelligence and imagination!
App Start Page countdown function