自定义view实现仪表盘,时钟
2018-01-26 16:25
369 查看
简单粗暴上代码
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int mWidth = wm.getDefaultDisplay().getWidth(); int mHeight = wm.getDefaultDisplay().getHeight(); /* public int mWidth = getWidth(); public int mHeight = getHeight();*/ public YiBiaoPan(Context context) { super(context); } public YiBiaoPan(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public YiBiaoPan(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { //画外圆 Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, paint); Log.e("adress",mWidth+","+mHeight); canvas.drawLine(mWidth/2,mHeight/2,mWidth,mHeight,paint); //画刻度线 Paint paint1 = new Paint(); paint.setStrokeWidth(3); for (int i = 0; i < 24; i++) { if (i == 0 || i == 6 || i == 12 || i == 18) { paint1.setStrokeWidth(5); paint1.setTextSize(30); canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 60, paint1); String degree = String.valueOf(i); canvas.drawText( degree, mWidth / 2 - paint1.measureText(degree) / 2, mHeight / 2 - mWidth / 2 + 90, paint1 ); } else { paint1.setStrokeWidth(3); paint1.setTextSize(15); canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 30, paint1); String degree = String.valueOf(i); canvas.drawText( degree, mWidth / 2 - paint1.measureText(degree) / 2, mHeight / 2 - mWidth / 2 + 60, paint1 ); } canvas.rotate(15, mWidth / 2, mHeight / 2); } //画指针 Paint paintHour = new Paint(); paintHour.setStrokeWidth(20); Paint paintMinute = new Paint(); paintMinute.setStrokeWidth(10); canvas.save(); canvas.translate(mWidth/2,mHeight/2); canvas.drawLine(0,0,100,100,paintHour); canvas.drawLine(0,0,100,200,paintMinute); canvas.restore(); }
相关文章推荐
- Android自定义View----时钟/仪表盘的简单实现
- Android自定义View实现时钟效果ClockView
- android自定义view(实现时钟显示)
- android 自定义view实现时钟
- android 让TextView支持的Html标签实现自定义时钟显示组件DigitalClock
- Android自定义View——实现时钟效果
- 自定义View+Handle 实现模拟时钟效果
- 自定义View:圆形仪表盘,实现展示不同级别范围
- Android自定义View:如何实现一个模拟时钟?
- 自定义View——可拖动设置时间的时钟具体实现1
- android 自定义View 仪表盘 DashboardView 的实现
- 自定义View实现仪表盘(账户安全)
- Android自定义ClockView实现时钟效果
- 自定义View——可拖动设置时间的时钟具体实现2
- 60.自定义View练习(五)高仿小米时钟 - 使用Camera和Matrix实现3D效果
- ViewPager中小点的实现+自定义View中设置属性
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- <Android 进阶(一)> 自定义View之仪表盘进度条
- Android TextView 自定义ClickableSpan实现@ 超链接
- android:自定义HorizontalScrollView实现qq侧滑菜单