| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 
 | public class Clock extends SurfaceView implements SurfaceHolder.Callback {
 private final Object lock = new Object();
 
 private SurfaceHolder mSurfaceHolder;
 
 private DrawThread mDrawThread;
 
 
 
 private Paint mPanelPaint;
 
 
 
 private Paint mScalePaint;
 
 
 
 private Paint mTextPaint;
 
 
 
 private Paint mHourPaint;
 
 
 
 private Paint mMinutePaint;
 
 
 
 private Paint mSecondPaint;
 
 private int mRadius;
 
 public Clock(Context context) {
 this(context, null);
 }
 
 public Clock(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }
 
 public Clock(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 
 mSurfaceHolder = this.getHolder();
 mSurfaceHolder.addCallback(this);
 
 
 mPanelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mPanelPaint.setColor(Color.RED);
 
 
 mScalePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mScalePaint.setColor(Color.argb(130, 255, 255, 255));
 
 mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mTextPaint.setColor(Color.WHITE);
 mTextPaint.setTextSize(40);
 mTextPaint.setTextAlign(Paint.Align.CENTER);
 
 
 mHourPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mHourPaint.setColor(Color.WHITE);
 
 
 mMinutePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mMinutePaint.setColor(Color.argb(200, 255, 255, 255));
 
 
 mSecondPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mSecondPaint.setStrokeWidth(2);
 mSecondPaint.setColor(Color.GRAY);
 
 setFocusable(false);
 setFocusableInTouchMode(false);
 }
 
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
 mDrawThread = new DrawThread();
 mDrawThread.setRun(true);
 mDrawThread.start();
 }
 
 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
 
 }
 
 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
 mDrawThread.setRun(false);
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 mRadius = Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2;
 }
 
 private class DrawThread extends Thread {
 
 private static final float RATIO = 6 / 7f;
 
 private boolean isRun;
 
 @Override
 public void run() {
 while (isRun) {
 synchronized (lock) {
 Canvas canvas = mSurfaceHolder.lockCanvas();
 if (canvas != null) {
 long start = System.currentTimeMillis();
 doDraw(canvas);
 mSurfaceHolder.unlockCanvasAndPost(canvas);
 long end = System.currentTimeMillis();
 long duration = end - start;
 if (duration < 1000) {
 try {
 Thread.sleep(1000 - duration);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 }
 }
 }
 }
 
 
 
 
 
 
 private void doDraw(final Canvas canvas) {
 
 canvas.drawColor(Color.RED);
 
 canvas.drawCircle(mRadius, mRadius, mRadius, mPanelPaint);
 
 for (int i = 1, scaleSize = 12 * 5 * 5; i <= scaleSize; i++) {
 canvas.rotate(1.2f, mRadius, mRadius);
 if (i % 25 == 0) {
 mScalePaint.setStrokeWidth(4);
 mScalePaint.setColor(Color.argb(150, 255, 255, 255));
 canvas.drawLine(mRadius, 0, mRadius, 50, mScalePaint);
 } else if (i % 5 == 0) {
 mScalePaint.setStrokeWidth(3);
 mScalePaint.setColor(Color.argb(120, 255, 255, 255));
 canvas.drawLine(mRadius, 0, mRadius, 30, mScalePaint);
 } else {
 mScalePaint.setStrokeWidth(2);
 mScalePaint.setColor(Color.argb(100, 255, 255, 255));
 canvas.drawLine(mRadius, 0, mRadius, 18, mScalePaint);
 }
 }
 
 RectF f = new RectF();
 
 
 Calendar calendar = Calendar.getInstance();
 int hour = calendar.get(Calendar.HOUR_OF_DAY);
 int minute = calendar.get(Calendar.MINUTE);
 int second = calendar.get(Calendar.SECOND);
 
 canvas.save();
 canvas.rotate(30 * (hour + (minute + second / 60) / 60f), mRadius, mRadius);
 canvas.drawRoundRect(new RectF(mRadius - 5, mRadius / 3, mRadius + 5, mRadius + 20), 10, 10, mHourPaint);
 canvas.restore();
 
 canvas.save();
 canvas.rotate(6 * (minute + second / 60f), mRadius, mRadius);
 canvas.drawRoundRect(new RectF(mRadius - 3f, 60, mRadius + 3f, mRadius + 20), 10, 10, mMinutePaint);
 canvas.restore();
 
 canvas.save();
 canvas.rotate(6 * second, mRadius, mRadius);
 mSecondPaint.setStyle(Paint.Style.FILL);
 canvas.drawRect(new RectF(mRadius - 2, 50, mRadius + 2, mRadius + 30), mSecondPaint);
 canvas.restore();
 
 
 for (int i = 1; i <= 12; i++) {
 
 float width = mTextPaint.measureText(String.valueOf(i));
 
 double x = mRadius + (mRadius - 50) * Math.cos((i * 30 - 90) * Math.PI / 180d);
 double y = mRadius + (mRadius - 50) * Math.sin((i * 30 - 90) * Math.PI / 180d) - width / 2 * Math.sin(i * 30);
 canvas.drawText(String.valueOf(i), (float) x, (float) y, mTextPaint);
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 mSecondPaint.setStyle(Paint.Style.FILL);
 canvas.drawCircle(mRadius, mRadius, 6, mSecondPaint);
 }
 
 void setRun(boolean isRun) {
 this.isRun = isRun;
 }
 }
 
 public void setRun(boolean isRun) {
 mDrawThread.setRun(isRun);
 }
 
 public void setRadius(int radius) {
 this.mRadius = radius;
 }
 }
 
 |