Countdown to App startup page and countdown to app startup
Reprinted please indicate the source: http://www.cnblogs.com/cnwutianhao/p/6753418.html
The sample code is written in RxJava + RxLifecycle + Data-Binding mode.
Example:
The implementation method is as follows:
1. import 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 countdown
Public final class RxHelper {private RxHelper () {throw new AssertionError ();}/*** countdown */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 ());}}
③ Custom method: Implement Asynchronous Loading
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 ("Skip" + integer );}});}
④ Custom method: Achieve redirection
private void doSkip() { if (!mIsSkip) { mIsSkip = true; finish(); startActivity(new Intent(SplashActivity.this, MainActivity.class)); overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit); }}
⑤ Set the topic style to full screen
<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 download: Countdown to App startup page
Follow my Sina Weibo account and check for the yellow V certification to obtain the latest Android development information.
Focus on science and technology critics, learn about science and technology, innovation, education, and maximize human wisdom and imagination!