androidcanvas使用line画半圆

本文实例为大家分享了android canvas使用line画半圆具体代码,供大家参考,具体内容如下

创新互联公司是专业的兰西网站建设公司,兰西接单;提供做网站、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行兰西网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

LineView.java

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) {
    this.progress = progress;
    invalidate();
  }

  @Override
  protected void onDraw(Canvas canvas) {

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);

    paint.setStrokeWidth(roundWidth);
    paint.setStyle(Paint.Style.STROKE);

    canvas.drawCircle(getWidth() / 2, getHeight() / 2, 200, paint);

    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(10);
    paint.setColor(Color.BLUE);
    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 = Math.pow((200 - roundWidth / 2), 2) - (Math.pow((startY-getHeight()/2), 2));
      startX = (int)(getWidth()/2-Math.sqrt(v));
      stopX = (int)(getWidth()/2+Math.sqrt(v));
      canvas.drawLine(startX, startY, stopX, stopY, paint);
    }
  }
}

MainActivity.java

public class MainActivity extends Activity {

  private LineView lv;

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

    lv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        lv.setEnabled(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() {
                  lv.setProgress(finalI);
                }
              });
              try {
                sleep(10);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            }

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

      }
    });
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


   

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页名称:androidcanvas使用line画半圆
浏览路径:http://azwzsj.com/article/godjdg.html