标签:
在android如果表示数字时针一般用DigitalClock,这个类是google给我们开发者提供的一个方便的类实现数字时针的功能,现在就写个demo,为什么要讲这个类呢?因为我下篇要讲ListView item倒计时功能,所以先把这个基础练习一下,以及它的实现方式,新建android项目digitalClockdemo
布局activity_main.xml文件
<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">
<DigitalClock android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:textColor="#ff00ff"
/>
</RelativeLayout>
这个数字是变化的 这只是静态图,为什么代码没写会出来这个效果,所以就得看下他的源码了
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();
}
}
}
首先知道了DigitalClock类是继承此TextView
从构造函数中开始看initClock()方法,代码如下
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();
}
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);这行代码表示没看懂?
android从源码带你熟悉DigitalClock 数字时针的应用以及它的使用场景
标签:
原文地址:http://blog.csdn.net/coderinchina/article/details/43450027