Custom view circular progress bar

Not much to say, go directly to the code

 First of all, of course, you need to write a few constructors for custom view.

  private Paint paint;
    private Paint paint2;
    private Paint paint3;
    private float circleText=13;
    private int height;
    private int width ;

    private boolean startAnimation = true;
    private  boolean noLayoutRefresh=true;
    public PieView(Context context) {
        this(context,null);
    }

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

    public PieView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
         if(attrs !=null){ //The following code to get the attributes written in xml          
             TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PieView, defStyleAttr, 0);
             for (int i = 0; i < typedArray.length(); i++) {
                 int index = typedArray.getIndex(i);
                 switch (index){
                     case R.styleable.PieView_textSize:
                         circleText = typedArray.getDimensionPixelSize(index,13);
                         break;
                 }
             }
         }

    }

start measuring 

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeW = MeasureSpec.getMode(widthMeasureSpec);
        int sizeW = MeasureSpec.getSize(widthMeasureSpec);

        if(noLayoutRefresh) {
            noLayoutRefresh=false;
            int modeH = MeasureSpec.getMode(heightMeasureSpec);
            int sizeH = MeasureSpec.getSize(heightMeasureSpec);
            if (modeW == MeasureSpec.EXACTLY) { //equivalent to setting an exact value such as manifest 149dp
                width = sizeW;
            } else {
                width = getMeasuredWidth() + getPaddingRight() + getPaddingLeft();

               if (modeW == MeasureSpec.AT_MOST) {
             /*if(mode == MeasureSpec.AT_MOST)*/
                   width = Math.min(width,sizeW);
                }
            }
            if (modeH == MeasureSpec.EXACTLY) {
                height = sizeH;
            } else  {

                height = getMeasuredHeight() + getPaddingTop() + getPaddingBottom();
                if (modeH == MeasureSpec.AT_MOST) {
                    height = Math.min(height,sizeH);
                }

            }

            setMeasuredDimension(width, height);
            init();
        }
    }

 
 
 
 

initialize some properties

private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.LTGRAY);
        paint.setStrokeWidth(20);
        paint.setStyle(Paint.Style.STROKE);

        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint2.setColor(Color.YELLOW);
        paint2.setStrokeCap(Paint.Cap.ROUND);//Circle
        paint2.setStrokeWidth(20);
        paint2.setStyle(Paint.Style.STROKE);

        paint3 = new Paint();
        paint3.setColor(Color.GRAY);
        paint3.setTextSize(circleText);

        height = height/2;
        width = width/2;
        if (startAnimation) {
            setValue(500,230);
           startAnimation = false;
        }
    }

 started to draw

  @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

         int r = width - DensityUtil.dip2px(MyApplication.context,20);
         if(height < width){
                 r = height -DensityUtil.dip2px(MyApplication.context,20);;
         }
        canvas.drawCircle(width, height,r,paint); //Draw a circle
        RectF rectF = new RectF();
        rectF.set(width -r, height -r, width +r, height +r);
        canvas.drawArc(rectF,90,mParent,false,paint2); //Draw a circular progress bar
        Rect rect = new Rect();
        paint3.getTextBounds(mContent,0,mContent.length(),rect);   

        canvas.drawText(mContent, width -rect.width()/2, height +rect.height()/2,paint3);  //绘制文字
        canvas.restore();

 Turn on animation to make the progress bar move

private void startAnimator(float start, float end, long animTime) {
        ValueAnimator     mAnimator = new ValueAnimator();

          mAnimator = ValueAnimator.ofFloat(start, end);
        mAnimator.setDuration (animTime);
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mParent = (float) animation.getAnimatedValue();
                mContent=((int)mParent)+"度";
             
                invalidate();
            }
        });
        mAnimator.start();
    }

//This demo contains flexible layout

There is also a custom super simple  Butterknife


Finally, give the demo address

https://download.csdn.net/download/cly19940419/10309899

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326005065&siteId=291194637