android從源碼帶你熟悉DigitalClock 數字時針的應用以及它的使用情境
在android如果表示數字時針一般用DigitalClock,這個類是google給我們開發人員提供的一個方便的類實現數字時針的功能,現在就寫個demo,為什麼要講這個類呢?因為我下篇要講ListView item倒計時功能,所以先把這個基礎練習一下,以及它的實現方式,建立android項目digitalClockdemo
布局activity_main.xml檔案
代碼都不用寫 直接運行起來就會有效果
這個數字是變化的 這隻是靜態圖,為什麼代碼沒寫會出來這個效果,所以就得看下他的源碼了
public class DigitalClock extends TextView { Calendar mCalendar; private final static String m12 = h:mm:ss aa; private final static String m24 = k:mm:ss; private FormatChangeObserver mFormatChangeObserver; private Runnable mTicker; private Handler mHandler; private boolean mTickerStopped = false; String mFormat; public DigitalClock(Context context) { super(context); initClock(context); } public DigitalClock(Context context, AttributeSet attrs) { super(context, attrs); initClock(context); } private void initClock(Context context) { Resources r = mContext.getResources(); 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; mCalendar.setTimeInMillis(System.currentTimeMillis()); setText(DateFormat.format(mFormat, mCalendar)); invalidate(); long now = SystemClock.uptimeMillis(); long next = now + (1000 - now % 1000); mHandler.postAtTime(mTicker, next); } }; mTicker.run(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mTickerStopped = true; } /** * 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(); } }}
這個類的源碼不算多,所以有耐心的全部看完,
首Crowdsourced Security Testing道了DigitalClock類是繼承此TextView
從建構函式中開始看initClock()方法,代碼如下
private void initClock(Context context) {
Resources r = mContext.getResources();
if (mCalendar == null) {
mCalendar = Calendar.getInstance();
}www.bkjia.com
mFormatChangeObserver = new FormatChangeObserver();
getContext().getContentResolver().registerContentObserver(
Settings.System.CONTENT_URI, true, mFormatChangeObserver);
setFormat();
}
setFormat()方法預設是顯示24小時的,最主要的是onAttachedToWindow()方法,
onAttachedToWindow()是掛載在表單上的 他在activity的onResume()方法之後會執行,
在onAttachedToWindow()方法中關鍵是這段代碼:
mHandler = new Handler();
/**
* requests a tick on the next hard-second boundary
*/
mTicker = new Runnable() {
public void run() {
if (mTickerStopped) return;
mCalendar.setTimeInMillis(System.currentTimeMillis());
setText(DateFormat.format(mFormat, mCalendar));
invalidate();
long now = SystemClock.uptimeMillis();
long next = now + (1000 - now % 1000);
mHandler.postAtTime(mTicker, next);
}
};
mTicker.run();
這是整個類的核心,
mCalendar.setTimeInMillis(System.currentTimeMillis());
setText(DateFormat.format(mFormat, mCalendar));
這二行代碼就是顯示在我們案頭上的時間 DigitalClock在內部已經實現了,所以為什麼我們不用寫代碼就看到數字時針時間了,
invalidate();沒時間變化一次 就會重新繪製UI
long now = SystemClock.uptimeMillis();//從開機到現在的毫秒數(手機睡眠的時間不包括在內);
long next = now + (1000 - now % 1000);
mHandler.postAtTime(mTicker, next);
long next = now + (1000 - now % 1000);這行代碼錶示沒看懂?