圆角矩形精度条

public class DownProgressBar extends View {
  /**
     * 注意:
     * 1,整个精度条的宽度 MaxProgress;
     * 2,当前精度条的宽度 currentProgress;
     * 4,背景颜色
     * 5,进度颜色
     * 6,文字,文字颜色,大小
     *
     * @param context
     */
private int mMaxProgress;
    private int mProgress;
    private int mBackgroundColor;
    private int mProgressColor;
    private String mProgressText;
    private int mProgressTextColor;
    private int mProgressTextSize;
    private Paint mBackgroundPaint;
    private Paint mProgressPaint;
    private Paint mProgressTextPaint;
    private static final int DEFAULT_MAX_PROGRESS = 100
private static final int DEFAULT_PROGRESS = 0;
    private static final int DEFAULT_BACKGROUND_COLOR = ParseStringColor("#000000");
    private static final int DEFAULT_PROGRESS_COLOR = ParseStringColor("#6FCADF");
    private static final int DEFAULT_PROGRESS_TEXT_COLOR = ParseStringColor("#000000");
    private static final int DEFAULT_PROGRESS_TEXT_SIZE = 10;
    private static final float DEFAULT_STORK_WIDTH = 5f;
    private static final String TAG = DownProgressBar.class.getSimpleName();
 public DownProgressBar(Context context) {
        this(context, null, 0);
    }

