|
@@ -0,0 +1,106 @@
|
|
|
+package com.wdkl.ncs.android.lib.widget;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Looper;
|
|
|
+import android.os.SystemClock;
|
|
|
+import android.text.format.DateFormat;
|
|
|
+import android.util.AttributeSet;
|
|
|
+import android.util.Log;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+public class MyClockView extends TextView {
|
|
|
+
|
|
|
+ private CharSequence mFormat = "MM-dd HH:mm";
|
|
|
+
|
|
|
+ private Calendar mTime;
|
|
|
+ private String mTimeZone;
|
|
|
+
|
|
|
+ private boolean mStopTicking;
|
|
|
+ private final Handler handler = new Handler(Looper.myLooper());
|
|
|
+
|
|
|
+ private final Runnable mTicker = new Runnable() {
|
|
|
+ public void run() {
|
|
|
+ if (mStopTicking) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ onTimeChanged();
|
|
|
+
|
|
|
+ long now = SystemClock.uptimeMillis();
|
|
|
+ long next = now + (1000 - now % 1000);
|
|
|
+
|
|
|
+ handler.postAtTime(mTicker, next);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ public MyClockView(Context context) {
|
|
|
+ super(context);
|
|
|
+ init();
|
|
|
+ }
|
|
|
+
|
|
|
+ public MyClockView(Context context, AttributeSet attrs) {
|
|
|
+ this(context, attrs, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public MyClockView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
+ this(context, attrs, defStyleAttr, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public MyClockView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
|
|
+ super(context, attrs, defStyleAttr, defStyleRes);
|
|
|
+
|
|
|
+ init();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init() {
|
|
|
+ createTime(mTimeZone);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void createTime(String timeZone) {
|
|
|
+ if (timeZone != null) {
|
|
|
+ mTime = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
|
|
|
+ } else {
|
|
|
+ mTime = Calendar.getInstance();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getTimeZone() {
|
|
|
+ return mTimeZone;
|
|
|
+ }
|
|
|
+
|
|
|
+ public CharSequence getFormat() {
|
|
|
+ return mFormat;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setFormat(CharSequence format) {
|
|
|
+ mFormat = format;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onAttachedToWindow() {
|
|
|
+ super.onAttachedToWindow();
|
|
|
+
|
|
|
+ mStopTicking = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDetachedFromWindow() {
|
|
|
+ super.onDetachedFromWindow();
|
|
|
+
|
|
|
+ mStopTicking = true;
|
|
|
+ handler.removeCallbacks(mTicker);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void startTick() {
|
|
|
+ mTicker.run();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void onTimeChanged() {
|
|
|
+ mTime.setTimeInMillis(System.currentTimeMillis());
|
|
|
+ setText(DateFormat.format(mFormat, mTime));
|
|
|
+ invalidate();
|
|
|
+ }
|
|
|
+}
|