App Start Page countdown function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.