Android绘制钟表的方法
作者:UserNamezhangxi 时间:2023-09-01 13:18:03
本文实例为大家分享了Android绘制钟表的具体代码,供大家参考,具体内容如下
首先要画一个表,我们要先知道步骤如何:
1、仪表盘----外面最大的圆盘
2、刻度线----四个长刻度和剩下的短刻度
3、刻度值----对应的刻度下的数字
4、指针------钟表的三个指针
5、指针动起来
明确思路,下来就是画图了
1、仪表盘,画圆
outCirclePaint = new Paint();
outCirclePaint.setStrokeWidth(2);
outCirclePaint.setAntiAlias(true);
outCirclePaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(mWidth/2,mHeight/2,mWidth/2,outCirclePaint);
2、画刻度,同时写刻度值
画刻度的思路是每次画一个刻度(短的线段)完成之后,旋转画布30°,因为360/12。遇到3、6、9、12 把刻度线画粗,画稍长一点。
for (int i = 0; i <= 12;i++){
if (i==3||i==6||i==9 || i==12){
degreePaint.setStrokeWidth(3);
degreePaint.setTextSize(30);
canvas.drawLine(mWidth/2,mHeight/2-mWidth/2,mWidth/2,mHeight/2-mWidth/2+30,degreePaint);
String degree = String.valueOf(i);
canvas.drawText(degree,
mWidth/2-degreePaint.measureText(degree)/2,
mHeight/2-mWidth/2 + 60,
degreePaint);
}else{
if (i!=0){ //遇到0不考虑划线 写刻度值
degreePaint.setStrokeWidth(2);
degreePaint.setTextSize(20);
canvas.drawLine(mWidth/2,mHeight/2-mWidth/2,mWidth/2,mHeight/2-mWidth/2+15,degreePaint);
String degree = String.valueOf(i);
canvas.drawText(degree,
mWidth/2-degreePaint.measureText(degree)/2,
mHeight/2-mWidth/2 + 40,
degreePaint);
}
}
canvas.rotate(30,mWidth/2,mHeight/2);
}
3、画指针
canvas.translate(mWidth/2,mHeight/2);
canvas.drawLine(0,0,hx,hy,hourPaint); // 小时
canvas.drawLine(0,0,mx,my,minPaint); // 分钟
canvas.drawLine(0,0,sx,sy,sPaint); // 秒
4、指针动起来
指针动起来也就是说让指针的一端固定,另外一端需要通过sin计算Y值,cos计算X值,指针长度自己确定好即可。
这样秒针每次动一下就是6°,以这个为秒针单位。
Math.PI/30 //π/30
分针同理
时针不一样,每次动一下是要30°
Math.PI/6 //π/6
Calendar calendar = Calendar.getInstance();
hcount = calendar.get(Calendar.HOUR_OF_DAY);
mcount = calendar.get(Calendar.MINUTE);
scount = calendar.get(Calendar.SECOND);
int hx = (int) (70*Math.cos(Math.PI*(hcount%12-15) / 6));
int hy = (int) (70*Math.sin(Math.PI*(hcount%12-15) / 6));
int mx = (int) (90*Math.cos(Math.PI*(mcount-15) / 30));
int my = (int) (90*Math.sin(Math.PI*(mcount-15) / 30));
int sx = (int) (110*Math.cos(Math.PI*(scount-15) / 30)); // -15 是为了调整时差(角度差)
int sy = (int) (110*Math.sin(Math.PI*(scount-15) / 30));
最后和画指针的结合起来进行绘制就可以让指针动起来。
附加一个功能 显示上午下午的功能
//绘制 上午下午
APMPaint.setTextSize(20);
APMPaint.setStrokeWidth(2);
canvas.rotate(-30,mWidth/2,mHeight/2);
String apm ;
if (hcount < 12){
apm = "AM";
}else{
apm = "PM";
}
canvas.drawText(apm,
mWidth/2-degreePaint.measureText(apm)/2,
mHeight/2+100,
APMPaint);
大家还可以继续拓展,添加星期,和每个月的日期,做成一个属于你自己的表。
效果图:
参考代码
来源:https://blog.csdn.net/UserNamezhangxi/article/details/80418307
标签:Android,钟表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android EditTextView 实现带空格分隔的输入(电话号码,银行卡)
2023-09-02 12:48:19
C#编程读取文档Doc、Docx及Pdf内容的方法
2023-01-16 06:59:29
C#计算器编写代码
2022-12-28 02:57:25
![](https://img.aspxhome.com/file/2023/5/108635_0s.gif)
基于Freemarker和xml实现Java导出word
2022-07-11 23:15:12
![](https://img.aspxhome.com/file/2023/2/113442_0s.png)
Java去掉小数点后面无效0的方案与建议
2023-11-29 11:46:57
一小时迅速入门Mybatis之初识篇
2023-07-20 10:27:23
![](https://img.aspxhome.com/file/2023/2/132212_0s.jpg)
详解Java的MyBatis框架和Spring框架的整合运用
2022-09-19 19:22:20
java实现Dijkstra最短路径算法
2022-11-30 21:02:15
![](https://img.aspxhome.com/file/2023/5/71915_0s.jpg)
Java的JDBC中Statement与CallableStatement对象实例
2022-11-14 17:22:08
JAVA熔断和降级真实关系的图文详解
2023-11-30 12:39:20
![](https://img.aspxhome.com/file/2023/7/67957_0s.png)
Android自定义星星可滑动评分控件
2022-03-18 10:23:03
![](https://img.aspxhome.com/file/2023/6/130436_0s.png)
Eclipse项目有红感叹号的解决方法
2023-02-13 10:54:15
Android 模仿iPhone列表数据View刷新动画详解
2021-10-05 07:57:47
![](https://img.aspxhome.com/file/2023/2/120162_0s.gif)
Qt QTableWidget基本操作及使用
2022-05-22 23:15:55
![](https://img.aspxhome.com/file/2023/5/117775_0s.gif)
C#高性能动态获取对象属性值的步骤
2022-08-13 13:23:12
![](https://img.aspxhome.com/file/2023/7/104297_0s.png)
C#如何将DLL打包到程序中
2022-08-06 13:18:57
![](https://img.aspxhome.com/file/2023/5/79095_0s.png)
Android显示系统SurfaceFlinger详解
2022-11-02 12:39:53
![](https://img.aspxhome.com/file/2023/3/122063_0s.jpg)
Spring @Cacheable指定失效时间实例
2022-12-06 08:58:35
![](https://img.aspxhome.com/file/2023/5/77385_0s.png)
java获取json中的全部键值对实例
2023-08-23 19:53:03
通过String.intern()方法浅谈堆中常量池
2022-07-06 05:14:01