仿照群英传-自定义View时钟样式
2016-07-14 16:13
393 查看
/** * 主要是支持wrap_content属性 * @param widthMeasureSpec * @param heightMeasureSpec */
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else { float textWidth = mWatch_watch_width * 2; int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight()); width = desired; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else { float textHeight = mWatch_watch_height * 2; int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom()); height = desired; } setMeasuredDimension((int) width, (int) height); } /** * 画圆 * @param canvas */ @Override protected void onDraw(Canvas canvas) { //画圆 Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); paint.setStrokeWidth(5); canvas.drawCircle(mWatch_watch_width, mWatch_watch_height, mWatch_watch_width - 5, paint); //画刻度 Paint mPaintDegree = new Paint(); mPaintDegree.setStrokeWidth(3); for (int i = 0; i < 24; i++) { if (i == 0 || i == 6 || i == 12 || i == 18) { mPaintDegree.setStrokeWidth(5); mPaintDegree.setTextSize(30); canvas.drawLine(mWatch_watch_width, mWatch_watch_height - mWatch_watch_width + 5, mWatch_watch_width, mWatch_watch_height - mWatch_watch_width + 40, mPaintDegree); String degree = String.valueOf(i); canvas.drawText(degree, mWatch_watch_width - mPaintDegree.measureText(degree) / 2, mWatch_watch_height - mWatch_watch_width + 90, mPaintDegree); } else { mPaintDegree.setStrokeWidth(3); mPaintDegree.setTextSize(15); canvas.drawLine(mWatch_watch_width, mWatch_watch_height - mWatch_watch_width + 5, mWatch_watch_width, mWatch_watch_height - mWatch_watch_width + 20, mPaintDegree); String degree = String.valueOf(i); canvas.drawText(degree, mWatch_watch_width - mPaintDegree.measureText(degree) / 2, mWatch_watch_height - mWatch_watch_width + 60, mPaintDegree); } canvas.rotate(15, mWatch_watch_width, mWatch_watch_height); } //画时针 Paint mPaintHout = new Paint(); mPaintHout.setStrokeWidth(20); Paint mPaintMinute = new Paint(); mPaintMinute.setStrokeWidth(10); canvas.save(); //translate 将原点平移。 canvas.translate(mWatch_watch_width, mWatch_watch_height); canvas.drawLine(0, 0, 50, 80, mPaintHout); canvas.drawLine(0, 0, 100, 100, mPaintMinute); canvas.restore(); super.onDraw(canvas); }
github:https://github.com/yingtongwang/Hello
相关文章推荐
- CSS3 几步即可实现loading动画效果
- CSS魔法堂:重新认识Box Model、IFC、BFC和Collapsing margins
- css动画效果:实现鼠标移入菜单栏文字下出现下划线
- div+css 如何让背景层半透明 内容不透明
- CSS 模块化开发
- css让图片显示圆角 纯CSS绘制漂亮的圆形图案效果
- 用currentStyle和getComputedStyle来获取CSS非行间元素
- CSS 颜色名 颜色值
- CSS3的 transform属性,怎么才能让他同时执行多个不同动画(属性)效果
- CSS经验分享
- css3鼠标滑过图片文字动画特效
- css3-Transform与css3-Transition
- css随笔
- 好记性不如烂笔头之html和css笔记
- css限制显示字数,文字长度超出部分用省略号表示
- DIV半透明层 CSS来实现网页背景半透明
- Chrome谷歌浏览器下不支持css字体小于12px的解决办法
- 彻底弄懂css中单位px和em,rem的区别
- CSS3的calc()使用
- 【内容】css3点击区域4种全屏放大显示内容动画特效