android canvas使用line画半圆

所属分类: 软件编程 / Android 阅读数: 66
收藏 0 赞 0 分享

本文实例为大家分享了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 动画之AlphaAnimation应用详解

本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之ScaleAnimation应用详解

本节讲解ScaleAnimation 动画在应用中的实现,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之RotateAnimation应用详解

本节讲解旋转动画效果RotateAnimation方法的应用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之文件操作模式深入理解

本文将介绍Android开发之文件操作模式,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android应用程序窗口(Activity)窗口对象(Window)创建指南

本文将详细介绍Android应用程序窗口(Activity)的窗口对象(Window)的创建过程,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android activity设置无标题实现全屏

本文将详细介绍Android如何设置Activity全屏和无标题的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android启动模拟器报错解决方法

本文将详细介绍Android模拟器报"Failed To Allocate memory 8"错误的解决办法,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android如何实现非本地图片的点击态

Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android viewpaper实例探讨

本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
收藏 0 赞 0 分享
查看更多