欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android canvas使用line画半圆

时间:2020-12-07 16:35:32|栏目:Android代码|点击:

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

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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >

   <com.xhly.sdv.lineview.view.LineView
    android:id="@+id/lv"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    />

</RelativeLayout>

上一篇:Android实现多级列表中的新建功能

栏    目:Android代码

下一篇:Android Beam 文件传输失败分析与解决方法

本文标题:android canvas使用line画半圆

本文地址:http://www.codeinn.net/misctech/30161.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有