Android时钟

1. Android时钟效果图:

Android 时钟效果图

2. 时钟所需资源:


(1)时钟表盘:

Android 时钟表盘


(2)时钟时针:

Android 时钟时针


(3)时钟分针和秒针:

Android 时钟分针和秒针

3. 代码实现:

package com.android.clockview;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class ClockView extends Activity {

	private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

	private QAnalogClock clock1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout lLayout = new LinearLayout(this);
		lLayout.setLayoutParams(new LinearLayout.LayoutParams(FP, FP));
		lLayout.setOrientation(LinearLayout.VERTICAL);

		clock1 = new QAnalogClock(this, "GMT+8:00");
		lLayout.addView(clock1, new LinearLayout.LayoutParams(WC, WC));

		setContentView(lLayout);
	}

	class QAnalogClock extends View {
		Bitmap mBmpDial;
		Bitmap mBmpHour;
		Bitmap mBmpMinute;
		Bitmap mBmpSecond;

		BitmapDrawable bmdHour;
		BitmapDrawable bmdMinute;
		BitmapDrawable bmdSecond;
		BitmapDrawable bmdDial;

		Paint mPaint;

		Handler tickHandler;

		int mWidth;
		int mHeigh;
		int mTempWidth;
		int mTempHeigh;
		int centerX;
		int centerY;

		int availableWidth = 100;
		int availableHeight = 100;

		private String sTimeZoneString;

		public QAnalogClock(Context context, String sTime_Zone) {
			super(context);
			sTimeZoneString = sTime_Zone;

			mBmpHour = BitmapFactory.decodeResource(getResources(),
					R.drawable.android_clock_hour);
			bmdHour = new BitmapDrawable(mBmpHour);

			mBmpMinute = BitmapFactory.decodeResource(getResources(),
					R.drawable.android_clock_minute);
			bmdMinute = new BitmapDrawable(mBmpMinute);

			mBmpSecond = BitmapFactory.decodeResource(getResources(),
					R.drawable.android_clock_minute);
			bmdSecond = new BitmapDrawable(mBmpSecond);

			mBmpDial = BitmapFactory.decodeResource(getResources(),
					R.drawable.android_clock_dial);
			bmdDial = new BitmapDrawable(mBmpDial);
			mWidth = mBmpDial.getWidth();
			mHeigh = mBmpDial.getHeight();
			centerX = availableWidth / 2;
			centerY = availableHeight / 2;

			mPaint = new Paint();
			mPaint.setColor(Color.BLUE);
			run();
		}

		public void run() {
			tickHandler = new Handler();
			tickHandler.post(tickRunnable);
		}

		private Runnable tickRunnable = new Runnable() {
			public void run() {
				postInvalidate();
				tickHandler.postDelayed(tickRunnable, 1000);
			}
		};

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);

			Calendar cal = Calendar.getInstance(TimeZone
					.getTimeZone(sTimeZoneString));
			int hour = cal.get(Calendar.HOUR);
			int minute = cal.get(Calendar.MINUTE);
			int second = cal.get(Calendar.SECOND);
			float hourRotate = hour * 30.0f + minute / 60.0f * 30.0f;
			float minuteRotate = minute * 6.0f;
			float secondRotate = second * 6.0f;

			boolean scaled = false;

			if (availableWidth < mwidth="" ||="" availableheight="">< mheigh)="" {="" scaled="true;" float="" scale="Math.min((float)" availablewidth="" (float)="" mwidth,="" (float)="" availableheight="" (float)="" mheigh);="" canvas.save();="" canvas.scale(scale,="" scale,="" centerx,="" centery);="" }="" bmddial.setbounds(centerx="" -="" (mwidth="" 2),="" centery="" -="" (mheigh="" 2),="" centerx="" +="" (mwidth="" 2),="" centery="" +="" (mheigh="" 2));="" bmddial.draw(canvas);="" mtempwidth="bmdHour.getIntrinsicWidth();" mtempheigh="bmdHour.getIntrinsicHeight();" canvas.save();="" canvas.rotate(hourrotate,="" centerx,="" centery);="" bmdhour.setbounds(centerx="" -="" (mtempwidth="" 2),="" centery="" -="" (mtempheigh="" 2),="" centerx="" +="" (mtempwidth="" 2),="" centery="" +="" (mtempheigh="" 2));="" bmdhour.draw(canvas);="" canvas.restore();="" mtempwidth="bmdMinute.getIntrinsicWidth();" mtempheigh="bmdMinute.getIntrinsicHeight();" canvas.save();="" canvas.rotate(minuterotate,="" centerx,="" centery);="" bmdminute.setbounds(centerx="" -="" (mtempwidth="" 2),="" centery="" -="" (mtempheigh="" 2),="" centerx="" +="" (mtempwidth="" 2),="" centery="" +="" (mtempheigh="" 2));="" bmdminute.draw(canvas);="" canvas.restore();="" mtempwidth="bmdSecond.getIntrinsicWidth();" mtempheigh="bmdSecond.getIntrinsicHeight();" canvas.rotate(secondrotate,="" centerx,="" centery);="" bmdsecond.setbounds(centerx="" -="" (mtempwidth="" 2),="" centery="" -="" (mtempheigh="" 2),="" centerx="" +="" (mtempwidth="" 2),="" centery="" +="" (mtempheigh="" 2));="" bmdsecond.draw(canvas);="" if="" (scaled)="" {="" canvas.restore();="" }="" }="" }="" }="">
暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心