设为首页 加入收藏

TOP

android-自定义控件之液位指示器(四)
2017-10-12 17:57:17 】 浏览:8753
Tags:android- 定义 控件 液位 指示器
ECIFIED) { result = Math.max(widthSpaceSize, getPaddingLeft() + getPaddingRight() + progressWidth + 2 * cheekWidth); } else if (widthSpaceMode == MeasureSpec.AT_MOST) { result = Math.min(widthSpaceSize, getPaddingLeft() + getPaddingRight() + progressWidth + 2 * cheekWidth); } return result; } /** * * @param heightMeasureSpec * @return获取高度尺寸 */ private int measurdHeight(int heightMeasureSpec) { int result = 0; int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec); int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec); if (heightSpaceMode == MeasureSpec.EXACTLY) { result = heightSpaceSize; heightZoom = heightSpaceSize/(progressHeight+2*cheekWidth); progressHeight = (int) (progressHeight*heightZoom); } else if (heightSpaceMode == MeasureSpec.UNSPECIFIED) { result = Math.max(heightSpaceSize, getPaddingTop() + getPaddingBottom() + progressHeight + 2 * cheekWidth); } else if (heightSpaceMode == MeasureSpec.AT_MOST) { result = Math.min(heightSpaceSize, getPaddingTop() + getPaddingBottom() + progressHeight + 2 * cheekWidth); } return result; } /** * 绘制控件 */ @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); radio = getProgress() * 1.0f / getMax(); progressNowHeight = (int) (progressHeight * radio); // 存绘画前画布状态 canvas.save(); // 把画布移动到要绘制的点 canvas.translate(getPaddingLeft(), getPaddingRight()); // 绘制容器外壳 paintCheek.setColor(cheekColor);// 画笔颜色 paintCheek.setAntiAlias(true);// 是否过度 paintCheek.setStyle(Style.STROKE);// 空心 paintCheek.setStrokeWidth(cheekWidth);// 边框的宽度 canvas.drawRect(cheekWidth/2, cheekWidth/2, progressWidth + cheekWidth*3/2, progressHeight + cheekWidth*3/2, paintCheek); // 绘总液位 paint.setColor(unReachedColor); paint.setStyle(Style.FILL); canvas.drawRect(cheekWidth, cheekWidth, progressWidth+cheekWidth, progressHeight+cheekWidth, paint); // 绘当前液位 paint.setStyle(Style.FILL); paint.setColor(reachedColor); canvas.drawRect(cheekWidth, cheekWidth + progressHeight - progressNowHeight, progressWidth + cheekWidth, progressHeight + cheekWidth, paint); // 绘液位指示 String text = getProgress() + "%"; paint.setTextSize(reachedTextsize); paint.setColor(reachedTextcolor); float textHeight = sp2px(reachedTextsize)/2; if(progressNowHeight >= progressHeight/2){ canvas.drawText(text, cheekWidth + progressWidth / 2, cheekWidth + progressHeight - progressNowHeight+textHeight, paint); }else{ canvas.drawText(text, cheekWidth + progressWidth / 2, cheekWidth + progressHeight - progressNowHeight, paint); } canvas.restore(); } }

  3.就是在布局中的引用了

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:jwwprogress="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res/com.example.jwwcallback"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    an
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android Material Design 下一篇春秋旅行安卓客户端源码项目

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目