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
原创粉丝点击