进度值跟着进度滑动progress

本文参考https://github.com/daimajia/NumberProgressBar做出的修改

效果图

public class MyProgressBar extends View {
    /**
     * 最大进度
     */
    private int mMaxProgress = 1000;

    /**
     * 当前进度
     */
    private int mCurrentProgress = 0;

    /**
     * 进度条颜色
     */
    private int mProgressStartColor;
    private int mProgressEndColor;

    /**
     * 进度值颜色
     */
    private int mTextColor;

    /**
     * 进度值文字大小
     */
    private float mTextSize;

    /**
     * 进度条高度
     */
    private float mProgressHeight;
    /**
     * 进度条圆角
     */
    private float mProgressRadius;

    /**
     * 进度值后缀
     */
    private String mSuffix = "";

    /**
     * 进度值前缀
     */
    private String mPrefix = "";


    private final int mDefaultTextColor = Color.parseColor("#ffffffff");
    private final float mDefaultProgressTextOffset = 3;
    private final float mDefaultTextSize = 10;
    private final float default_reached_bar_height;

    /**
     * For save and restore instance of progressbar.
     */
    private static final String INSTANCE_STATE = "saved_instance";
    private static final String INSTANCE_TEXT_COLOR = "text_color";
    private static final String INSTANCE_TEXT_SIZE = "text_size";
    private static final String INSTANCE_REACHED_BAR_HEIGHT = "reached_bar_height";
    private static final String INSTANCE_MAX = "max";
    private static final String INSTANCE_PROGRESS = "progress";
    private static final String INSTANCE_SUFFIX = "suffix";
    private static final String INSTANCE_PREFIX = "prefix";
    private static final String INSTANCE_TEXT_VISIBILITY = "text_visibility";

    private static final int PROGRESS_TEXT_VISIBLE = 0;


    /**
     * 要绘制文本的的宽
     */
    private float mDrawTextWidth;

    /**
     * 绘制文本开始。
     */
    private float mDrawTextStart;

    /**
     * 绘制文本结束。
     */
    private float mDrawTextEnd;


    private String mCurrentDrawText;


    private Paint mProgressBarPaint;

    private Paint mTextPaint;

    private RectF mProgressRectF = new RectF(0, 0, 0, 0);

    /**
     * 进度值与进度条间距
     */
    private float mOffset;

    private boolean mDrawProgressBar = true;

    private boolean mIfDrawText = true;

    private OnProgressBarListener mListener;
    private LinearGradient mLinearGradient;

    public enum ProgressTextVisibility {
        Visible, Invisible
    }

    public MyProgressBar(Context context) {
        this(context, null);
    }

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

    public MyProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        default_reached_bar_height = dp2px(1.5f);

        //load styled attributes.
        final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyProgressBar,
                defStyleAttr, 0);

        mProgressStartColor = attributes.getColor(R.styleable.MyProgressBar_progress_start_color, Color.parseColor("#30F6C9A0"));
        mProgressEndColor = attributes.getColor(R.styleable.MyProgressBar_progress_end_color, Color.parseColor("#F6C9A0"));
        mTextColor = attributes.getColor(R.styleable.MyProgressBar_progress_text_color, mDefaultTextColor);
        mTextSize = attributes.getDimension(R.styleable.MyProgressBar_progress_text_size, sp2px(mDefaultTextSize));

        mProgressHeight = attributes.getDimension(R.styleable.MyProgressBar_progress_bar_height, dp2px(8));
        mProgressRadius = attributes.getDimension(R.styleable.MyProgressBar_progress_bar_radius, dp2px(4));
        mOffset = attributes.getDimension(R.styleable.MyProgressBar_progress_text_offset, dp2px(mDefaultProgressTextOffset));

        int textVisible = attributes.getInt(R.styleable.MyProgressBar_progress_text_visibility, PROGRESS_TEXT_VISIBLE);
        if (textVisible != PROGRESS_TEXT_VISIBLE) {
            mIfDrawText = false;
        }

        setProgress(attributes.getInt(R.styleable.MyProgressBar_progress_current, 0));
        setMax(attributes.getInt(R.styleable.MyProgressBar_progress_max, 100));

        attributes.recycle();
        initPaint();
    }

    @Override
    protected int getSuggestedMinimumWidth() {
        return (int) mTextSize;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measure(widthMeasureSpec, true), measure(heightMeasureSpec, false));
    }

    private int measure(int measureSpec, boolean isWidth) {
        int result;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom();
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight();
            result += padding;
            if (mode == MeasureSpec.AT_MOST) {
                if (isWidth) {
                    result = Math.max(result, size);
                } else {
                    result = Math.min(result, size);
                }
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mIfDrawText) {
            calculateDrawRectF();
        } else {
            calculateDrawRectFWithText();
        }

        if (mDrawProgressBar) {
            mLinearGradient = new LinearGradient(mProgressRectF.left
                    , mProgressRectF.top, mProgressRectF.right
                    , mProgressRectF.bottom
                    , mProgressStartColor
                    , mProgressEndColor
                    , Shader.TileMode.CLAMP);
            mProgressBarPaint.setShader(mLinearGradient);
            canvas.drawRoundRect(mProgressRectF, dp2px(4), dp2px(4), mProgressBarPaint);
        }


        if (mIfDrawText) {
            canvas.drawText(mCurrentDrawText, mDrawTextStart, mDrawTextEnd, mTextPaint);
        }
    }

    private void initPaint() {
        mProgressBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);
    }


    private void calculateDrawRectFWithText() {
        mProgressRectF.left = getPaddingLeft();
        mProgressRectF.top = getHeight() / 2.0f - mProgressHeight / 2.0f;
        mProgressRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight()) / (getMax() * 1.0f) * getProgress() + getPaddingLeft();
        mProgressRectF.bottom = getHeight() / 2.0f + mProgressHeight / 2.0f;

    }

    private void calculateDrawRectF() {

        mCurrentDrawText = String.valueOf(getProgress());
        mCurrentDrawText = mPrefix + mCurrentDrawText + mSuffix;
        mDrawTextWidth = mTextPaint.measureText(mCurrentDrawText);

        if (getProgress() == 0) {
            mDrawProgressBar = false;
            mDrawTextStart = getPaddingLeft();
        } else {
            mDrawProgressBar = true;
            mProgressRectF.left = getPaddingLeft();
            mProgressRectF.top = getHeight() / 2.0f - mProgressHeight / 2.0f;
            mProgressRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight()) / (getMax() * 1.0f) * getProgress() - mOffset + getPaddingLeft();
            mProgressRectF.bottom = getHeight() / 2.0f + mProgressHeight / 2.0f;
            mDrawTextStart = (mProgressRectF.right + mOffset);
        }

        mDrawTextEnd = (int) ((getHeight() / 2.0f) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f));

        if ((mDrawTextStart + mDrawTextWidth) >= getWidth() - getPaddingRight()) {
            mDrawTextStart = getWidth() - getPaddingRight() - mDrawTextWidth;
            mProgressRectF.right = mDrawTextStart - mOffset;
        }


    }


    public int getTextColor() {
        return mTextColor;
    }

    public float getProgressTextSize() {
        return mTextSize;
    }

    public int getProgress() {
        return mCurrentProgress;
    }

    public int getMax() {
        return mMaxProgress;
    }

    public float getProgressHeight() {
        return mProgressHeight;
    }


    public void setProgressTextSize(float textSize) {
        this.mTextSize = textSize;
        mTextPaint.setTextSize(mTextSize);
        invalidate();
    }

    public void setProgressTextColor(int textColor) {
        this.mTextColor = textColor;
        mTextPaint.setColor(mTextColor);
        invalidate();
    }


    public void setProgressHeight(float height) {
        mProgressHeight = height;
    }


    public void setMax(int maxProgress) {
        if (maxProgress > 0) {
            this.mMaxProgress = maxProgress;
            invalidate();
        }
    }

    public void setSuffix(String suffix) {
        if (suffix == null) {
            mSuffix = "";
        } else {
            mSuffix = suffix;
        }
    }

    public String getSuffix() {
        return mSuffix;
    }

    public void setPrefix(String prefix) {
        if (prefix == null) {
            mPrefix = "";
        } else {
            mPrefix = prefix;
        }
    }

    public String getPrefix() {
        return mPrefix;
    }

    public void incrementProgressBy(int by) {
        if (by > 0) {
            setProgress(getProgress() + by);
        }

        if (mListener != null) {
            mListener.onProgressChange(getProgress(), getMax());
        }
    }

    public void setProgress(int progress) {
        if (progress <= getMax() && progress >= 0) {
            this.mCurrentProgress = progress;
            invalidate();
        }
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        final Bundle bundle = new Bundle();
        bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
        bundle.putInt(INSTANCE_TEXT_COLOR, getTextColor());
        bundle.putFloat(INSTANCE_TEXT_SIZE, getProgressTextSize());
        bundle.putFloat(INSTANCE_REACHED_BAR_HEIGHT, getProgressHeight());
        bundle.putInt(INSTANCE_MAX, getMax());
        bundle.putInt(INSTANCE_PROGRESS, getProgress());
        bundle.putString(INSTANCE_SUFFIX, getSuffix());
        bundle.putString(INSTANCE_PREFIX, getPrefix());
        bundle.putBoolean(INSTANCE_TEXT_VISIBILITY, getProgressTextVisibility());
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            final Bundle bundle = (Bundle) state;
            mTextColor = bundle.getInt(INSTANCE_TEXT_COLOR);
            mTextSize = bundle.getFloat(INSTANCE_TEXT_SIZE);
            mProgressHeight = bundle.getFloat(INSTANCE_REACHED_BAR_HEIGHT);
            initPaint();
            setMax(bundle.getInt(INSTANCE_MAX));
            setProgress(bundle.getInt(INSTANCE_PROGRESS));
            setPrefix(bundle.getString(INSTANCE_PREFIX));
            setSuffix(bundle.getString(INSTANCE_SUFFIX));
            setProgressTextVisibility(bundle.getBoolean(INSTANCE_TEXT_VISIBILITY) ? ProgressTextVisibility.Visible : ProgressTextVisibility.Invisible);
            super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
            return;
        }
        super.onRestoreInstanceState(state);
    }

    public float dp2px(float dp) {
        final float scale = getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public float sp2px(float sp) {
        final float scale = getResources().getDisplayMetrics().scaledDensity;
        return sp * scale;
    }

    public void setProgressTextVisibility(ProgressTextVisibility visibility) {
        mIfDrawText = visibility == ProgressTextVisibility.Visible;
        invalidate();
    }

    public boolean getProgressTextVisibility() {
        return mIfDrawText;
    }

    public void setOnProgressBarListener(OnProgressBarListener listener) {
        mListener = listener;
    }
}

滑动监听


public interface OnProgressBarListener {

    void onProgressChange(int current, int max);
}
attrs
<declare-styleable name="MyProgressBar">
        <attr name="progress_current" format="integer"/>
        <attr name="progress_max" format="integer"/>

        <attr name="progress_bar_height" format="dimension"/>
        <attr name="progress_bar_radius" format="dimension"/>
        <attr name="progress_text_size" format="dimension"/>
        <attr name="progress_text_color" format="color"/>
        <attr name="progress_start_color" format="color"/>
        <attr name="progress_end_color" format="color"/>

        <attr name="progress_text_offset" format="dimension"/>

        <attr name="progress_text_visibility" format="enum">
            <enum name="visible" value="0"/>
            <enum name="invisible" value="1"/>
        </attr>
    </declare-styleable>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值