Android 自定义view钟表
来源:互联网 发布:速拓软件价格 编辑:程序博客网 时间:2024/05/16 18:46
搞Android也有一段时间了,现在最大的收货是搬代码很快,生产代码就是蜗牛一样的速度了,博客就开始作为一个学习的记录自定义view的一个练习,在网上看到了很多关于钟表的,整合一个写了个自己的!废话不多说
- 关于钟表就是刻度 秒分时指针,数字时间显示,这里先画外圆再画刻度以及数字,最后画指针先上个效果图吧!不知道搞动态 就来个静态的吧
这里是继承的是view
public class TimeView extends View { private String[] times = {"5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60"};//每5个刻度显示数字 private Calendar mCalendar; public static final int NEED_INVALIDATE = 100; private int inRounds = 300;//钟表圆的半径 private int onRounds = 50;//内院的半径 private int textdistance = 200;//文字距离刻度线的距离 private int Secondhand=240;//秒针长度 private int Minutehand=200;//分针长度 private int Hourhand=160;//时针长度 private Context context; public TimeView(Context context) { super(context); } @RequiresApi(api = Build.VERSION_CODES.N) public TimeView(Context context, AttributeSet attrs) { super(context, attrs); mCalendar = Calendar.getInstance(); handler.sendEmptyMessage(NEED_INVALIDATE);//通过handler来进行更新 this.context = context; } public TimeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TimeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }
onDraw方法里面就是要绘制的内容了,关于Canvas 的绘制一般有几种方法
- canvas.drawCircle(float cx, float cy, float radius, Paint paint)//画圆
float cx :x轴坐标
float cy:y轴坐标
float radius:半径
paint:画笔 canvas.drawLine(float startX, float startY, float stopX, float stopY, Paint paint)//画线
float startX :开始x轴坐标
float startY:开始y轴坐标
float stopX:结束时x轴坐标
float stopY:结束时y轴坐标
paint:画笔canvas.rotate(float degrees, float px, float py)//以px,py平移,用到rotate就要和 canvas.save()以及canvas.restore()使用了
@RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint cylindricalpaint = new Paint();//外圆 cylindricalpaint.setAntiAlias(true); cylindricalpaint.setColor(Color.BLUE); cylindricalpaint.setStrokeWidth(5); cylindricalpaint.setStyle(Paint.Style.STROKE); canvas.drawCircle((getWidth()) / 2, (getHeight()) / 2, UiUtils.px2dip(context, inRounds), cylindricalpaint); Paint centerpaint = new Paint();//内圆 centerpaint.setAntiAlias(true); centerpaint.setColor(Color.BLUE); centerpaint.setStrokeWidth(5); centerpaint.setStyle(Paint.Style.STROKE); canvas.drawCircle((getWidth()) / 2, (getHeight()) / 2, UiUtils.px2dip(context, onRounds), centerpaint); Paint Line = new Paint();//刻度线和秒分钟的画笔 Line.setStrokeWidth(5); Line.setAntiAlias(true); Line.setColor(Color.YELLOW); Paint mPaintText = new Paint();//时间 mPaintText = new Paint(); mPaintText.setColor(Color.BLUE); mPaintText.setStrokeWidth(10); mPaintText.setTextAlign(Paint.Align.CENTER); mPaintText.setTextSize(20); /** * 以每个60分钟为60个刻度进行旋转绘制 */ for (int i = 1; i <= 60; i++) { canvas.save(); canvas.rotate(360 / 60 * i, (getWidth()) / 2, (getHeight()) / 2); /** * 画刻度线和文字 * 画直线的参数有startX:起始端点的X坐标。 startY:起始端点的Y坐标。 stopX:终止端点的X坐标。 stopY:终止端点的Y坐标。 paint:绘制直线所使用的画笔。 */ if (isIn(String.valueOf(i), times)) { canvas.drawText("" + i / 5, (getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, textdistance), mPaintText); canvas.drawLine((getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, inRounds), (getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, 240), Line); } else { canvas.drawLine((getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, inRounds), (getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, 280), Line); } canvas.restore(); } int hour = mCalendar.get(Calendar.HOUR); int minutes = mCalendar.get(Calendar.MINUTE); int sec = mCalendar.get(Calendar.SECOND); float hourrad = minutes / 60f * 360;//获取旋转读书 canvas.save(); canvas.rotate(hourrad, (getWidth()) / 2, (getHeight()) / 2); //左起:起始位置x坐标,起始位置y坐标,终止位置x坐标,终止位置y坐标,画笔(一个) canvas.drawLine((getWidth()) / 2, (getHeight()) / 2, (getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, Minutehand), Line); canvas.restore(); float hourDegree = (hour * 60 + minutes) / 12f / 60 * 360;//得到时钟旋转的角度 canvas.save(); canvas.rotate(hourDegree, (getWidth()) / 2, (getHeight()) / 2); canvas.drawLine((getWidth()) / 2, (getHeight()) / 2, (getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, Hourhand), Line); canvas.restore(); float secDegree = sec / 60f * 360;//得到秒针旋转的角度 canvas.save(); canvas.rotate(secDegree, (getWidth()) / 2, (getHeight()) / 2); canvas.drawLine((getWidth()) / 2, (getHeight()) / 2, (getWidth()) / 2, (getHeight()) / 2 - UiUtils.px2dip(context, Secondhand), Line); canvas.restore(); } //每隔一秒,在handler中调用一次重新绘制方法 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case NEED_INVALIDATE: mCalendar = Calendar.getInstance(); invalidate();//告诉UI主线程重新绘制 handler.sendEmptyMessageDelayed(NEED_INVALIDATE, 1000); break; default: break; } } };
1 0
- 【Android】自定义View -- 钟表
- Android 自定义view钟表
- android自定义View之钟表
- android自定义View之钟表诞生记
- android自定义view实现一个钟表
- 自定义View 钟表
- Android自定义控件——自定义View绘制钟表
- 自定义View之绘制钟表
- 自定义View仿小米钟表
- 自定义 View 实现钟表效果
- 自定义View --画个钟表
- android自定义钟表
- 安卓自定义View实现钟表
- 安卓自定义View实现钟表
- 自定义View-自制简单的钟表
- 自定义钟表
- 自定义View新手实战-一步步实现精美的钟表界面
- 自定义View新手实战-一步步实现精美的钟表界面
- Android例子—Fragment实例讲解—底部导航栏+ViewPager滑动切换页面
- 解决coreldraw缩略图不显示问题
- ARM WFI和WFE指令
- 1059
- Struts2 拦截器(Interceptor )原理和配置
- Android 自定义view钟表
- python中numpy使用
- P1020 导弹拦截
- Java反射机制常用方法
- 数据库读写分离
- 1060
- 备注
- 递推递归练习--F(计算组合数)
- 端口被占用