我的自定义View基础(一)

来源:互联网 发布:修改软件的软件 编辑:程序博客网 时间:2024/06/06 07:09

Android菜鸟的学习阶段,自定义View的学习必不可少。结合网络上众大神的自定义View文章的教学,我将自己学习到的内容做个记录,方便以后温故。


1.构造函数:


一般常用的是第一个(调用于new一个MyView的对象)和第二个构造方法(xml布局中调用)。


2.MyView自定义属性:

TypedArray和attrs.xml和AttributeSet这一系列都是自定义控件属性时要用到的内容。

首先说说res/values/attrs.xml:它是定义成类似于这种形式的。

<?xml version="1.0" encoding="utf-8"?> 

<resources> 

   <declare-styleable name="MyView"> 

    <attr name="titleTextColor" format="color"/>

    <attr name="titleTextSize" format="dimension" />   

   </declare-styleable> 

</resources> 


使用情况:


注:红框处表示引用。我在属性中使用了"snail"表示。

或者可以将最后的"res-auto"替换成"res/包名"。

比如我的是:

xmlns:snail="http://schemas.android.com/apk/res/com.lonbon.rxandroiddemo" 


format属性:

1.reference:参考某一资源ID

2.color:颜色值。

3.boolean:布尔值。

4.dimension:尺寸值。

5.float:浮点值。

6.integer:整型值。

7.string:字符串。

8.fraction:百分数。(动画中使用)

9.enum:枚举值。

  1. <declare-styleable name="myView"> 
  2.                    <attr name="orientation"> 
  3.                           <enum name="horizontal" value="0" /> 
  4.                           <enum name="vertical" value="1" /> 
  5.                    </attr>            
  6.  </declare-styleable> 

10.flag:位或运算。

  1. <declare-styleable name="myView"> 
  2. <attr name="windowSoftInputMode"> 
  3.      <flag name = "stateUnspecified" value = "0" /> 
  4.      <flag name = "stateUnchanged" value = "1" /> 
  5.      <flag name = "stateHidden" value = "2" /> 
  6.      <flag name = "stateAlwaysHidden" value = "3" /> 
  7.      <flag name = "stateVisible" value = "4" /> 
  8.      <flag name = "stateAlwaysVisible" value = "5" /> 
  9.      <flag name = "adjustUnspecified" value = "0x00" /> 
  10.      <flag name = "adjustResize" value = "0x10" /> 
  11.      <flag name = "adjustPan" value = "0x20" /> 
  12.      <flag name = "adjustNothing" value = "0x30" /> 
  13. </attr>         
  14. </declare-styleable> 

注意:

属性定义时可以指定多种类型值。

(1)属性定义:

  1. <declare-styleable name = "名称"> 
  2.       <attr name = "background" format = "reference|color" /> 
  3. </declare-styleable> 


(2)属性使用:

  1. <ImageView 
  2.     android:layout_width = "42dip" 
  3.     android:layout_height = "42dip" 
  4.     android:background = "@drawable/图片ID|#00FF00" 
  5. /> 
TypedArray的使用:
获取属性值之后记得调用recycle()。

3.onMeasure

onMeasure 过程决定了View的宽/高,Measure 完成以后,可以通过 getMeasuredWidth 和 getMeasuredHeight 方法来获取到 View 测量后的宽和高。getWidth 和 getHeight 方法与 getMeasuredWidth 和 getMeasuredHeight 方法的区别:getWidth 和 getHeight 屏幕上展示的部分宽高,getMeasuredWidth 和 getMeasuredHeight 方法是view测量后的宽和高,包括不在屏幕中显示部分的宽和高。


理解MeasureSpec

MeasureSpec代表一个32位的int值,高2位代表SpecMode,低30位代表SpecSize。前者是指测量模式,后者是指某种测量模式下的规格大小。

SpecMode有三类:

1.UNSPECIFIED:父容器不对 View 有任何限制,要多大给多大,这种情况一般用于系统内部,表示一种测量的状态。

2.EXACTLY:父容器已经检测出 View 所需要的精确大小,这个时候 View 的最终大小就是 SpecSize 所指定的值。它对应于 LayoutParams 中的 match_parent 和具体的数值这两种模式。

3.AT_MOST:父容器指定了一个可用大小即 SpecSize ,View 的大小不能大于这个值,具体是什么值要看不同 View 的具体实现。它对应于 LayoutParams 中的 wrap_content。


测量完之后调用 setMeasuredDimension(width,height)设置宽高,width 与 height 分别表示测量后的宽,测量后的高。