Android自定义View画出一个时钟(时针、分针、秒针)完美搭配当前时间走动
来源:互联网 发布:软件盒子网站 编辑:程序博客网 时间:2024/05/21 09:03
1、获取时间值
private void getDatas() { SimpleDateFormat format = new SimpleDateFormat("HH,mm,ss"); String time = format.format(new Date()); try { String s[] = time.split(","); hcount = Integer.parseInt(s[0]); mcount = Integer.parseInt(s[1]); scount = Integer.parseInt(s[2]); } catch (Exception ex) { ex.printStackTrace(); } }
2、画出外层圆
Paint paintCircle = new Paint(); paintCircle.setStyle(Paint.Style.STROKE); paintCircle.setStrokeWidth(3); paintCircle.setColor(colorCalibration); paintCircle.setAntiAlias(true); //画出外层的圆盘 canvas.drawCircle(width / 2, height / 2, radius, paintCircle);
3、画出上下午标识
// 绘制上下午 paintCircle.setTextSize(24); paintCircle.setStyle(Paint.Style.FILL); paintCircle.setStrokeWidth(0); canvas.drawText(hcount < 12 ? "AM" : "PM", width / 2 - paintCircle.measureText("PM") / 2, height / 2 + 50, paintCircle);
4、利用画布canvas的rotate旋转画出刻度条
Paint paintDegree = new Paint(); paintDegree.setColor(colorCalibration); paintDegree.setStyle(Paint.Style.STROKE); paintDegree.setStrokeWidth(3); paintDegree.setAntiAlias(true); //画出12个小时的刻度线 for (int i = 0; i < 12; i++) { canvas.drawLine(width / 2, height / 2 - radius, width / 2, height / 2 - radius + 30, paintDegree); canvas.rotate(ANGLE_HOUR, width / 2, height / 2); } //画出60个分钟的刻度线 for (int x = 0; x < 60; x++) { paintDegree.setStrokeWidth(2); if (x % 5 != 0) { canvas.drawLine(width / 2, height / 2 - radius, width / 2, height / 2 - radius + 20, paintDegree); } canvas.rotate(ANGLE_MINUTE, width / 2, height / 2); }
5、画出当前的时针分针秒针
int hourRadius = radius * 5 / 12;//时针长度 int minuteRaidus = radius * 8 / 12;//分针长度 int secondRaidus = radius * 10 / 12;//秒针长度 Paint paintHour = new Paint(); paintHour.setStyle(Paint.Style.STROKE); paintHour.setAntiAlias(true); paintHour.setStrokeWidth(7); paintHour.setColor(colorHour); //将坐标系的平移至原点为(wdith/2,height/2)的地方 canvas.translate(width / 2, height / 2); canvas.save(); int offset = 30 * mcount / 60; offset -= offset % ANGLE_MINUTE;//时针相对分针数,有一个偏移量 int rotateH = 180 + ANGLE_HOUR * hcount + offset; canvas.rotate(rotateH); canvas.drawLine(0, -DEFAULT_POINT_BACK_LENGTH, 0, hourRadius, paintHour);//画时针 canvas.restore(); Paint paintMinute = new Paint(); paintMinute.setStrokeWidth(5); paintMinute.setColor(colorMinute); paintMinute.setStyle(Paint.Style.STROKE); paintMinute.setAntiAlias(true); int rotateM = 180 + ANGLE_MINUTE * mcount; canvas.save(); canvas.rotate(rotateM); canvas.drawLine(0, -DEFAULT_POINT_BACK_LENGTH, 0, minuteRaidus, paintMinute);//画分针 canvas.restore(); Paint paintSecond = new Paint(); paintSecond.setStrokeWidth(3); paintSecond.setColor(colorSecond); paintSecond.setStyle(Paint.Style.STROKE); paintSecond.setAntiAlias(true); int rotateS = 180 + ANGLE_MINUTE * scount; canvas.save(); canvas.rotate(rotateS); canvas.drawLine(0, -DEFAULT_POINT_BACK_LENGTH, 0, secondRaidus, paintSecond);//画秒针 canvas.restore();
6、画出圆心
//画圆心 Paint paintCenter = new Paint(); paintCenter.setColor(Color.WHITE); canvas.drawCircle(0, 0, 2, paintCenter);
7、延迟一秒就更新界面,重绘
postInvalidateDelayed(1000);//延迟一秒执行
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyClockView, defStyleAttr, 0); colorHour = typedArray.getColor(R.styleable.MyClockView_clock_hour_color, Color.RED);//时针颜色 colorMinute = typedArray.getColor(R.styleable.MyClockView_clock_minute_color, Color.GREEN);//分针颜色 colorSecond = typedArray.getColor(R.styleable.MyClockView_clock_second_color, Color.BLUE);//秒针颜色 colorCalibration = typedArray.getColor(R.styleable.MyClockView_clock_calibration_color, Color.BLACK);//时钟刻度颜色值 typedArray.recycle();
完成。
具体代码详情请移步:https://github.com/xiaofuchen/MyClock
阅读全文
0 0
- Android自定义View画出一个时钟(时针、分针、秒针)完美搭配当前时间走动
- Android实现模拟时钟(简单+漂亮)--时针、分针、秒针
- hdu 1006 时针/分针/秒针
- 时针分针秒针夹角问题
- 时针分针重合问题与时针、分针、秒针重合问题
- 在一天的24小时之中,时钟的时针、分针和秒针完全重合在一起的时候有几次?都分别是什么时间?你怎样算出来的?
- 在一天的24小时之中,时钟的时针、分针和秒针完全重合在一起的时候有几次?都分别是什么时间?你怎样算出
- 在一天的24小时之中,时钟的时针、分针和秒针完
- 时针,分针和秒针的重合问题
- 时针分针秒针一天到底重合几次
- 时针分针秒针一天重合几次
- 逻辑思维题之时针分针秒针问题
- HDU 5387 Clock(计算时针与分针 时针与秒针 分针与秒针的夹角)
- 在一天的24小时之中,时钟的时针、分针和秒针完全重合在一起的时候有几次?
- 在一天的24小时之中,时钟的时针、分针和秒针完全重合在一起的时候有几次?
- 在一天的24小时之中,时钟的时针、分针和秒针完全重合在一起的时候有几次? 都分别是什么时间?你怎样算出来的?(5分钟-15分钟)
- 时针、分针和秒针是群快乐的好伙伴
- 在一天的24小时之中,时钟的时针、分针和秒针完整重合在一路的时辰有几回?都分辨是什么时光?你如何算出来的?
- bzoj 4578 [Usaco2016 OPen]Splitting the Field
- SCFF使用中遇到的问题(二)
- alphago阿尔法狗学习
- spring快速入门
- 深入理解zygote——2(代码源于GooGle)
- Android自定义View画出一个时钟(时针、分针、秒针)完美搭配当前时间走动
- SimpleDateFormat使用,有时区的格式
- 从配置角度来说制约linux高并发的两个点
- jquery操作select(取值,设置选中)
- PrintWriter打印流实例
- <第二行代码>中的天气应用
- [PHP]
- okhttp多图上传
- CentOS 下配置vsftpd,匿名可上传可下载