代码实现
自定义控件的代码:
public class ClockView extends View{ private Paint circlePaint,dialPaint,numberPaint; //view 的宽高 private float mWidth,mHeight; //圆的半径 private float circleRadius; //圆心X,Y坐标 private float circleX,circleY; private int second,minute; private double hour; private Handler handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==0){ invalidate(); } } }; public ClockView(Context context, AttributeSet attrs) { super(context, attrs); initPaint(); } private void initPaint(){ //刻盘圆,小时刻度,时针和分针的画笔 circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); circlePaint.setColor(Color.BLACK); circlePaint.setStyle(Paint.Style.STROKE); circlePaint.setStrokeWidth(10); //分钟刻度的画笔 dialPaint = new Paint(Paint.ANTI_ALIAS_FLAG); dialPaint.setColor(Color.BLACK); dialPaint.setStrokeWidth(5); //数字的画笔 numberPaint = new Paint(Paint.ANTI_ALIAS_FLAG); numberPaint.setColor(Color.BLACK); numberPaint.setStrokeWidth(5); numberPaint.setTextSize(30); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); if(mWidth public class Point { private float x; private float y; public Point(float x, float y) { this.x = x; this.y = y;}public float getX() { return x;}public void setX(float x) { this.x = x;}public float getY() { return y;}public void setY(float y) { this.y = y;} Acitivity: public class ClockActivity extends Activity{ private ClockView clockView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.clock_layout); clockView = (ClockView) findViewById(R.id.clock); } @Override protected void onResume() { super.onResume(); clockView.startClock(); } @Override protected void onStop() { super.onStop(); clockView.stopClock(); } } xml布局: