当前位置:   article > 正文

Android 自定义横向进度条_android自定义横向进度条

android自定义横向进度条

Android 自定义横向进度条

一、工具类如下代码,可自定义进度条背景色,边框颜色,进度值设置等

public class ZzHorizontalProgressBar extends View {
    private int max;
    private double progress;
    private int bgColor;
    private int progressColor;
    private int padding;
    private boolean openGradient;
    private int gradientFrom;
    private int gradientTo;
    private boolean showSecondProgress;
    private int secondProgress;
    private int secondProgressShape;
    private boolean showZeroPoint;

    private Paint secondProgressPaint;
    private Paint secondGradientPaint;
    private Paint progressPaint;
    private Paint gradientPaint;
    private Paint bgPaint;
    private boolean openSecondGradient;
    private int secondGradientFrom;
    private int secondGradientTo;
    private int secondProgressColor;

    private int radius;
    private boolean drawBorder = false;
    private int borderColor;
    private int borderWidth;

    /**
     * 文字百分比的字体大小(sp)
     */
    private int paintTextSize;

    /**
     * 百分比文字的颜色
     */
    private int paintTextColor;
    /**
     * 画中间的百分比文字的画笔
     */
    private Paint paintText;

    /**
     * 要画的文字的宽度
     */
    private int textWidth;

    /**
     * 画文字时底部的坐标
     */
    private float textBottomY;
    /**
     * 得到自定义视图的Y轴中心点
     */
    private int viewCenterY;
    /**
     * 包裹文字的矩形
     */
    private Rect rect = new Rect();
    /**
     * 得到自定义视图的宽度
     */
    private int viewWidth;

    /**
     * 文字总共移动的长度(即从0%到100%文字左侧移动的长度)
     */
    private int totalMovedLength;
    /**
     * Contxt
     */
    private Context context;

    //    private boolean useRectMode = false;
    private int showMode = 0;
    private Paint borderPaint;

    public static enum ShowMode {
        ROUND, RECT, ROUND_RECT
    }


    private String mText;


    private OnProgressChangedListener onProgressChangedListener;

    public interface OnProgressChangedListener {
        void onProgressChanged(ZzHorizontalProgressBar progressBar, int max, double progress);

        void onSecondProgressChanged(ZzHorizontalProgressBar progressBar, int max, double progress);
    }

    public ZzHorizontalProgressBar(Context context) {
        super(context);
        this.context = context;
        init(context, null);
    }

