自定义View-进度条

1.创建View和编写res/values/attrs.xml
public class ProgressBar extends View {
    private int mInnerBackground = Color.RED;
    private int mOuterBackground = Color.RED;
    private int mRoundWidth = 10;// 10px
    private float mProgressTextSize = 15;
    private int mProgressTextColor = Color.RED;
    private Paint mInnerPaint, mOuterPaint, mTextPaint;


    private int mMax = 100;
    private int mProgress = 50;
    public ProgressBar(Context context) {
        this(context,null);
    }

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

    public ProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressBar);
        mInnerBackground = array.getColor(R.styleable.ProgressBar_innerBackground, mInnerBackground);
        mOuterBackground = array.getColor(R.styleable.ProgressBar_outerBackground, mOuterBackground);
        mRoundWidth = (int) array.getDimension(R.styleable.ProgressBar_roundWidth, dip2px(10));
        mProgressTextSize = array.getDimensionPixelSize(R.styleable.ProgressBar_progressTextSize,
                sp2px(mProgressTextSize));
        mProgressTextColor = array.getColor(R.styleable.ProgressBar_progressTextColor, mProgressTextColor);

        array.recycle();
        mInnerPaint = new Paint();
        mInnerPaint.setColor(mInnerBackground);
        mInnerPaint.setStrokeWidth(mRoundWidth);
        mInnerPaint.setStyle(Paint.Style.STROKE);

        mOuterPaint = new Paint();
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setColor(mOuterBackground);
        mOuterPaint.setStrokeWidth(mRoundWidth);
        mOuterPaint.setStyle(Paint.Style.STROKE);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mProgressTextColor);
        mTextPaint.setTextSize(mProgressTextSize);
    }
    private int sp2px(float sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP
                , sp, getResources().getDisplayMetrics());
    }

    private float dip2px(int dip) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP
                , dip, getResources().getDisplayMetrics());
    }
}

 

<!--圆形进度条-->
    <declare-styleable name="ProgressBar">
        <attr name="innerBackground" format="color"/>
        <attr name="outerBackground" format="color"/>
        <attr name="roundWidth" format="dimension"/>
        <attr name="progressTextSize" format="dimension"/>
        <attr name="progressTextColor" format="color"/>
    </declare-styleable>
<!--圆形进度条-->
    <com.itjs.myapplication.progressbar.ProgressBar
        android:id="@+id/progress_bar"
        app:progressTextSize="20sp"
        app:progressTextColor="@color/colorAccent"
        app:innerBackground="@color/colorPrimary"
        app:outerBackground="@color/colorAccent"
        app:roundWidth="20dp"
        android:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 2.onMeasure()、onDraw()
@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //先画内圆
        int center = getWidth() / 2;
        canvas.drawCircle(center,center,center - mRoundWidth / 2 ,mInnerPaint);

        //画外圆 画圆弧
        RectF rect = new RectF(0 + mRoundWidth / 2, 0 + mRoundWidth / 2,
                getWidth() - mRoundWidth / 2, getHeight() - mRoundWidth / 2);
        if (mProgress == 0){return;}
        float percent = (float) mProgress/mMax;
        canvas.drawArc(rect,0,percent*360,false,mOuterPaint);

        // 画进度文字
        String text = ((int) (percent * 100)) + "%";
        Rect textBounds = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), textBounds);
        int x = getWidth() / 2 - textBounds.width() / 2;

        Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        int baseLineY = getHeight() / 2 + dy;

        canvas.drawText(text, x, baseLineY, mTextPaint);
    }
 3.调用以及测试
// 给几个方法
    public synchronized void setMax(int max) {
        if (max < 0) {

        }
        this.mMax = max;
    }

    public synchronized void setProgress(int progress) {
        if (progress < 0) {
        }
        this.mProgress = progress;
        // 刷新 invalidate
        invalidate();
    }
    private ProgressBar mProgressBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressBar =  findViewById(R.id.progress_bar);
        progressBarVoid();
    }
    private void progressBarVoid() {
        mProgressBar.setMax(4000);

        ValueAnimator animator = ObjectAnimator.ofFloat(0, 4000);
        animator.setDuration(2000);
        animator.start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float progress = (float) animation.getAnimatedValue();
                mProgressBar.setProgress((int) progress);
            }
        });
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值