Implementation of Android Simple timer

Source: Internet
Author: User

This is a simple timer for 3s,5s and 10S, respectively. Countdown process, the interface will display numbers, the number of display with some gradient animation, the effect looks good.

This program is written by itself, so just do it in the UI thread, and the actual use of the project must always be perfected.

The XML code is as follows:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "Vertical" tools:context= ". Mainactivity "android:background=" @android: Color/background_dark "> <linearlayout android:layout_width=            "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <Button Android:id= "@+id/timer_three_second" android:layout_width= "Wrap_content" android:layout_he            ight= "Wrap_content" android:layout_weight= "1" android:text= "3S Countdown"/> <button Android:id= "@+id/timer_five_second" android:layout_width= "wrap_content" android:layout_height= "wrap _content "Android:layout_weight= "1" android:text= "5S Countdown"/> <button android:id= "@+id/timer_ten_seco nd "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layo ut_weight= "1" android:text= "10S Countdown"/> </LinearLayout> <linearlayout android:id= "@+id/t Imer_root_view "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:grav Ity= "Center_horizontal" android:layout_margintop= "100DP" > </linearlayout></linearlayout></spa N></span>
The Java code is as follows:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >package Com.jackie.timer;import Android.os.handler;import Android.os.message;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.util.log;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.animation.alphaanimation;import Android.view.animation.animation;import Android.view.animation.animationset;import Android.view.animation.scaleanimation;import Android.widget.Button; Import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.toast;public class    Mainactivity extends Actionbaractivity implements View.onclicklistener {private LinearLayout mtimerrootview;    Private Button mthreebtn;    Private Button mfivebtn;    Private Button mtenbtn;    Private ImageView Mtimerview;    Private Animationset Manimationset;    Used to record the countdown time private int mcurrentvalue = 0; PrivAte final int second_1 = 1000;            /** * @fields Timer_icon_ids:todo Countdown Event Picture */public static final int[] Timer_icon_ids = new int[]{ 0,//starting from 1, no 0 r.drawable.camera_timer_icon_1, r.drawable.camera_timer_icon_2, r.drawable . Camera_timer_icon_3, R.drawable.camera_timer_icon_4, R.drawable.camera_timer_icon_5, R.D            Rawable.camera_timer_icon_6, r.drawable.camera_timer_icon_7, R.drawable.camera_timer_icon_8,    R.drawable.camera_timer_icon_9, R.drawable.camera_timer_icon_10,};    private static final int msg_update_timer_value = 0; Private Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {SWI                    TCH (msg.what) {case msg_update_timer_value:remindtimevalue ();            Break    }        }    }; @Override protected void OnCreate (Bundle SavedinstanCestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Mtimerrootview = (linearlayout) Findviewbyid (R.id.timer_root_view);        MTHREEBTN = (Button) Findviewbyid (R.id.timer_three_second);        MFIVEBTN = (Button) Findviewbyid (R.id.timer_five_second);        MTENBTN = (Button) Findviewbyid (R.id.timer_ten_second);        Mthreebtn.setonclicklistener (this);        Mfivebtn.setonclicklistener (this);    Mtenbtn.setonclicklistener (this); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.menu_main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent Activity in Androidmanifest.xml.        int id = item.getitemid ();        Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item);                } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.timer_three_second:                Mcurrentvalue = 3;            Break                Case r.id.timer_five_second:mcurrentvalue = 5;            Break                Case R.id.timer_ten_second:mcurrentvalue = 10;        Break    } mhandler.sendemptymessage (Msg_update_timer_value); } private void Remindtimevalue () {if (mcurrentvalue <= 0) {Toast.maketext (this, "Countdown end", TOAST.L        Ength_long). Show ();            } else {//update interface Showtimerremind ();            mcurrentvalue--;        Each 1S Resend message mhandler.sendemptymessagedelayed (Msg_update_timer_value, second_1); }} Private void Showtimerremind () {Runonuithread (new Runnable () {@Override public void run () {                Create a countdown display of view Creattimerview ();                Create animation initanimation (); if (Mcurrentvalue > 0 && mcurrentvalue < timer_icon_ids.length) {//"description of function" This method is used to set an animation effect to finish executing                    , the View object remains in the terminating position.                    The execution of this method requires that the Setfillenabled method be used first to enable the fill effect, otherwise the setting is invalid.                    Manimationset.setfillafter (TRUE);                    Mtimerview.setimagedrawable (Getdrawable (Timer_icon_ids[mcurrentvalue));                    Mtimerview.setvisibility (view.visible);                Mtimerview.startanimation (Manimationset);    }            }        }); } private void Creattimerview () {if (Mtimerview = = null) {Mtimerview = new ImageView (mainactivity.th            IS);        Mtimerrootview.addview (Mtimerview); }} private void Initanimation () {ScaleaNimation Mscale = new Scaleanimation (1.0f, 0.5f,//x from 1 to 0.5 1.0f, 0.5f,        Y from 1 to 0.5 animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);        Alphaanimation Malpha = new Alphaanimation (0f, 1f);        Manimationset = new Animationset (true);        Manimationset.addanimation (Mscale);        Manimationset.addanimation (Malpha);        Manimationset.setduration (600); Manimationset.setanimationlistener (New Animation.animationlistener () {@Override public void Onanima Tionstart (Animation Animation) {if (Mtimerview! = null) {mtimerview.setvisibility (view.i                    nvisible);                Manimationset.setfillafter (FALSE); }} @Override public void Onanimationend (Animation Animation) {} @     Override public void Onanimationrepeat (Animation Animation) {       }        });    Manimationset.setfillafter (TRUE); }}</span></span>
The effect is as follows:


Because there is a gradual change in the number of the process, so not very good, the specific effect can see the real machine.

Second, this program does not take into account some cases, for example, in the countdown process, it is not allowed to click the button and so on.

Implementation of Android Simple timer

Related Article

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.