时钟

时钟实现代码

public class SimulateClock extends View {
	private static final String TAG = "SimulateClock";
	// 时钟盘,分针、秒针、时针对象

	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 =220;
	int availableHeight = 220;

	public SimulateClock(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public SimulateClock(Context context, AttributeSet attrs) {
		this(context, attrs, 1);
	}

	public SimulateClock(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		
			mBmpHour = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.hour1);
			bmdHour = new BitmapDrawable(context.getResources(), mBmpHour);

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

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

			mBmpDial = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.clock_bg1);

	
	

		bmdDial = new BitmapDrawable(context.getResources(), mBmpDial);

		mWidth = mBmpDial.getWidth();
		mHeigh = mBmpDial.getHeight();
		Log.d(TAG, "==mWidth==" + mWidth
				+ "==mHeigh==" + mHeigh);
		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();
		cal.setTimeInMillis(System.currentTimeMillis());
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		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();
		}
	}
}

源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值