当前位置: 代码迷 >> Android >> Android钟表
  详细解决方案

Android钟表

热度:59   发布时间:2016-05-01 13:42:55.0
Android时钟

1. Android时钟,效果图:

????时钟

2. 时钟所需资源:

?

??? (1)时钟表盘:

????????????表盘

?

??? (2)时钟时针:

????????????时针

??? (3)时钟分针和秒针:

????????????分针和秒针

3. 代码实现:

[java:firstline[1]]?view plaincopy
  1. package?com.android.clockview;??
  2. ??
  3. import?java.util.Calendar;??
  4. import?java.util.TimeZone;??
  5. ??
  6. import?android.app.Activity;??
  7. import?android.content.Context;??
  8. import?android.graphics.Bitmap;??
  9. import?android.graphics.BitmapFactory;??
  10. import?android.graphics.Canvas;??
  11. import?android.graphics.Color;??
  12. import?android.graphics.Paint;??
  13. import?android.graphics.drawable.BitmapDrawable;??
  14. import?android.os.Bundle;??
  15. import?android.os.Handler;??
  16. import?android.view.View;??
  17. import?android.view.ViewGroup;??
  18. import?android.widget.LinearLayout;??
  19. ??
  20. public?class?ClockView?extends?Activity?{??
  21. ??
  22. ????private?final?int?FP?=?ViewGroup.LayoutParams.FILL_PARENT;??
  23. ????private?final?int?WC?=?ViewGroup.LayoutParams.WRAP_CONTENT;??
  24. ??
  25. ????private?QAnalogClock?clock1;??
  26. ??
  27. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  28. ????????super.onCreate(savedInstanceState);??
  29. ????????LinearLayout?lLayout?=?new?LinearLayout(this);??
  30. ????????lLayout.setLayoutParams(new?LinearLayout.LayoutParams(FP,?FP));??
  31. ????????lLayout.setOrientation(LinearLayout.VERTICAL);??
  32. ??
  33. ????????clock1?=?new?QAnalogClock(this,?"GMT+8:00");??
  34. ????????lLayout.addView(clock1,?new?LinearLayout.LayoutParams(WC,?WC));??
  35. ??
  36. ????????setContentView(lLayout);??
  37. ????}??
  38. ??
  39. ????class?QAnalogClock?extends?View?{??
  40. ????????Bitmap?mBmpDial;??
  41. ????????Bitmap?mBmpHour;??
  42. ????????Bitmap?mBmpMinute;??
  43. ????????Bitmap?mBmpSecond;??
  44. ??
  45. ????????BitmapDrawable?bmdHour;??
  46. ????????BitmapDrawable?bmdMinute;??
  47. ????????BitmapDrawable?bmdSecond;??
  48. ????????BitmapDrawable?bmdDial;??
  49. ??
  50. ????????Paint?mPaint;??
  51. ??
  52. ????????Handler?tickHandler;??
  53. ??
  54. ????????int?mWidth;??
  55. ????????int?mHeigh;??
  56. ????????int?mTempWidth;??
  57. ????????int?mTempHeigh;??
  58. ????????int?centerX;??
  59. ????????int?centerY;??
  60. ??
  61. ????????int?availableWidth?=?100;??
  62. ????????int?availableHeight?=?100;??
  63. ??
  64. ????????private?String?sTimeZoneString;??
  65. ??
  66. ????????public?QAnalogClock(Context?context,?String?sTime_Zone)?{??
  67. ????????????super(context);??
  68. ????????????sTimeZoneString?=?sTime_Zone;??
  69. ??
  70. ????????????mBmpHour?=?BitmapFactory.decodeResource(getResources(),??
  71. ????????????????????R.drawable.android_clock_hour);??
  72. ????????????bmdHour?=?new?BitmapDrawable(mBmpHour);??
  73. ??
  74. ????????????mBmpMinute?=?BitmapFactory.decodeResource(getResources(),??
  75. ????????????????????R.drawable.android_clock_minute);??
  76. ????????????bmdMinute?=?new?BitmapDrawable(mBmpMinute);??
  77. ??
  78. ????????????mBmpSecond?=?BitmapFactory.decodeResource(getResources(),??
  79. ????????????????????R.drawable.android_clock_minute);??
  80. ????????????bmdSecond?=?new?BitmapDrawable(mBmpSecond);??
  81. ??
  82. ????????????mBmpDial?=?BitmapFactory.decodeResource(getResources(),??
  83. ????????????????????R.drawable.android_clock_dial);??
  84. ????????????bmdDial?=?new?BitmapDrawable(mBmpDial);??
  85. ????????????mWidth?=?mBmpDial.getWidth();??
  86. ????????????mHeigh?=?mBmpDial.getHeight();??
  87. ????????????centerX?=?availableWidth?/?2;??
  88. ????????????centerY?=?availableHeight?/?2;??
  89. ??
  90. ????????????mPaint?=?new?Paint();??
  91. ????????????mPaint.setColor(Color.BLUE);??
  92. ????????????run();??
  93. ????????}??
  94. ??
  95. ????????public?void?run()?{??
  96. ????????????tickHandler?=?new?Handler();??
  97. ????????????tickHandler.post(tickRunnable);??
  98. ????????}??
  99. ??
  100. ????????private?Runnable?tickRunnable?=?new?Runnable()?{??
  101. ????????????public?void?run()?{??
  102. ????????????????postInvalidate();??
  103. ????????????????tickHandler.postDelayed(tickRunnable,?1000);??
  104. ????????????}??
  105. ????????};??
  106. ??
  107. ????????protected?void?onDraw(Canvas?canvas)?{??
  108. ????????????super.onDraw(canvas);??
  109. ??
  110. ????????????Calendar?cal?=?Calendar.getInstance(TimeZone??
  111. ????????????????????.getTimeZone(sTimeZoneString));??
  112. ????????????int?hour?=?cal.get(Calendar.HOUR);??
  113. ????????????int?minute?=?cal.get(Calendar.MINUTE);??
  114. ????????????int?second?=?cal.get(Calendar.SECOND);??
  115. ????????????float?hourRotate?=?hour?*?30.0f?+?minute?/?60.0f?*?30.0f;??
  116. ????????????float?minuteRotate?=?minute?*?6.0f;??
  117. ????????????float?secondRotate?=?second?*?6.0f;??
  118. ??
  119. ????????????boolean?scaled?=?false;??
  120. ??
  121. ????????????if?(availableWidth?<?mWidth?||?availableHeight?<?mHeigh)?{??
  122. ????????????????scaled?=?true;??
  123. ????????????????float?scale?=?Math.min((float)?availableWidth?/?(float)?mWidth,??
  124. ????????????????????????(float)?availableHeight?/?(float)?mHeigh);??
  125. ????????????????canvas.save();??
  126. ????????????????canvas.scale(scale,?scale,?centerX,?centerY);??
  127. ????????????}??
  128. ??
  129. ????????????bmdDial.setBounds(centerX?-?(mWidth?/?2),?centerY?-?(mHeigh?/?2),??
  130. ????????????????????centerX?+?(mWidth?/?2),?centerY?+?(mHeigh?/?2));??
  131. ????????????bmdDial.draw(canvas);??
  132. ??
  133. ????????????mTempWidth?=?bmdHour.getIntrinsicWidth();??
  134. ????????????mTempHeigh?=?bmdHour.getIntrinsicHeight();??
  135. ????????????canvas.save();??
  136. ????????????canvas.rotate(hourRotate,?centerX,?centerY);??
  137. ????????????bmdHour.setBounds(centerX?-?(mTempWidth?/?2),?centerY??
  138. ????????????????????-?(mTempHeigh?/?2),?centerX?+?(mTempWidth?/?2),?centerY??
  139. ????????????????????+?(mTempHeigh?/?2));??
  140. ????????????bmdHour.draw(canvas);??
  141. ??
  142. ????????????canvas.restore();??
  143. ??
  144. ????????????mTempWidth?=?bmdMinute.getIntrinsicWidth();??
  145. ????????????mTempHeigh?=?bmdMinute.getIntrinsicHeight();??
  146. ????????????canvas.save();??
  147. ????????????canvas.rotate(minuteRotate,?centerX,?centerY);??
  148. ????????????bmdMinute.setBounds(centerX?-?(mTempWidth?/?2),?centerY??
  149. ????????????????????-?(mTempHeigh?/?2),?centerX?+?(mTempWidth?/?2),?centerY??
  150. ????????????????????+?(mTempHeigh?/?2));??
  151. ????????????bmdMinute.draw(canvas);??
  152. ??
  153. ????????????canvas.restore();??
  154. ??
  155. ????????????mTempWidth?=?bmdSecond.getIntrinsicWidth();??
  156. ????????????mTempHeigh?=?bmdSecond.getIntrinsicHeight();??
  157. ????????????canvas.rotate(secondRotate,?centerX,?centerY);??
  158. ????????????bmdSecond.setBounds(centerX?-?(mTempWidth?/?2),?centerY??
  159. ????????????????????-?(mTempHeigh?/?2),?centerX?+?(mTempWidth?/?2),?centerY??
  160. ????????????????????+?(mTempHeigh?/?2));??
  161. ????????????bmdSecond.draw(canvas);??
  162. ??
  163. ????????????if?(scaled)?{??
  164. ????????????????canvas.restore();??
  165. ????????????}??
  166. ????????}??
  167. ????}??
  168. ??????
  169. } ?

?

  相关解决方案