    public DownProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

public DownProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DownProgressBar);
        mMaxProgress = a.getInt(R.styleable.DownProgressBar_down_progress_bar_max, DEFAULT_MAX_PROGRESS);
        mProgress = a.getInt(R.styleable.DownProgressBar_down_progress_bar_cur, DEFAULT_PROGRESS);
        mBackgroundColor = a.getColor(R.styleable.DownProgressBar_down_progress_bar_background_color, DEFAULT_BACKGROUND_COLOR);

        mProgressColor = a.getColor(R.styleable.DownProgressBar_down_progress_bar_progress_color, DEFAULT_PROGRESS_COLOR);
        mProgressText = a.getString(R.styleable.DownProgressBar_down_progress_bar_text);
        mProgressTextColor = a.getColor(R.styleable.DownProgressBar_down_progress_bar_text_color, DEFAULT_PROGRESS_TEXT_COLOR);
        mProgressTextSize = a.getDimensionPixelSize(R.styleable.DownProgressBar_down_progress_bar_text_size, DEFAULT_PROGRESS_TEXT_SIZE);
        a.recycle();
        initView();
    }
 private void initView() {
        mBackgroundPaint = new Paint();
        mBackgroundPaint.setStrokeWidth(DEFAULT_STORK_WIDTH);
        mBackgroundPaint.setColor(mBackgroundColor);
        mBackgroundPaint.setStyle(Paint.Style.FILL);
        mBackgroundPaint.setAntiAlias(true);
        mProgressPaint = new Paint();
        mProgressPaint.setStrokeWidth(DEFAULT_STORK_WIDTH);
        mProgressPaint.setAntiAlias(true);
        mProgressPaint.setStyle(Paint.Style.FILL);
        mProgressPaint.setColor(mProgressColor);
        mProgressTextPaint = new Paint();
        mProgressTextPaint.setStrokeWidth(DEFAULT_STORK_WIDTH);
        mProgressTextPaint.setAntiAlias(true);
        mProgressTextPaint.setStyle(Paint.Style.FILL);
        mProgressTextPaint.setColor(mProgressTextColor);
        mProgressTextPaint.setTextSize(mProgressTextSize);
        mProgressTextPaint.setTextAlign(Paint.Align.CENTER);

    }

  @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int cWidth = canvas.getWidth();
        int cHeight = canvas.getHeight();
        //画背景图形,圆角的
        //mBackgroundPaint
        RectF backGroundRectf = new RectF(0, 0, cWidth, cHeight);
        float Ry = cHeight / 2;
        float Rx = cHeight / 2;
        canvas.drawRoundRect(backGroundRectf, Rx, Ry, mBackgroundPaint);
        int progress = (int) ((mProgress * 1.0 / mMaxProgress) * cWidth);
        LogUtils.LOGE(TAG, "downLoadProgress   >>>>>>>>" + progress);
        //刚开始的时候
         if (progress > 0) {
            if (progress < cWidth - (cHeight / 2)) {
                RectF rectF = new RectF(0, 0, cHeight, cHeight);
                if (180 - (progress * 5) > 90) {
                    canvas.drawArc(rectF, 180 - (progress * 5), 2 * (progress * 5), false, mProgressPaint);
                } else {
                    canvas.drawArc(rectF, 90, 180, true, mProgressPaint);
                    RectF progressRectf = new RectF(cHeight / 2, 0, progress, cHeight);
                    canvas.drawRect(progressRectf, mProgressPaint);
                } 
          } else if (progress >= cWidth - (cHeight / 2)) {
                RectF rectF = new RectF(0, 0, cHeight, cHeight);
                canvas.drawArc(rectF, 90, 180, true, mProgressPaint);
                RectF progressRectf = new RectF(cHeight / 2, 0, cWidth - (cHeight / 2), cHeight);
                canvas.drawRect(progressRectf, mProgressPaint);

                if (progress < cWidth) {
                    int rightPro = cWidth - progress;
                    RectF rectFr = new RectF(cWidth - cHeight, 0, cWidth, cHeight);
                    Log.d("gxl", "rightPro   >>>>" + rightPro);
                    canvas.drawArc(rectFr, 270, 180, false, mProgressPaint);
                    canvas.drawArc(rectFr, 360 - (rightPro * 5), 2 * (rightPro * 5), false, mBackgroundPaint);
                 } else {
                    RectF rectFr = new RectF(cWidth - cHeight, 0, cWidth, cHeight);
                    canvas.drawArc(rectFr, 270, 180, false, mProgressPaint);
                }

            }
        }
        //文字
        canvas.drawText(mProgressText, cWidth / 2, (cHeight + mProgressTextSize - DEFAULT_STORK_WIDTH) / 2, mProgressTextPaint);


    }
  private static int ParseStringColor(String colorStr) {
        return Color.parseColor(colorStr);
    }
 /**
     * 设置最大进度
     *
     * @param mMaxProgress
     */
    public void setmMaxProgress(int mMaxProgress) {
        this.mMaxProgress = mMaxProgress;
    }
 /**
     * 设置当前的进度
     *
     * @param mProgress
     */
    public void setmProgress(int mProgress) {
        this.mProgress = mProgress;
        invalidate();
    }
 /**
     * 设置当前文本
     *
     * @param mProgressText
     */
    public void setmProgressText(String mProgressText) {
        this.mProgressText = mProgressText;
        invalidate();
    }
 public String getmProgressText() {
        return mProgressText;
    }

public void setmProgressTextColor(int mProgressTextColor) {
        this.mProgressTextColor = mProgressTextColor;
        mProgressTextPaint.setColor(mProgressTextColor);
        invalidate();
    }

 public void setmBackgroundColor(int mBackgroundColor) {
        this.mBackgroundColor = mBackgroundColor;
        mBackgroundPaint.setColor(mBackgroundColor);
        invalidate();
    }


}



attrs文件内容

<!-- /**
* 注意:
* 1,整个精度条的宽度 MaxProgress;
* 2,当前精度条的宽度 currentProgress;
* 4,背景颜色
* 5,进度颜色
* 6,文字,文字颜色,大小
*
* @param context
*/-->

<declare-styleable name="DownProgressBar">
<attr name="down_progress_bar_max" format="integer"></attr>
<attr name="down_progress_bar_cur" format="integer"></attr>
<attr name="down_progress_bar_background_color" format="color"></attr>
<attr name="down_progress_bar_progress_color" format="color"></attr>

<attr name="down_progress_bar_text" format="string"></attr>

<attr name="down_progress_bar_text_color" format="color"></attr>
<attr name="down_progress_bar_text_size" format="dimension"></attr>
</declare-styleable>

 

 

posted @ 2018-10-19 11:50  Recol  阅读(133)  评论(0编辑  收藏  举报