SoFunction
Updated on 2025-04-09

Android canvas uses line to draw semicircles

The examples in this article share with you the specific code of Android canvas using line to draw semicircles for your reference. The specific content is as follows

public class LineView extends View {
  private int progress = 0;
  private int max = 100;
  private int roundWidth = 50;
  public LineView(Context context) {
    super(context);
  }

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

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

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

  @Override
  protected void onDraw(Canvas canvas) {

    Paint paint = new Paint();
    (true);
    ();

    (roundWidth);
    ();

    (getWidth() / 2, getHeight() / 2, 200, paint);

    ();
    (10);
    ();
    int startX = 0, startY = 0, stopX = 0, stopY = 0;

    startX=0;
    stopX=500;
    for (int i = 0; i <= progress; i++) {
      startY = stopY = getHeight()/2+200-roundWidth/2 - i*(400-roundWidth)/max;
      double v = ((200 - roundWidth / 2), 2) - (((startY-getHeight()/2), 2));
      startX = (int)(getWidth()/(v));
      stopX = (int)(getWidth()/2+(v));
      (startX, startY, stopX, stopY, paint);
    }
  }
}

public class MainActivity extends Activity {

  private LineView lv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    lv = (LineView)findViewById();

    (new () {
      @Override
      public void onClick(View v) {
        (false);


        new Thread() {
          @Override
          public void run() {
            for (int i = 0; i <= 100; i++) {

              final int finalI = i;
              runOnUiThread(new Runnable() {
                @Override
                public void run() {
                  (finalI);
                }
              });
              try {
                sleep(10);
              } catch (InterruptedException e) {
                ();
              }
            }

            runOnUiThread(new Runnable() {
              @Override
              public void run() {
                (true);
              }
            });
          }
        }.start();

      }
    });
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >

   <
    android:
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    />

</RelativeLayout>

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.