android HorizontalProgressBarWithNumber, android 自定义pm横线分割线控件(非浸入式)绿,棕,红

public class HorizontalProgressBarWithNumber extends ProgressBar {

    private static final int DEFAULT_TEXT_SIZE = 10;
    private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;
    private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;
    private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;
    private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;
    private static final int DEFAULT_SIZE_TEXT_OFFSET = 10;

    /**
     * painter of all drawing things
     */
    protected Paint mPaint = new Paint();
    /**
     * color of progress number
     */
    protected int mTextColor = DEFAULT_TEXT_COLOR;
    /**
     * size of text (sp)
     */
    protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);

    /**
     * offset of draw progress
     */
    protected int mTextOffset = dp2px(DEFAULT_SIZE_TEXT_OFFSET);

    /**
     * height of reached progress bar
     */
    protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);

    /**
     * color of reached bar
     */
    protected int mReachedBarColor = DEFAULT_TEXT_COLOR;
    /**
     * color of unreached bar
     */
    protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;
    /**
     * height of unreached progress bar
     */
    protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);
    /**
     * view width except padding
     */
    protected int mRealWidth;

    protected boolean mIfDrawText = true;

    protected static final int VISIBLE = 0;
    private int mFirstBarColor;
    private int mSecondBarColor;
    private int mThridBarColor;

    public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs,
                                           int defStyle) {
        super(context, attrs, defStyle);

        setHorizontalScrollBarEnabled(true);

        obtainStyledAttributes(attrs);

        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);

    }

    /**
     * get the styled attributes
     *
     * @param attrs
     */
    private void obtainStyledAttributes(AttributeSet attrs) {
        // init values from custom attributes
        final TypedArray attributes = getContext().obtainStyledAttributes(
                attrs, R.styleable.HorizontalProgressBarWithNumber);

        mTextColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_progress_text_color,
                        DEFAULT_TEXT_COLOR);
        mTextSize = (int) attributes.getDimension(
                R.styleable.HorizontalProgressBarWithNumber_progress_text_size,
                mTextSize);

        mFirstBarColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_first_reached_color,
                        mTextColor);

        mSecondBarColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_second_reached_color,
                        mTextColor);

        mThridBarColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_third_reached_color,
                        mTextColor);


        mReachedBarColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_progress_reached_color,
                        mTextColor);
        mUnReachedBarColor = attributes
                .getColor(
                        R.styleable.HorizontalProgressBarWithNumber_progress_unreached_color,
                        DEFAULT_COLOR_UNREACHED_COLOR);
        mReachedProgressBarHeight = (int) attributes
                .getDimension(
                        R.styleable.HorizontalProgressBarWithNumber_progress_reached_bar_height,
                        mReachedProgressBarHeight);
        mUnReachedProgressBarHeight = (int) attributes
                .getDimension(
                        R.styleable.HorizontalProgressBarWithNumber_progress_unreached_bar_height,
                        mUnReachedProgressBarHeight);
        mTextOffset = (int) attributes
                .getDimension(
                        R.styleable.HorizontalProgressBarWithNumber_progress_text_offset,
                        mTextOffset);

        int textVisible = attributes
                .getInt(R.styleable.HorizontalProgressBarWithNumber_progress_text_visibility,
                        VISIBLE);
        if (textVisible != VISIBLE) {
            mIfDrawText = false;
        }
        attributes.recycle();
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec,
                                          int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        if (widthMode == MeasureSpec.AT_MOST) {
//            int a = 0;
//        }

        if (heightMode != MeasureSpec.EXACTLY) {

            float textHeight = (mPaint.descent() + mPaint.ascent());
            int exceptHeight = (int) (getPaddingTop() + getPaddingBottom() + Math
                    .max(Math.max(mReachedProgressBarHeight,
                            mUnReachedProgressBarHeight), Math.abs(textHeight * 3)));

            heightMeasureSpec = MeasureSpec.makeMeasureSpec(exceptHeight,
                    MeasureSpec.EXACTLY);
            //宽度
            int width = getScreenWidth();
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);

        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    /**
     * 得到屏幕宽度
     *
     * @return
     */
    private int getScreenWidth() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        //画笔平移到指定paddingLeft getHeight() / 2位置,注意以后坐标都为以此为00
        canvas.translate(getPaddingLeft() + mRealWidth / 4, getHeight() / 2);

        boolean noNeedBg = false;
        //当前进度和总值的比例
        float radio = getProgress() * 1.0f / getMax();
        //已到达的宽度
        float progressPosX = (int) (mRealWidth * radio);
//        //绘制的文本
//        String text_first = "75";
//
//        //拿到字体的宽度和高度
//        float textWidth = mPaint.measureText(text_first);
//        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;


//
//        //如果到达最后,则未到达的进度条不需要绘制
//        if (progressPosX + textWidth > mRealWidth) {
//            progressPosX = mRealWidth - textWidth;
//            noNeedBg = true;
//        }
//
//        // 绘制已到达的进度
//        float endX = progressPosX - mTextOffset / 2;
//        if (endX > 0) {
//            mPaint.setColor(mReachedBarColor);
//            mPaint.setStrokeWidth(mReachedProgressBarHeight);
//            canvas.drawLine(0, 0, endX, 0, mPaint);
//        }
//
//        // 绘制文本
//        if (mIfDrawText) {
//            mPaint.setColor(mTextColor);
//            canvas.drawText(text, progressPosX, -textHeight, mPaint);
//        }
//
//        // 绘制未到达的进度条
//        if (!noNeedBg) {
//            float start = progressPosX + mTextOffset / 2 + textWidth;
//            mPaint.setColor(mUnReachedBarColor);
//            mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
//            canvas.drawLine(start, 0, mRealWidth, 0, mPaint);
//        }

        /**
         * 绘制pm2.5 分割线 ,绿,棕,红
         */
        int first = mRealWidth / 2 / 3;
        mPaint.setColor(mFirstBarColor);
        mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
        canvas.drawLine(0, 0, first, 0, mPaint);

        int second = mRealWidth / 2 / 3 * 2;
        mPaint.setColor(mSecondBarColor);
        mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
        canvas.drawLine(first, 0, second, 0, mPaint);


        mPaint.setColor(mThridBarColor);
        mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
        canvas.drawLine(second, 0, mRealWidth / 2, 0, mPaint);

        /**
         * 绘制数字
         */

        //绘制的文本
        String text_first = "75";

        //拿到字体的宽度和高度
        float textWidth = mPaint.measureText(text_first);
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;

        // 绘制文本
        if (mIfDrawText) {
            mPaint.setColor(mTextColor);
            canvas.drawText(text_first, first, textHeight + textHeight / 2, mPaint);
        }


        //绘制的文本
        String text_second = "150";

        //拿到字体的宽度和高度
        float textWidth_second = mPaint.measureText(text_first);
        float textHeight_second = (mPaint.descent() + mPaint.ascent()) / 2;

        // 绘制文本
        if (mIfDrawText) {
            mPaint.setColor(mTextColor);
            canvas.drawText(text_second, second, textHeight + textHeight / 2, mPaint);
        }


        canvas.restore();


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        mRealWidth = w - getPaddingRight() - getPaddingLeft();
        mRealWidth = w - getPaddingRight() - getPaddingLeft();
    }

    /**
     * dp 2 px
     *
     * @param dpVal
     */
    protected int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, getResources().getDisplayMetrics());
    }

    /**
     * sp 2 px
     *
     * @param spVal
     * @return
     */
    protected int sp2px(int spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, getResources().getDisplayMetrics());

    }
}
 

图片效果:


main.xml资源配置:

<com.massky.horizontalprogressbar.HorizontalProgressBarWithNumber
    android:id="@+id/id_progressbar01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:progress_reached_bar_height="6dp"
    app:progress_unreached_bar_height="6dp"
    android:layout_marginTop="50dip"
    app:progress_unreached_color="@color/colorPrimaryDark"
    app:progress_reached_color="@android:color/black"
    app:first_reached_color="@color/colorgreen"
    app:progress_text_size="14sp"

    app:second_reached_color="@color/colororiange"
    app:third_reached_color="@color/colorred"
    android:padding="5dp" />

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值