Android:圆环进度控件
来源:互联网 发布:社交软件发展前景 编辑:程序博客网 时间:2024/05/16 12:01
import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Typeface;import android.text.TextUtils;import android.util.AttributeSet;import android.view.View;/** * 圆环进度比例控件 */public class RoundPercentView extends View { private Context context; private Paint paint; /** * 圆环线颜色 */ private int roundColor; /** * 圆环进度线颜色 */ private int roundProgressColor; /** * 圆环线宽度 */ private float roundWidth; /** * 百分比字体颜色 */ private int percentTextColor; /** * 百分比字体大小 */ private float percentTextSize; /** * 百分比字体颜色 */ private int percentColor; /** * 百分号字体大小 */ private float percentSize; /** * 战绩字体颜色 */ private int resultTextColor; /** * 战绩字体大小 */ private float resultTextSize; /** * 最大进度 */ private int max = 100; /** * 当前进度 */ private int progress; /** * 战绩结果 */ private String result; public RoundPercentView(Context context) { this(context, null); } public RoundPercentView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundPercentView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; paint = new Paint(); //获取默认值 roundColor = context.getResources().getColor(R.color.round_color); roundProgressColor = context.getResources().getColor(R.color.round_progress_color); percentTextColor = context.getResources().getColor(R.color.round_text_color); percentColor = context.getResources().getColor(R.color.theme_text_normal); percentTextSize = CommonUtil.dipToPx(context, 26); percentSize = CommonUtil.dipToPx(context, 13); roundWidth = CommonUtil.dipToPx(context, 1); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int centerXY = getWidth() / 2; int radius = (int) (centerXY - roundWidth / 2); /* 画最外层的大圆环 */ paint.setColor(roundColor); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(roundWidth); paint.setAntiAlias(true); canvas.drawCircle(centerXY, centerXY, radius, paint); /* 画圆环的进度 */ paint.setStrokeWidth(roundWidth); paint.setColor(roundProgressColor); RectF rectF = new RectF(centerXY - radius, centerXY - radius, centerXY + radius, centerXY + radius); paint.setStyle(Paint.Style.STROKE); canvas.drawArc(rectF, -90, 360 * progress / max, false, paint); /* 画百分比文字 */ paint.setStrokeWidth(0); paint.setColor(percentTextColor); paint.setTextSize(percentTextSize); paint.setTypeface(Typeface.DEFAULT); int percent = (int) (((float) progress / (float) max) * 100); float percentTextWidth = paint.measureText(percent + ""); if (TextUtils.isEmpty(result)) { canvas.drawText(percent + "", centerXY - percentTextWidth / 2, centerXY + percentTextSize / 2, paint); } else { canvas.drawText(percent + "", centerXY - percentTextWidth / 2, centerXY + percentTextSize / 2 - resultTextSize / 2, paint); } /* 画百分号 */ paint.setColor(percentColor); paint.setTextSize(percentSize); float percentWidth = paint.measureText("%"); canvas.drawText("%", centerXY + percentTextWidth / 2 + CommonUtil.dipToPx(context, 3), centerXY + percentWidth / 2, paint); /* 画战绩结果文字 */ if (!TextUtils.isEmpty(result)) { paint.setStrokeWidth(0); paint.setColor(resultTextColor); paint.setTextSize(resultTextSize); paint.setTypeface(Typeface.DEFAULT); float resultTextWidth = paint.measureText(result); canvas.drawText(result, centerXY - resultTextWidth / 2, centerXY + resultTextSize / 2 + resultTextSize, paint); } } /** * 设置圆环和百分比数据,必须设置 * * @param progress 当前进度值 * @param max 最大进度值 * @param percentTextColor 百分比文字颜色 * @param percentTextSize 百分比文字字体大小 */ public synchronized void setCricleData(int progress, int max, int percentTextColor, float percentTextSize) { if (max > 0 && progress <= max) { this.max = max; this.progress = progress; } this.percentTextColor = percentTextColor; postInvalidate(); } /** * 设置战绩结果数据,可以不设置 * * @param result * @param resultTextColor * @param resultTextSize */ public synchronized void setResultData(String result, int resultTextColor, float resultTextSize) { this.result = result; this.resultTextColor = resultTextColor; this.resultTextSize = resultTextSize; postInvalidate(); }}
0 0
- Android:圆环进度控件
- Android学习之自定义控件-圆环进度条加内圆填充进度
- Android圆环控件
- 圆环进度
- 圆环进度
- Android 自定义View实例之进度圆环
- android 自定义pm2.5进度显示圆环
- Android开发 自定义动态进度圆环
- 自定义控件Android圆环进度条
- 自定义控件,动画效果的进度圆环RingProgressBar
- <Android>进度控件seekbar
- 自定义进度圆环
- 自定义圆环进度
- canvas圆环百分比进度
- android自定义圆环控件 滑动选择百分比
- Android自定义控件之百分比圆环进度条
- Android自定义控件之百分比圆环进度条
- Android自定义控件之百分比圆环进度条
- java判断文件后缀与前缀是否是设定的值
- 五步精听法
- spring加载properties文件,并将属性值注入到指定类的指定成员变量
- 大姐大经济的
- ssh整合问题
- Android:圆环进度控件
- 哈希的拓展——位图
- Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide
- Android dialog 详解七中方法!
- Spring transaction 事务 --Isolation & Progation
- 原来多线程中的join()是这么回事
- 图像平移变换
- RecyclerView 使用时空指针异常
- Apache Log4j配置说明