时钟实现代码
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();
}
}
}