SoFunction
Updated on 2025-04-10

Android customization of semicircle progress effect with animation

This article shares the specific code for Android semicircle progress effect for your reference. The specific content is as follows

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
  * Created by ouhimehime on 16/6/15.
  * -------------------
  */
public class CustomView extends View {


  //brush  private Paint paint;
  private RectF oval;


  //Arc color  private int roundColor;
  //Progress color  private int progressColor;
  //Text content  private boolean textIsShow;
  //Font size  private float textSize = 14;
  //Text color  private int textColor;
  //Maximum progress  private int max = 1000;
  //Current progress  private int progress = 300;
  //Arc width  private int roundWidth = 30;

  private int viewWidth; //Width--Area occupied by the control
  private float nowPro = 0;//For animation
  private ValueAnimator animator;

  public CustomView(Context context) {
    super(context);
  }

  public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initAttrs(attrs, context);
  }

  public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initAttrs(attrs, context);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initAttrs(attrs, context);
  }


  private void initAttrs(AttributeSet attr, Context context) {
    TypedArray array = (attr, );


    roundColor = (.CustomView_roundColor, );//Ring color    progressColor = (.CustomView_progressColor, );//Progress color    textIsShow = (.CustomView_textIsShow, false);//Word    textSize = (.CustomView_textSize, 14);//Text size    textColor = (.CustomView_textColor, );//Text color    roundWidth = (.CustomView_roundWidth, 30);//Ring width
    ();

    //Animation    animator = (0, progress);
    (1800);
    (new OvershootInterpolator());
    (new () {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        nowPro = (float) ();
        postInvalidate();
      }
    });
    ();

  }

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

    final int widthSpecMode = (widthMeasureSpec);
    final int widthSpecSize = (widthMeasureSpec);

    if (widthSpecMode == MeasureSpec.AT_MOST) {//Available for maximum space      setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) ((20) * (widthSpecSize / 2)));
    } else if (widthMeasureSpec == ) {//Generally refers to the exact value      setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) ((20) * (widthSpecSize / 2)));
    } else {
      setMeasuredDimension(widthMeasureSpec, (viewWidth / 2) + (int) ((20) * (viewWidth / 2)));
    }
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    (w, h, oldw, oldh);

    viewWidth = w;//Get the width to calculate the actual size of the control
    //Calculate the area occupied by the canvas    oval = new RectF();
     = roundWidth + getPaddingLeft();
     = roundWidth + getPaddingTop();
     = viewWidth - roundWidth - getPaddingRight();
     = viewWidth - roundWidth - getPaddingBottom();

  }


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

    Paint paint = new Paint();
    (true);            //Set the brush to jagged without serration    (roundColor);           //Set the brush color    (roundWidth);        //Line width    ();       //Hollow    (oval, 160, 220, false, paint);  //Draw an arc
    //Draw progress layer    (progressColor);
    (roundWidth + 1);
    (oval, 160, 220 * nowPro / max, false, paint); //Draw an arc

    if (textIsShow) {
      (textColor);
      (0);
      ();
      (textSize * 2);
      float textWidth = ((int) ((nowPro / (float) max) * 100) + "%");
      ((int) ((nowPro / (float) max) * 100) + "%", viewWidth / 2 - textWidth / 2, viewWidth / 2, paint);
    }

  }


  private int getDefaultHeight() {
    return 0;
  }

  private int getDefaultWidth() {
    return 0;
  }


  public int getRoundColor() {
    return roundColor;
  }

  public void setRoundColor(int roundColor) {
     = roundColor;
  }

  public int getProgressColor() {
    return progressColor;
  }

  public void setProgressColor(int progressColor) {
     = progressColor;
  }

  public boolean getText() {
    return textIsShow;
  }

  public void setText(boolean text) {
     = text;
  }

  public float getTextSize() {
    return textSize;
  }

  public void setTextSize(float textSize) {
     = textSize;
  }

  public int getTextColor() {
    return textColor;
  }

  public void setTextColor(int textColor) {
     = textColor;
  }

  public int getMax() {
    return max;
  }

  public void setMax(int max) {
     = max;
  }

  public int getProgress() {
    return progress;
  }

  public void setProgress(int progress) {
     = progress;
  }
}

Custom properties

<declare-styleable name="CustomView">
    <attr name="roundColor" format="color" />
    <attr name="progressColor" format="color" />
    <attr name="textIsShow" format="boolean" />
    <attr name="textSize" format="dimension" />
    <attr name="textColor" format="color" />
    <attr name="roundWidth" format="integer" />
  </declare-styleable>

usage

<
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    app:progressColor="@android:color/holo_orange_dark"
    app:roundColor="@android:color/holo_blue_dark"
    app:roundWidth="45"
    app:textColor="@android:color/black"
    app:textIsShow="true"
    app:textSize="14sp" />

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.