    public ZzHorizontalProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ZzHorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        initAttrs(context, attrs);
        initPaths();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZzHorizontalProgressBar);
        max = a.getInteger(R.styleable.ZzHorizontalProgressBar_zpb_max, 100);
        progress = a.getInteger(R.styleable.ZzHorizontalProgressBar_zpb_progress, 0);
        bgColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_bg_color, 0xffB8B8B8);
        progressColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_pb_color, 0xffFF4081);
        secondProgressColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_second_pb_color, 0xffFF4081);
        padding = a.getDimensionPixelSize(R.styleable.ZzHorizontalProgressBar_zpb_padding, 0);
        showZeroPoint = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_show_zero_point, false);
        showSecondProgress = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_show_second_progress, false);
        secondProgress = a.getInteger(R.styleable.ZzHorizontalProgressBar_zpb_second_progress, 0);
        secondProgressShape = a.getInteger(R.styleable.ZzHorizontalProgressBar_zpb_show_second_point_shape, 0);
        openGradient = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_open_gradient, false);
        gradientFrom = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_gradient_from, 0xffFF4081);
        paintTextColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_text, 0xffcd5c5c);
        gradientTo = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_gradient_to, 0xffFF4081);
        openSecondGradient = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_open_second_gradient, false);
        showMode = a.getInt(R.styleable.ZzHorizontalProgressBar_zpb_show_mode, 0);
        secondGradientFrom = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_second_gradient_from, 0xffFF4081);
        secondGradientTo = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_second_gradient_to, 0xffFF4081);
        radius = a.getDimensionPixelSize(R.styleable.ZzHorizontalProgressBar_zpb_round_rect_radius, 20);
        drawBorder = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_draw_border, false);
        borderWidth = a.getDimensionPixelSize(R.styleable.ZzHorizontalProgressBar_zpb_border_width, 1);
        paintTextSize = a.getDimensionPixelSize(R.styleable.ZzHorizontalProgressBar_zpb_border_width, 20);
        borderColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_border_color, 0xffff001f);
        a.recycle();
    }

    private void initPaths() {
        progressPaint = new Paint();
        progressPaint.setColor(progressColor);
        progressPaint.setStyle(Paint.Style.FILL);
        progressPaint.setAntiAlias(true);

        secondProgressPaint = new Paint();
        secondProgressPaint.setColor(secondProgressColor);
        secondProgressPaint.setStyle(Paint.Style.FILL);
        secondProgressPaint.setAntiAlias(true);

        gradientPaint = new Paint();
        gradientPaint.setStyle(Paint.Style.FILL);
        gradientPaint.setAntiAlias(true);

        secondGradientPaint = new Paint();
        secondGradientPaint.setStyle(Paint.Style.FILL);
        secondGradientPaint.setAntiAlias(true);

        bgPaint = new Paint();
        bgPaint.setColor(bgColor);
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setAntiAlias(true);

        borderPaint = new Paint();
        borderPaint.setColor(borderColor);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(borderWidth);
        borderPaint.setAntiAlias(true);

        //设置百分比文字的尺寸
//        int paintTextSizePx = sp2px(context, paintTextSize);
        // 百分比文字画笔的属性
        paintText = new Paint();
        mText = "Udf32fA";
//        paintText.setColor(Color.BLACK);
        paintText.setColor(Color.parseColor("#333333"));
        paintText.setTextSize(30);
        paintText.setAntiAlias(true);
        paintText.setTypeface(Typeface.DEFAULT_BOLD);
        //获得绘制文本的宽和高
        rect = new Rect();
        paintText.getTextBounds(mText, 0, mText.length(), rect);

    }

    /**
     * 得到视图等的高度宽度尺寸数据
     */
    private void getWidthAndHeight() {
        //得到包围文字的矩形的宽高
        paintText.getTextBounds("000%", 0, "000%".length(), rect);
        textWidth = rect.width();
        textBottomY = viewCenterY + rect.height() / 2;

        //得到自定义视图的高度
        int viewHeight = getMeasuredHeight();
        viewWidth = getMeasuredWidth();
        viewCenterY = viewHeight / 2;
        totalMovedLength = viewWidth - textWidth;

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (showMode) {
            case 0:
                //half cicle
                drawBackground(canvas);
                drawProgress(canvas);
                drawBorder(canvas);
                break;
            case 1:
                //rect
                drawBackgroundRectMode(canvas);
                drawProgressRectMode(canvas);
                drawBorderRect(canvas);
                break;
            case 2:
                //custom radius
                drawBackgroundRoundRectMode(canvas);
                drawProgressRoundRectMode(canvas);
                drawBorderRoundRect(canvas);
                break;
        }


//            canvas.drawText(progress + "%",progress, padding + progressHeight / 2, paintText);
//        canvas.drawText(mText + "%", percent, width, paintText);
//        canvas.drawText(progress + "%", getWidth() / 2 - rect.width() / 2, getHeight() / 2 + rect.height() / 2, paintText);
        Log.i("aaaa", "onDraw: " + (getWidth() / 2 - rect.width() / 2) + "------------" + (getHeight() / 2 + rect.height() / 2));

    }

    private void drawProgress(Canvas canvas) {
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width - 1;
        }
        double percent = 0;
        if (max != 0) {
            percent = progress * 1.0f / max;
        }
        int progressHeight = getHeight() - padding * 2;
        if (progressHeight % 2 != 0) {
            progressHeight = progressHeight - 1;
        }


        if (openGradient) {
            int progressWidth = width - padding * 2;
            float mDx = convertToFloat(progressWidth * percent);


            int colors[] = new int[2];
            float positions[] = new float[2];
            //from color
            colors[0] = gradientFrom;
            positions[0] = 0;
            //to color
            colors[1] = gradientTo;
            positions[1] = 1;
            LinearGradient shader = new LinearGradient(
                    padding + progressHeight / 2, padding, padding + progressHeight / 2 + mDx, padding + progressHeight,
                    colors,
                    positions,
                    Shader.TileMode.MIRROR);
            //gradient
            gradientPaint.setShader(shader);

            int radius = width > getHeight() ? getHeight() / 2 : width / 2;
            if (mDx < getHeight()) {
                //left circle
                if (progress == 0) {
                    if (showZeroPoint) {
                        canvas.drawCircle(padding + progressHeight / 2, padding + progressHeight / 2, progressHeight / 2, gradientPaint);
                    }
                } else {
                    canvas.drawCircle(padding + progressHeight / 2, padding + progressHeight / 2, progressHeight / 2, gradientPaint);
                }

            } else {
                //progress line
                RectF rectF = new RectF(padding, padding, padding + mDx, padding + progressHeight);
                canvas.drawRoundRect(rectF, radius, radius, gradientPaint);
            }

        } else {
            int progressWidth = width - padding * 2 - progressHeight;
            float dx = convertToFloat(progressWidth * percent);
            progressPaint.setColor(progressColor);
            //left circle
            if (progress == 0) {
                if (showZeroPoint) {
                    canvas.drawCircle(padding + progressHeight / 2, padding + progressHeight / 2, progressHeight / 2, progressPaint);
                }
            } else {
                canvas.drawCircle(padding + progressHeight / 2, padding + progressHeight / 2, progressHeight / 2, progressPaint);
            }


            //right circle
            if (progress == 0) {
                if (showZeroPoint) {
                    canvas.drawCircle(padding + progressHeight / 2 + dx, padding + progressHeight / 2, progressHeight / 2, progressPaint);
                }
            } else {
                canvas.drawCircle(padding + progressHeight / 2 + dx, padding + progressHeight / 2, progressHeight / 2, progressPaint);
            }
            //middle line
            RectF midRecf = new RectF(padding + progressHeight / 2, padding, padding + progressHeight / 2 + dx, padding + progressHeight);
            canvas.drawRect(midRecf, progressPaint);


            //得到float型进度
            float progressFloat = convertToFloat(progress / 100.0f);
            //当前文字移动的长度
            float currentMovedLentgh = totalMovedLength * progressFloat;
//            canvas.drawText(progress + "%", (getWidth() / 2 - rect.width() / 2) + progress, getHeight() / 2 + rect.height() / 2, paintText);
            if (progress > 95) {
                canvas.drawText(progress + "%", currentMovedLentgh - textWidth / 2, getHeight() / 2 + rect.height() / 2, paintText);
            } else {
                canvas.drawText(progress + "%", currentMovedLentgh + textWidth, getHeight() / 2 + rect.height() / 2, paintText);
            }

        }

        //draw second progress
        if (showSecondProgress) {
            float s_percent = 0;
            if (max != 0) {
                s_percent = secondProgress * 1.0f / max;
            }
            int s_progressHeight = getHeight() - padding * 2;
            if (s_progressHeight % 2 != 0) {
                s_progressHeight = s_progressHeight - 1;
            }
            if (openSecondGradient) {
                int s_progressWidth = width - padding * 2;
                float s_mDx = s_progressWidth * s_percent;

                int s_colors[] = new int[2];
                float s_positions[] = new float[2];
                //from color
                s_colors[0] = secondGradientFrom;
                s_positions[0] = 0;
                //to color
                s_colors[1] = secondGradientTo;
                s_positions[1] = 1;
                LinearGradient s_shader = new LinearGradient(
                        padding + s_progressHeight / 2, padding, padding + s_progressHeight / 2 + s_mDx, padding + s_progressHeight,
                        s_colors,
                        s_positions,
                        Shader.TileMode.MIRROR);
                //gradient
                secondGradientPaint.setShader(s_shader);

                int s_radius = width > getHeight() ? getHeight() / 2 : width / 2;
                if (s_mDx < getHeight()) {
                    //left circle
                    if (secondProgress == 0) {
                        if (showZeroPoint) {
                            canvas.drawCircle(padding + s_progressHeight / 2, padding + s_progressHeight / 2, s_progressHeight / 2, secondGradientPaint);
                        }
                    } else {
                        canvas.drawCircle(padding + s_progressHeight / 2, padding + s_progressHeight / 2, s_progressHeight / 2, secondGradientPaint);
                    }
                } else {
                    //progress line
                    RectF rectF = new RectF(padding, padding, padding + s_mDx, padding + s_progressHeight);
                    canvas.drawRoundRect(rectF, s_radius, s_radius, secondGradientPaint);
                }
            } else {
                //no gradient
                if (secondProgressShape == 0) {
                    //point shape
                    int s_progressWidth = width - padding * 2;
                    float s_mDx = s_progressWidth * s_percent;
                    //progress line
                    float px = padding + s_progressHeight / 2 + s_mDx;
                    if (px < width - padding - s_progressHeight / 2) {
                        if (secondProgress == 0) {
                            if (showZeroPoint) {
                                canvas.drawCircle(px, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
                            }
                        } else {
                            canvas.drawCircle(px, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
                        }
                    } else {
                        canvas.drawCircle(px - s_progressHeight, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
                    }

                } else {
                    //line shape
                    int s_progressWidth = width - padding * 2 - s_progressHeight;
                    float dx = s_progressWidth * s_percent;
                    secondProgressPaint.setColor(secondProgressColor);
                    //left circle
                    if (secondProgress == 0) {
                        if (showZeroPoint) {
                            canvas.drawCircle(padding + s_progressHeight / 2, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
                        }
                    } else {
                        canvas.drawCircle(padding + s_progressHeight / 2, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
                    }
                    //right circle
                    if (secondProgress == 0) {
                        if (showZeroPoint) {
                            canvas.drawCircle(padding + s_progressHeight / 2 + dx, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
                        }
                    } else {
                        canvas.drawCircle(padding + s_progressHeight / 2 + dx, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
                    }
                    //middle line
                    RectF midRecf = new RectF(padding + s_progressHeight / 2, padding, padding + s_progressHeight / 2 + dx, padding + s_progressHeight);
                    canvas.drawRect(midRecf, secondProgressPaint);
                }
            }


        }


    }


    public static Float convertToFloat(Double doubleValue) {
        return doubleValue == null ? null : doubleValue.floatValue();
    }

    //
    private void drawProgressRectMode(Canvas canvas) {
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width - 1;
        }
        float percent = 0;
        if (max != 0) {
            percent = convertToFloat(progress * 1.0f / max);
        }
        int progressHeight = getHeight() - padding * 2;
        if (progressHeight % 2 != 0) {
            progressHeight = progressHeight - 1;
        }
        if (openGradient) {
            int progressWidth = width - padding * 2;
            float mDx = progressWidth * percent;

            int colors[] = new int[2];
            float positions[] = new float[2];
            //from color
            colors[0] = gradientFrom;
            positions[0] = 0;
            //to color
            colors[1] = gradientTo;
            positions[1] = 1;
            LinearGradient shader = new LinearGradient(
                    padding + progressHeight / 2, padding, padding + progressHeight / 2 + mDx, padding + progressHeight,
                    colors,
                    positions,
                    Shader.TileMode.MIRROR);
            //gradient
            gradientPaint.setShader(shader);

            int radius = width > getHeight() ? getHeight() / 2 : width / 2;
            //progress line
            RectF rectF = new RectF(padding, padding, padding + mDx, padding + progressHeight);
            canvas.drawRect(rectF, gradientPaint);

        } else {
            int progressWidth = width - padding * 2;
            float dx = progressWidth * percent;
            progressPaint.setColor(progressColor);
            RectF midRecf = new RectF(padding, padding, padding + dx, padding + progressHeight);
            canvas.drawRect(midRecf, progressPaint);
        }

        //draw second progress
        if (showSecondProgress) {
            float s_percent = 0;
            if (max != 0) {
                s_percent = secondProgress * 1.0f / max;
            }
            int s_progressHeight = getHeight() - padding * 2;
            if (s_progressHeight % 2 != 0) {
                s_progressHeight = s_progressHeight - 1;
            }
            if (openSecondGradient) {
                int s_progressWidth = width - padding * 2;
                float s_mDx = s_progressWidth * s_percent;

                int s_colors[] = new int[2];
                float s_positions[] = new float[2];
                //from color
                s_colors[0] = secondGradientFrom;
                s_positions[0] = 0;
                //to color
                s_colors[1] = secondGradientTo;
                s_positions[1] = 1;
                LinearGradient s_shader = new LinearGradient(
                        padding + s_progressHeight / 2, padding, padding + s_progressHeight / 2 + s_mDx, padding + s_progressHeight,
                        s_colors,
                        s_positions,
                        Shader.TileMode.MIRROR);
                //gradient
                secondGradientPaint.setShader(s_shader);

                //progress line
                RectF rectF = new RectF(padding, padding, padding + s_mDx, padding + s_progressHeight);
                canvas.drawRect(rectF, secondGradientPaint);
            } else {
                //no gradient
                //line shape
                int s_progressWidth = width - padding * 2;
                float dx = s_progressWidth * s_percent;
                secondProgressPaint.setColor(secondProgressColor);
                RectF midRecf = new RectF(padding, padding, padding + dx, padding + s_progressHeight);
                canvas.drawRect(midRecf, secondProgressPaint);
            }
        }

    }

    private void drawProgressRoundRectMode(Canvas canvas) {
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width - 1;
        }
        float percent = 0;
        if (max != 0) {
            percent = convertToFloat(progress * 1.0f / max);
        }
        int progressHeight = getHeight() - padding * 2;
        if (progressHeight % 2 != 0) {
            progressHeight = progressHeight - 1;
        }
        if (openGradient) {
            int progressWidth = width - padding * 2 - borderWidth;
            float mDx = progressWidth * percent;

            int colors[] = new int[2];
            float positions[] = new float[2];
            //from color
            colors[0] = gradientFrom;
            positions[0] = 0;
            //to color
            colors[1] = gradientTo;
            positions[1] = 1;
            LinearGradient shader = new LinearGradient(
                    padding + progressHeight / 2, padding, padding + progressHeight / 2 + mDx, padding + progressHeight,
                    colors,
                    positions,
                    Shader.TileMode.MIRROR);
            //gradient
            gradientPaint.setShader(shader);
            //progress line
            RectF rectF = new RectF(padding + borderWidth / 2, padding + borderWidth / 2, padding + mDx + borderWidth / 2, padding + progressHeight - borderWidth / 2);
            canvas.drawRoundRect(rectF, radius, radius, gradientPaint);

        } else {
            int progressWidth = width - padding * 2 - borderWidth;
            float dx = progressWidth * percent;
            progressPaint.setColor(progressColor);
            RectF rectF = new RectF(padding + borderWidth / 2, padding + borderWidth / 2, padding + dx + borderWidth / 2, padding + progressHeight - borderWidth / 2);
            canvas.drawRoundRect(rectF, radius, radius, progressPaint);
        }

        //draw second progress
        if (showSecondProgress) {
            float s_percent = 0;
            if (max != 0) {
                s_percent = secondProgress * 1.0f / max;
            }
            int s_progressHeight = getHeight() - padding * 2;
            if (s_progressHeight % 2 != 0) {
                s_progressHeight = s_progressHeight - 1;
            }
            if (openSecondGradient) {
                int s_progressWidth = width - padding * 2;
                float s_mDx = s_progressWidth * s_percent;

                int s_colors[] = new int[2];
                float s_positions[] = new float[2];
                //from color
                s_colors[0] = secondGradientFrom;
                s_positions[0] = 0;
                //to color
                s_colors[1] = secondGradientTo;
                s_positions[1] = 1;
                LinearGradient s_shader = new LinearGradient(
                        padding + s_progressHeight / 2 + borderWidth / 2, padding + borderWidth / 2, padding + s_progressHeight / 2 + s_mDx - borderWidth / 2, padding + s_progressHeight - borderWidth / 2,
                        s_colors,
                        s_positions,
                        Shader.TileMode.MIRROR);
                //gradient
                secondGradientPaint.setShader(s_shader);

                //progress line
                RectF rectF = new RectF(padding + borderWidth / 2, padding + borderWidth / 2, padding + s_mDx - borderWidth / 2, padding + s_progressHeight - borderWidth / 2);
                canvas.drawRoundRect(rectF, radius, radius, secondGradientPaint);
            } else {
                //no gradient
                //line shape
                int s_progressWidth = width - padding * 2;
                float dx = s_progressWidth * s_percent;
                secondProgressPaint.setColor(secondProgressColor);
                RectF midRecf = new RectF(padding + borderWidth / 2, padding + borderWidth / 2, padding + dx - borderWidth / 2, padding + s_progressHeight - borderWidth / 2);
                canvas.drawRoundRect(midRecf, radius, radius, secondProgressPaint);
            }
        }

    }

    private void drawBackground(Canvas canvas) {
        int bgHeight = getHeight();
        if (bgHeight % 2 != 0) {
            bgHeight = bgHeight - 1;
        }
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width - 1;
        }

        //left circle
        canvas.drawCircle(bgHeight / 2, bgHeight / 2, bgHeight / 2, bgPaint);
        //right circle
        canvas.drawCircle(width - bgHeight / 2, bgHeight / 2, bgHeight / 2, bgPaint);
        //middle line
        RectF midRecf = new RectF(bgHeight / 2, 0, width - bgHeight / 2, bgHeight);
        canvas.drawRect(midRecf, bgPaint);

    }

    private void drawBorder(Canvas canvas) {
        if (drawBorder) {
            int bgHeight = getHeight();
            if (bgHeight % 2 != 0) {
                bgHeight = bgHeight - 1;
            }
            int width = getWidth();
            if (width % 2 != 0) {
                //Fix Me
                width = width - 1;
            }
            RectF rect = new RectF(0, 0, width, bgHeight);
            canvas.drawRoundRect(rect, bgHeight / 2, bgHeight / 2, borderPaint);
        }
    }

    private void drawBorderRect(Canvas canvas) {
        if (drawBorder) {
            int bgHeight = getHeight();
            if (bgHeight % 2 != 0) {
                bgHeight = bgHeight - 1;
            }
            int width = getWidth();
            if (width % 2 != 0) {
                //Fix Me
                width = width - 1;
            }
            RectF rect = new RectF(0, 0, width, bgHeight);
            canvas.drawRect(rect, borderPaint);
        }
    }

    private void drawBorderRoundRect(Canvas canvas) {
        if (drawBorder) {
            int bgHeight = getHeight();
            if (bgHeight % 2 != 0) {
                bgHeight = bgHeight - 1;
            }
            int width = getWidth();
            if (width % 2 != 0) {
                //Fix Me
                width = width - 1;
            }
            RectF rect = new RectF(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, bgHeight - borderWidth / 2);
            canvas.drawRoundRect(rect, radius, radius, borderPaint);
        }
    }

    private void drawBackgroundRectMode(Canvas canvas) {
        int bgHeight = getHeight();
        if (bgHeight % 2 != 0) {
            bgHeight = bgHeight - 1;
        }
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width - 1;
        }

        RectF midRecf = new RectF(0, 0, width, bgHeight);
        canvas.drawRect(midRecf, bgPaint);
    }

    private void drawBackgroundRoundRectMode(Canvas canvas) {
        int bgHeight = getHeight();
        if (bgHeight % 2 != 0) {
            bgHeight = bgHeight - 1;
        }
        int width = getWidth();
        if (width % 2 != 0) {
            //Fix Me
            width = width - 1;
        }

        RectF midRecf = new RectF(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, bgHeight - borderWidth / 2);
        canvas.drawRoundRect(midRecf, radius, radius, bgPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        getWidthAndHeight();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
        invalidate();
    }

    public double getProgress() {
        return progress;
    }

    public void setProgress(double progress) {
        if (progress < 0) {
            this.progress = 0;
        } else if (progress > max) {
            this.progress = max;
        } else {
            this.progress = progress;
        }
        invalidate();
        if (onProgressChangedListener != null) {
            onProgressChangedListener.onProgressChanged(this, max, this.progress);
        }
    }

    public boolean isShowSecondProgress() {
        return showSecondProgress;
    }

    public void setShowSecondProgress(boolean showSecondProgress) {
        this.showSecondProgress = showSecondProgress;
        invalidate();
    }

    public int getSecondProgress() {
        return secondProgress;
    }

    public void setSecondProgress(int secondProgress) {
        if (secondProgress < 0) {
            this.secondProgress = 0;
        } else if (secondProgress > max) {
            this.secondProgress = max;
        } else {
            this.secondProgress = secondProgress;
        }
        invalidate();
        if (onProgressChangedListener != null) {
            onProgressChangedListener.onSecondProgressChanged(this, max, this.secondProgress);
        }
    }

    public int getSecondProgressShape() {
        return secondProgressShape;
    }

    public void setSecondProgressShape(int secondProgressShape) {
        this.secondProgressShape = secondProgressShape;
        invalidate();
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        bgPaint.setColor(bgColor);
        invalidate();
    }

    public boolean isOpenSecondGradient() {
        return openSecondGradient;
    }

    public void setOpenSecondGradient(boolean openSecondGradient) {
        this.openSecondGradient = openSecondGradient;
        invalidate();
    }

    public int getSecondGradientFrom() {
        return secondGradientFrom;
    }

    public void setSecondGradientFrom(int secondGradientFrom) {
        this.secondGradientFrom = secondGradientFrom;
        invalidate();
    }

    public int getSecondGradientTo() {
        return secondGradientTo;
    }

    public void setSecondGradientTo(int secondGradientTo) {
        this.secondGradientTo = secondGradientTo;
        invalidate();
    }

    public int getSecondProgressColor() {
        return secondProgressColor;
    }

    public void setSecondProgressColor(int secondProgressColor) {
        this.secondProgressColor = secondProgressColor;
        secondProgressPaint.setColor(secondProgressColor);
        invalidate();
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        progressPaint.setColor(progressColor);
        invalidate();
    }

    public int getPadding() {
        return padding;
    }

    public void setPadding(int padding) {
        this.padding = padding;
        invalidate();
    }

    public void setShowMode(ShowMode showMode) {
        switch (showMode) {
            case ROUND:
                this.showMode = 0;
                break;
            case RECT:
                this.showMode = 1;
                break;
            case ROUND_RECT:
                this.showMode = 2;
                break;
        }
        invalidate();
    }

    /**
     * get the percentage value of progress and max.
     *
     * @return percentage value
     */
    public int getPercentage() {
        if (max == 0) {
            return 0;
        }
        return (int) (progress * 100.0 / max);
    }

    public boolean isOpenGradient() {
        return openGradient;
    }

    public void setOpenGradient(boolean openGradient) {
        this.openGradient = openGradient;
        invalidate();
    }

    public int getGradientFrom() {
        return gradientFrom;
    }

    public void setGradientFrom(int gradientFrom) {
        this.gradientFrom = gradientFrom;
        invalidate();
    }

    public int getGradientTo() {
        return gradientTo;
    }

    public void setGradientTo(int gradientTo) {
        this.gradientTo = gradientTo;
        invalidate();
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
        this.borderPaint.setColor(this.borderColor);
        invalidate();
    }

    public void setGradientColor(int from, int to) {
        this.gradientFrom = from;
        this.gradientTo = to;
        invalidate();
    }

    public void setSecondGradientColor(int from, int to) {
        this.secondGradientFrom = from;
        this.secondGradientTo = to;
        invalidate();
    }

    public void setGradientColorAndBorderColor(int from, int to, int borderColor) {
        this.gradientFrom = from;
        this.gradientTo = to;
        this.borderColor = borderColor;
        this.borderPaint.setColor(this.borderColor);
        invalidate();
    }

    public int getBorderColor() {
        return borderColor;
    }


    public void setOnProgressChangedListener(OnProgressChangedListener onProgressChangedListener) {
        this.onProgressChangedListener = onProgressChangedListener;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929
  • 930
  • 931
  • 932
  • 933
  • 934
  • 935
  • 936
  • 937
  • 938
  • 939
  • 940
  • 941
  • 942
  • 943
  • 944
  • 945
  • 946
  • 947
  • 948
  • 949
  • 950
  • 951

二、把下面代码拷贝到styles文件中


    <declare-styleable name="ZzHorizontalProgressBar">
        <attr name="zpb_padding" format="dimension" />
        <attr name="zpb_bg_color" format="color|reference" />
        <attr name="zpb_pb_color" format="color|reference" />
        <attr name="zpb_second_pb_color" format="color|reference" />
        <attr name="zpb_max" format="integer" />
        <attr name="zpb_text" format="color|reference" />
        <attr name="zpb_progress" format="integer" />
        <attr name="zpb_show_zero_point" format="boolean" />
        <attr name="zpb_show_second_progress" format="boolean" />
        <attr name="zpb_second_progress" format="integer" />
        <attr name="zpb_show_second_point_shape" format="enum">
            <enum name="point" value="0" />
            <enum name="line" value="1" />
        </attr>
        <attr name="zpb_open_gradient" format="boolean" />
        <attr name="zpb_gradient_from" format="color|reference" />
        <attr name="zpb_gradient_to" format="color|reference" />
        <attr name="zpb_open_second_gradient" format="boolean" />
        <attr name="zpb_second_gradient_from" format="color|reference" />
        <attr name="zpb_second_gradient_to" format="color|reference" />
        <attr name="zpb_show_mode" format="enum">
            <enum name="round" value="0" />
            <enum name="rect" value="1" />
            <enum name="round_rect" value="2" />
        </attr>
        <attr name="zpb_round_rect_radius" format="dimension|reference" />
        <attr name="zpb_draw_border" format="boolean" />
        <attr name="zpb_border_width" format="dimension|reference" />
        <attr name="zpb_border_color" format="color|reference" />

    </declare-styleable>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

三、效果图如下:

请添加图片描述

资源下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/365011
推荐阅读
相关标签
  

闽ICP备14008679号