自定义View 画圆

  1. public class CustomViews extends View implements Runnable {  
  2.   
  3.     private Paint mPaint;  
  4.     private Context mContext;  
  5.     private int screenWidth;  
  6.     private int screenHeight;  
  7.     private int radial = 50;  
  8.   
  9.     public CustomViews(Context context) {  
  10.         super(context, null);  
  11.         init();  
  12.     }  
  13.   
  14.     public CustomViews(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         mContext = context;  
  17.         init();  
  18.     }  
  19.   
  20.     /** 
  21.      * 由于onDraw()方法会不停的绘制 所以需要定义一个初始化画笔方法 避免导致不停创建造成内存消耗过大 
  22.      */  
  23.     private void init() {  
  24.         mPaint = new Paint();  
  25.         // 设置画笔为抗锯齿  
  26.         mPaint.setAntiAlias(true);  
  27.         // 设置颜色为红色  
  28.         mPaint.setColor(Color.RED);  
  29.         /** 
  30.          * 画笔样式分三种: 1.Paint.Style.STROKE:描边 2.Paint.Style.FILL_AND_STROKE:描边并填充 
  31.          * 3.Paint.Style.FILL:填充 
  32.          */  
  33.         mPaint.setStyle(Paint.Style.STROKE);  
  34.         /** 
  35.          * 设置描边的粗细,单位:像素px 注意:当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素 
  36.          */  
  37.         mPaint.setStrokeWidth(20);  
  38.         /** 
  39.          * 获取屏幕的宽高 
  40.          */  
  41.         WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);  
  42.         Display display = wm.getDefaultDisplay();  
  43.         screenWidth = display.getWidth();  
  44.         screenHeight = display.getHeight();  
  45.     }  
  46.   
  47.     @Override  
  48.     protected void onDraw(Canvas canvas) {  
  49.         super.onDraw(canvas);  
  50.         /** 
  51.          * 绘制圆环drawCircle的前两个参数表示圆心的XY坐标, 这里我们用到了一个工具类获取屏幕尺寸以便将其圆心设置在屏幕中心位置, 
  52.          * 第三个参数是圆的半径,第四个参数则为我们的画笔 
  53.          *  
  54.          */  
  55.         canvas.drawCircle(screenWidth / 2, screenHeight / 2, radial, mPaint);  
  56.   
  57.     }  
  58.   
  59.     @Override  
  60.     public void run() {  
  61.         /** 
  62.          * 使用while循环不断的刷新view的半径 
  63.          * 当半径小于100每次增加10 invalidate()重绘view会报错 
  64.          * android.view.ViewRootImpl$CalledFromWrongThreadException 是非主线程更新UI 
  65.          * Android给提供postInvalidate();快捷方法来重绘view  
  66.          *  现在明白了invalidate和postInvalidate的小区别了吧 
  67.          */   
  68.         while (true) {  
  69.             if (radial <= 170) {  
  70.                 radial += 20;  
  71.                 postInvalidate();  
  72.             }else{  
  73.                 radial = 50;  
  74.             }  
  75.             try {  
  76.                 Thread.sleep(200);  
  77.             } catch (InterruptedException e) {  
  78.                 e.printStackTrace();  
  79.             }  
  80.         }  
  81.     }  

  1. }  

        XML文件
        
  1.   xmlns:tools="http://schemas.android.com/tools"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent">  
  4.   
  5.   
  6.     <com.haiwei.customview.CustomViews  
  7.         android:id="@+id/cv"  
  8.         android:layout_centerInParent="true"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"/>  
  11.   
  12.   
  13. </RelativeLayout>  
  14.  
  15. MainActivity
    1. public class MainActivity extends Activity {  
    2.   
    3.     private CustomViews mCustomView;  
    4.       
    5.     @Override  
    6.     protected void onCreate(Bundle savedInstanceState) {  
    7.         super.onCreate(savedInstanceState);  
    8.         setContentView(R.layout.activity_main);  
    9.         mCustomView = (CustomViews) findViewById(R.id.cv);  
    10.         new Thread(mCustomView).start();  
    11.     }  
    12. }  


          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值