Analog Clock (AnalogClock)
Displays a surface with a clock and a minute hand
Will change over time.
Common Properties:
Android:dial
You can provide a custom picture for the surface
Below we look directly at the code:
1.Activity
//Analog Clock Public classAnalogclockactivityextendsActivity {PrivateTextView Timetextview; PrivateHandler Handler; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.analog_clock); Timetextview=(TextView) Findviewbyid (R.ID.TIMETEXTVIEWID);//handler mechanism to send messagesHandler =NewHandler () { Public voidhandlemessage (Message msg) {Timetextview.settext (currenttime ()); } };//Set the timeTimetextview.settext (CurrentTime ()); NewCurrenttimethread (handler). Start (); } PrivateString currenttime () {Calendar C=calendar.getinstance (); intYear =C.get (calendar.year); intmonth = C.get (calendar.month) + 1; intDay =C.get (Calendar.day_of_month); inthour =C.get (Calendar.hour_of_day); intminute =C.get (Calendar.minute); intSecond =C.get (Calendar.second); //(Month < 10?) "0" + month:month) Three mesh operation methodString currenttime = year + "-" + (Month < 10?) "0" + month:month) + "-" + (Day < 10?) "0" + day:day) + "" + (Hour < 10?) 0 "+ hour:hour) +": "+ (Minute < 10?) 0 "+ Minute:minute) +": "+ (Second < 10?) 0 "+Second:second); returncurrenttime; }}//turn on a child threadclassCurrenttimethreadextendsThread {PrivateHandler Handler; PublicCurrenttimethread (Handler Handler) { This. Handler =handler; } Public voidrun () { while(true) { Try{Thread.Sleep (1000); Handler.sendemptymessage (1); } Catch(interruptedexception e) {e.printstacktrace (); } } }}
2.xml Layout file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"android:padding= "5DP" > <DigitalClock android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:textsize= "20SP"/><!--analog Clock-<AnalogClock android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:dial= "@drawable/biao_pan"/> <TextView Android:id= "@+id/timetextviewid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:textsize= "20SP"/></linearlayout>
3. Display
Analog Clock (AnalogClock)