Effect chart, every 1 seconds, change the time
Xml:
Copy Code code as follows:
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity "
Android:background= "#000000"
>
<com.gettime.customdigitalclock
Android:id= "@+id/time"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_gravity= "Center"
android:gravity= "Center"
Android:textcolor= "#FF0000"
Android:textsize= "45SP"/>
</RelativeLayout>
The top <com.gettime.customdigitalclock is the full position of the class
Mainactivity.java
Package com.gettime;
Import Android.os.Bundle;
Import android.app.Activity;
public class Mainactivity extends activity {
Private Customdigitalclock Timeclock;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
timeclock= (Customdigitalclock) Findviewbyid (r.id.time);
Timeclock.setendtime (System.currenttimemillis () +30*1000*24*24);
}
}
Customdigitalclock.java
/*
* Copyright (C) the * Project
* All right reserved.
* Version 1.00 2012-2-11
* Author veally@foxmail.com
*/
Package com.gettime;
Import Java.util.Calendar;
Import Android.content.Context;
Import Android.database.ContentObserver;
Import Android.os.Handler;
Import Android.os.SystemClock;
Import android.provider.Settings;
Import android.text.Html;
Import android.text.Spannable;
Import android.text.Spanned;
Import Android.text.style.AbsoluteSizeSpan;
Import Android.util.AttributeSet;
Import Android.widget.DigitalClock;
/**
* Custom Digital Clock
*
* @author veally@foxmail.com
*/
public class Customdigitalclock extends DigitalClock {
Calendar Mcalendar;
Private final static String M12 = "h:mm AA";
Private final static String M24 = "k:mm";
Private Formatchangeobserver Mformatchangeobserver;
Private Runnable Mticker;
Private Handler Mhandler;
Private long Endtime;
public static long Distancetime;
Private Clocklistener Mclocklistener;
private static Boolean Isfirst;
Private Boolean mtickerstopped;
@SuppressWarnings ("unused")
Private String Mformat;
Public Customdigitalclock {
Super (context);
Initclock (context);
}
Public Customdigitalclock (context context, AttributeSet Attrs) {
Super (context, attrs);
Initclock (context);
}
private void Initclock (context context) {
if (Mcalendar = = null) {
Mcalendar = Calendar.getinstance ();
}
Mformatchangeobserver = new Formatchangeobserver ();
GetContext (). Getcontentresolver (). Registercontentobserver (
Settings.System.CONTENT_URI, True, mformatchangeobserver);
SetFormat ();
}
@Override
protected void Onattachedtowindow () {
mtickerstopped = false;
Super.onattachedtowindow ();
Mhandler = new Handler ();
/**
* Requests a tick on the next hard-second boundary
*/
Mticker = new Runnable () {
public void Run () {
if (mtickerstopped)
Return
Long currenttime = System.currenttimemillis ();
if (currenttime/1000 = = endtime/1000-5 * 60) {
Mclocklistener.remainfiveminutes ();
}
Distancetime = Endtime-currenttime;
Distancetime/= 1000;
if (Distancetime = = 0) {
SetText ("00:00:00");
Ondetachedfromwindow ();
else if (Distancetime < 0) {
SetText ("00:00:00");
Ondetachedfromwindow ();
} else {
SetText (DealTime (distancetime));
}
Invalidate ();
Long now = Systemclock.uptimemillis ();
Long next = Now + (1000-now% 1000);
Mhandler.postattime (Mticker, next);
}
};
Mticker.run ();
}
/**
* Deal Time string
*
* @param time
* @return
*/
public static spanned dealtime (long) {
spanned str;
StringBuffer returnstring = new StringBuffer ();
Long day = time/(24 * 60 * 60);
Long hours = (time% (24 * 60 * 60))/(60 * 60);
Long minutes = ((Time% (24 * 60 * 60))% (60 * 60))/60;
Long second = ((Time% (24 * 60 * 60))% (60 * 60))% 60;
String daystr = string.valueof (day);
String hoursstr = Timestrformat (string.valueof (hours));
String minutesstr = Timestrformat (string.valueof (minutes));
String secondstr = Timestrformat (string.valueof (second));
Returnstring.append (DAYSTR). Append ("Days"). Append (Hoursstr). Append ("hours")
. Append (Minutesstr). Append ("Minutes"). Append (Secondstr). Append ("seconds");
str = html.fromhtml (returnstring.tostring ());
if (day >= 10) {
((spannable) str). Setspan (New Absolutesizespan (16), 2, 3,
spannable.span_exclusive_exclusive);
((spannable) str). Setspan (New Absolutesizespan (16), 5, 7,
spannable.span_exclusive_exclusive);
((spannable) str). Setspan (New Absolutesizespan (16), 9, 11,
spannable.span_exclusive_exclusive);
((spannable) str). Setspan (New Absolutesizespan (16), 13, 14,
spannable.span_exclusive_exclusive);
} else {
((spannable) str). Setspan (New Absolutesizespan (16), 1, 2,
spannable.span_exclusive_exclusive);
((spannable) str). Setspan (New Absolutesizespan (16), 4, 6,
spannable.span_exclusive_exclusive);
((spannable) str). Setspan (New Absolutesizespan (16), 8, 10,
spannable.span_exclusive_exclusive);
((spannable) str). Setspan (New Absolutesizespan (16), 12, 13,
spannable.span_exclusive_exclusive);
}
return returnstring.tostring ();
return str;
}
/**
* Format time
*
* @param timestr
* @return
*/
private static string Timestrformat (String timestr) {
Switch (Timestr.length ()) {
Case 1:
Timestr = "0" + timestr;
Break
}
return timestr;
}
@Override
protected void Ondetachedfromwindow () {
Super.ondetachedfromwindow ();
Mtickerstopped = true;
}
/**
* Clock end time is on.
*
* @param endtime
*/
public void Setendtime (long endtime) {
This.endtime = Endtime;
}
/**
* Pulls 12/24 mode from system settings
*/
Private Boolean Get24hourmode () {
Return Android.text.format.DateFormat.is24HourFormat (GetContext ());
}
private void SetFormat () {
if (Get24hourmode ()) {
Mformat = M24;
} else {
Mformat = M12;
}
}
Private class Formatchangeobserver extends Contentobserver {
Public Formatchangeobserver () {
Super (New Handler ());
}
@Override
public void OnChange (Boolean selfchange) {
SetFormat ();
}
}
public void Setclocklistener (Clocklistener clocklistener) {
This.mclocklistener = Clocklistener;
}
Public interface Clocklistener {
void Timeend ();
void Remainfiveminutes ();
}
}