根據AnalogClock(時針),增加了顯示秒針的widget

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.shen.apitest.MyAnalogClock 
        android:id="@+id/clock" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
        
		
</RelativeLayout>


            文章 要點: 取消了廣播接收器,增加了handle message的消息處理機制,

 用到的資源圖片,都是apiDemo下面的drawable 

路徑(D:\android-sdk-windows\platforms\android-8\data\res\drawable-hdpi)

        

package com.shen.apitest;

/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.TimeZone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews.RemoteView;

import com.shen.R;

/**
 * This widget display an analogic clock with two hands for hours and minutes.
 */
@RemoteView
public class MyAnalogClock extends View {
	private Time mCalendar;
	private Drawable mHourHand;
	private Drawable mMinuteHand;
	private Drawable mDial;

	private int mDialWidth;
	private int mDialHeight;

	private boolean mAttached;

	private float mMinutes;
	private float mHour;

	private boolean mChanged;

	// /增加了秒針顯示所用到的秒錶
	private static String debug = "MyAnalogClock";

	private static int SECONDS_FLAG = 0;
	private Message secondsMsg;
	private float mSeconds;
	// /////end

	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:
				onTimeChanged();// 重新獲取的系統的當前時間,得到時,分,秒
				invalidate();// 強制繪製,調用自身的onDraw();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	public MyAnalogClock(Context context) {
		this(context, null);
	}

	public MyAnalogClock(Context context, AttributeSet attrs) {
		this(context, attrs, 0);

	}

	public MyAnalogClock(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Resources r = context.getResources();

		if (mDial == null) {
			mDial = r.getDrawable(R.drawable.clock_dial);
		}

		if (mHourHand == null) {
			mHourHand = r.getDrawable(R.drawable.clock_hand_hour);
		}

		if (mMinuteHand == null) {
			mMinuteHand = r.getDrawable(R.drawable.clock_hand_minute);
		}

		mCalendar = new Time();

		mDialWidth = mDial.getIntrinsicWidth();
		mDialHeight = mDial.getIntrinsicHeight();

	}

	@Override
	/*
	 * * 吸附到窗體上
	 */
	protected void onAttachedToWindow() {
		Log.e(debug, "onAttachedToWindow");
		super.onAttachedToWindow();

		if (!mAttached) {
			mAttached = true;
			// /////////修改到秒針顯示後,不需要廣播接收器
			// IntentFilter filter = new IntentFilter();
			//
			// filter.addAction(Intent.ACTION_TIME_TICK);// 每隔一分鐘會發出這樣的一個action
			// filter.addAction(Intent.ACTION_TIME_CHANGED);//
			// 外部修改系統時間,發出這樣的action
			// filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);//
			// 外部修改系統的時區,發出action

			// getContext().registerReceiver(mIntentReceiver, filter);
			// null,handler 這兩個參數暫時無效,故去掉,
			// ///////end
		}

		// NOTE: It's safe to do these after registering the receiver since the
		// receiver always runs
		// in the main thread, therefore the receiver can't run before this
		// method returns.

		// The time zone may have changed while the receiver wasn't registered,
		// so update the Time
		mCalendar = new Time();

		// Make sure we update to the current time
		onTimeChanged();

		initSecondsThread();
	}

	private void initSecondsThread() {
		secondsMsg = mHandler.obtainMessage(SECONDS_FLAG);
		Thread newThread = new Thread() {
			@Override
			public void run() {
				while (mAttached) {
					// 如果這個消息不重新獲取的話,
					// 會拋一個this message is already in use 的異常
					secondsMsg = mHandler.obtainMessage(SECONDS_FLAG);
					// /end
					mHandler.sendMessage(secondsMsg);
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}
		};
		newThread.start();

	}

	@Override
	/*
	 * * 脫離窗體 在按home按鍵,不觸發這個事件,所以這個應用的監聽還是持續監聽着。
	 * 如果外部修改系統事件,action=Intent.ACTION_TIME_CHANGED 按back按鍵,觸發事件,下次從onCreate從新載入
	 */
	protected void onDetachedFromWindow() {
		Log.e(debug, "onDetachedFromWindow");
		super.onDetachedFromWindow();
		if (mAttached) {
			// getContext().unregisterReceiver(mIntentReceiver); 增加秒針,不需要接收器了。
			mAttached = false;
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		Log.e(debug, "onMeasure");
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);

		float hScale = 1.0f;
		float vScale = 1.0f;

		if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
			hScale = (float) widthSize / (float) mDialWidth;
		}

		if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
			vScale = (float) heightSize / (float) mDialHeight;
		}

		float scale = Math.min(hScale, vScale);

		setMeasuredDimension(resolveSize((int) (mDialWidth * scale),
				widthMeasureSpec), resolveSize((int) (mDialHeight * scale),
				heightMeasureSpec));
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		Log.e(debug, "onSizeChanged");
		super.onSizeChanged(w, h, oldw, oldh);
		mChanged = true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.e(debug, "canvas");
		boolean changed = mChanged;
		if (changed) {
			mChanged = false;
		}

		int availableWidth = getWidth();
		int availableHeight = getHeight();

		int x = availableWidth / 2;
		int y = availableHeight / 2;

		final Drawable dial = mDial;
		int w = dial.getIntrinsicWidth();
		int h = dial.getIntrinsicHeight();

		boolean scaled = false;

		if (availableWidth < w || availableHeight < h) {
			scaled = true;
			float scale = Math.min((float) availableWidth / (float) w,
					(float) availableHeight / (float) h);
			canvas.save();
			canvas.scale(scale, scale, x, y);
		}

		if (changed) {
			dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
		}
		dial.draw(canvas);
		// //////以上是繪製12個小時背景圖

		canvas.save();
		canvas.rotate(mHour / 12.0f * 360.0f, x, y);

		final Drawable hourHand = mHourHand;
		if (changed) {
			w = hourHand.getIntrinsicWidth();
			h = hourHand.getIntrinsicHeight();
			hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y
					+ (h / 2));
		}
		hourHand.draw(canvas);
		canvas.restore();
		// //////以上是繪製時針

		canvas.save();
		canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);

		final Drawable minuteHand = mMinuteHand;
		if (changed) {
			w = minuteHand.getIntrinsicWidth();
			h = minuteHand.getIntrinsicHeight();
			minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y
					+ (h / 2));
		}
		minuteHand.draw(canvas);
		canvas.restore();
		// //////以上是繪製分針

		// /增加秒針的繪製
		canvas.save();
		canvas.rotate(mSeconds / 60.0f * 360.0f, x, y);

		final Drawable secondHand = mMinuteHand;// //用時針來代替秒針
		if (changed) {
			w = secondHand.getIntrinsicWidth();
			h = secondHand.getIntrinsicHeight();
			secondHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y
					+ (h / 2));
		}
		secondHand.draw(canvas);
		canvas.restore();
		// /////end

		if (scaled) {
			canvas.restore();
		}
	}

	/**
	 * 改變時間
	 */
	private void onTimeChanged() {
		Log.e(debug, "onTimeChanged");
		mCalendar.setToNow();// ///獲取手機自身的當前時間,而非實際中的標準的北京時間

		int hour = mCalendar.hour;// 小時
		int minute = mCalendar.minute;// 分鐘
		int second = mCalendar.second;// 秒

		mSeconds = second;
		mMinutes = minute + second / 60.0f;
		mHour = hour + mMinutes / 60.0f;

		mChanged = true;
	}

	/**
	 * 這個接收器,只接受三個action, 1.Intent.ACTION_TIME_TICK,每分鐘發出一次
	 * 2.Intent.ACTION_TIME_CHANGE, 外部修改系統時間 3.Intent.ACTION_TIMEZONE_CHANGED
	 * 外部系統的時區 按home,還能繼續監聽 按back,監聽銷燬
	 */
	private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.e(debug, "onReceive");
			Log.e(debug, "intent action=" + intent.getAction());
			if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
				String tz = intent.getStringExtra("time-zone");
				mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
			}
			onTimeChanged();
			invalidate();
		}
	};
}


 

analogclock.xml

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章