首页 > 其他 > 详细

自定义View(一),初识自定义View

时间:2016-04-15 02:08:14      阅读:144      评论:0      收藏:0      [点我收藏+]

看了无数资料,总结一下自定义View
先明白一个自定义View的三大流程

  • onMeasure()
    测量,决定View的大小

  • onLayout()
    布局,决定View在ViewGroup中的位置

  • onDraw()
    绘制,画出这个View的内容

这三个方法都存在于View类中,我们自定义View需要针对这三个方法做出修改来达到我们需要的目标或功能
先来一个最基本的例子,我们单纯的画一个圆,我们只需修改onDraw()方法即可
MyCustomVew.java

  1. public class MyCustomView extends View { 
  2.  
  3. public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { 
  4. super(context, attrs, defStyleAttr); 
  5. // TODO Auto-generated constructor stub 
  6. } 
  7.  
  8. public MyCustomView(Context context, AttributeSet attrs) { 
  9. super(context, attrs); 
  10. // TODO Auto-generated constructor stub 
  11. } 
  12.  
  13. public MyCustomView(Context context) { 
  14. super(context); 
  15. // TODO Auto-generated constructor stub 
  16. } 
  17.  
  18. @Override 
  19. protected void onDraw(Canvas canvas) { 
  20. // TODO Auto-generated method stub 
  21. super.onDraw(canvas); 
  22. // 实例化画笔并打开抗锯齿 
  23. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
  24. // 设置画笔颜色 
  25. paint.setColor(Color.RED); 
  26. /** 
  27. * 画笔样式分三种:  
  28. * 1.Paint.Style.STROKE:描边  
  29. * 2.Paint.Style.FILL_AND_STROKE:描边并填充 
  30. * 3.Paint.Style.FILL:填充 既然是画圆,那么就选择样式为描边 
  31. */ 
  32. paint.setStyle(Paint.Style.STROKE); 
  33. /* 
  34. * 设置描边的粗细,单位:像素px 注意:当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素 
  35. */ 
  36. paint.setStrokeWidth(10); 
  37. // 参数含义依次为:圆心X坐标、圆心Y坐标、圆半径、画笔 
  38. canvas.drawCircle(500, 500, 200, paint); 
  39. } 
  40. } 

在Activity的布局文件中引入这个自定义View

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2. xmlns:app="http://schemas.android.com/apk/res-auto" 
  3. android:id="@+id/main_root_ll" 
  4. android:layout_width="match_parent" 
  5. android:layout_height="match_parent" 
  6. android:orientation="vertical" > 
  7.  
  8. <com.example.testcustomview.MyCustomView 
  9. android:id="@+id/main_cv" 
  10. android:layout_width="match_parent" 
  11. android:layout_height="match_parent" /> 
  12.  
  13. </LinearLayout> 

运行结果如下
技术分享
如果我们想要让这个圆动起来呢?我们只要不断的去修改onDraw()不断的绘制就可以了
譬如我们想要画一个由小到大的实心圆,我们需要做的就是不断的改变的半径
MyCustomView

  1. public class MyCustomView extends View implements Runnable { 
  2.  
  3. private int radiu;// 圆的半径 
  4. private Paint paint; 
  5.  
  6. public MyCustomView(Context context, AttributeSet attrs) { 
  7. super(context, attrs); 
  8. initPaint(); 
  9. } 
  10.  
  11. public MyCustomView(Context context) { 
  12. this(context, null); 
  13. } 
  14.  
  15. public void initPaint() { 
  16. paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
  17. // 设置画笔颜色 
  18. paint.setColor(Color.RED); 
  19. /** 
  20. * 画笔样式分三种: 1.Paint.Style.STROKE:描边 2.Paint.Style.FILL_AND_STROKE:描边并填充 
  21. * 3.Paint.Style.FILL:填充 既然是画圆,那么就选择样式为描边 
  22. */ 
  23. paint.setStyle(Paint.Style.FILL_AND_STROKE); 
  24. /* 
  25. * 设置描边的粗细,单位:像素px 注意:当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素 
  26. */ 
  27. paint.setStrokeWidth(10); 
  28. } 
  29.  
  30. @Override 
  31. protected void onDraw(Canvas canvas) { 
  32. // TODO Auto-generated method stub 
  33. super.onDraw(canvas); 
  34. canvas.drawCircle(500, 500, radiu, paint); 
  35. } 
  36.  
  37. @Override 
  38. public void run() { 
  39. while (radiu <= 200) { 
  40. try { 
  41. radiu += 10; 
  42. Thread.sleep(300); 
  43. //刷新View 
  44. postInvalidate(); 
  45. } catch (InterruptedException e) { 
  46. // TODO Auto-generated catch block 
  47. e.printStackTrace(); 
  48. } 
  49. } 
  50. } 
  51. } 

可以看到我们在run方法中调用了一个postInvalidate(),这个方法还有一个对应的方法Invalidate(),这两个方法的区别在于

  • postInvalidate()
    前者是在非UI线程中使,用来刷新界面

  • Invalidate()
    在UI线程自身中使用,用来刷新界面

刚才的例子是画了一个圆,canvas还提供了其他一系列方法来供我们调用,用来画各种各样的图形
下篇文章来介绍

自定义View(一),初识自定义View

原文:http://www.cnblogs.com/xs104/p/5393765.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!