zoukankan      html  css  js  c++  java
  • 自定义View(一),初识自定义View

    看了无数资料,总结一下自定义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. public MyCustomView(Context context, AttributeSet attrs)
    8. super(context, attrs); 
    9. // TODO Auto-generated constructor stub 

    10.  
    11. public MyCustomView(Context context)
    12. super(context); 
    13. // TODO Auto-generated constructor stub 

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


    在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> 

    运行结果如下
    enter description here
    如果我们想要让这个圆动起来呢?我们只要不断的去修改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. public MyCustomView(Context context)
    11. this(context, null); 

    12.  
    13. public void initPaint()
    14. paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    15. // 设置画笔颜色 
    16. paint.setColor(Color.RED); 
    17. /** 
    18. * 画笔样式分三种: 1.Paint.Style.STROKE:描边 2.Paint.Style.FILL_AND_STROKE:描边并填充 
    19. * 3.Paint.Style.FILL:填充 既然是画圆,那么就选择样式为描边 
    20. */ 
    21. paint.setStyle(Paint.Style.FILL_AND_STROKE); 
    22. /* 
    23. * 设置描边的粗细,单位:像素px 注意:当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素 
    24. */ 
    25. paint.setStrokeWidth(10); 

    26.  
    27. @Override 
    28. protected void onDraw(Canvas canvas)
    29. // TODO Auto-generated method stub 
    30. super.onDraw(canvas); 
    31. canvas.drawCircle(500, 500, radiu, paint); 

    32.  
    33. @Override 
    34. public void run()
    35. while (radiu <= 200) { 
    36. try
    37. radiu += 10
    38. Thread.sleep(300); 
    39. //刷新View 
    40. postInvalidate(); 
    41. } catch (InterruptedException e) { 
    42. // TODO Auto-generated catch block 
    43. e.printStackTrace(); 




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

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

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

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

  • 相关阅读:
    MySQL for OPS 02:SQL 基础
    Samba:基于公网 IP 的服务访问
    MySQL for OPS 01:简介 / 安装初始化 / 用户授权管理
    Samba:打造企业级授权文件共享服务器
    嵌入式web server——Goahead移植要点
    libConfuse的使用
    【工具篇】notepad++
    使用sprintf打印float并控制小数位数时引起的问题
    【工具篇】source Insight
    【工具篇】xshell
  • 原文地址:https://www.cnblogs.com/xs104/p/5393765.html
Copyright © 2011-2022 走看看