Android 自定义View (一)

很多的Android入门程序猿来说对于Android自定义View,可能都是比较恐惧的,但是这又是高手进阶的必经之路,所有准备在自定义View上面花一些功夫,多写一些文章。先总结下自定义View的步骤:

1、自定义View的属性

2、在View的构造方法中获得我们自定义的属性

[ 3、重写onMesure ]

4、重写onDraw

我把3用[]标出了,所以说3不一定是必须的,当然了大部分情况下还是需要重写的。

1、自定义View的属性,首先在res/values/  下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式。

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <?xml= =?> <resources> <attr= = /> <attr= = /> <attr= = /> <declare-styleable=> <attr= /> <attr= /> <attr= /> </declare-styleable> </resources>  

我们定义了字体,字体颜色,字体大小3个属性,format是值该属性的取值类型:

 

一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;不清楚的可以google一把。

然后在布局中声明我们的自定义View

 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <RelativeLayout:android=  
  2. :tools=  
  3. :custom=  
  4. :layout_width=  
  5. :layout_height= >  
  6. .customview01.CustomTitleView :layout_width=  
  7. :layout_height=  
  8. :titleText=  
  9. :titleTextColor=  
  10. :titleTextSize= />  
  11. </RelativeLayout>  


一定要引入 xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01"我们的命名空间,后面的包路径指的是项目的package

 

2、在View的构造方法中,获得我们的自定义的样式

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.      * 文本 
  3.      */ private      * 文本的颜色 
  4.      */ privateint      * 文本的大小 
  5.      */ privateint      * 绘制时控制文本绘制的范围 
  6.      */ private private public this);  
  7. public thisnull      * 获得我自定义的样式属性 
  8.      *  
  9.      * @param context 
  10.      * @param attrs 
  11.      * @param defStyle 
  12.      */ publicint super          * 获得我们所定义的自定义样式属性 
  13.          */ );  
  14. int forint; i < n; i++)  
  15. int switch case break case   
  16. break case   
  17. int , getResources().getDisplayMetrics()));  
  18. break          * 获得绘制文本的宽和高 
  19.          */ new   
  20. new , mTitleText.length(), mBound);  
  21.     }  


我们重写了3个构造方法,默认的布局文件调用的是两个参数的构造方法,所以记得让所有的构造调用我们的三个参数的构造,我们在三个参数的构造中获得自定义属性。

 

3、我们重写onDraw,onMesure调用系统提供的:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @Override protectedvoidintint super   
  2. protectedvoid , getMeasuredWidth(), getMeasuredHeight(), mPaint);  
  3.  - mBound.width() / , getHeight() /  + mBound.height() / , mPaint);  
  4.     }  

此时的效果是:

 

是不是觉得还不错,基本已经实现了自定义View。但是此时如果我们把布局文件的宽和高写成wrap_content,会发现效果并不是我们的预期:


系统帮我们测量的高度和宽度都是MATCH_PARNET,当我们设置明确的宽度和高度时,系统帮我们测量的结果就是我们设置的结果,当我们设置为WRAP_CONTENT,或者MATCH_PARENT系统帮我们测量的结果就是MATCH_PARENT的长度。

所以,当设置了WRAP_CONTENT时,我们需要自己进行测量,即重写onMesure方法”:

重写之前先了解MeasureSpec的specMode,一共三种类型:

EXACTLY:一般是设置了明确的值或者是MATCH_PARENT

AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT

UNSPECIFIED:表示子布局想要多大就多大,很少使用

下面是我们重写onMeasure代码:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @Override protectedvoidintint int int int int int int if else , mTitle.length(), mBounds);  
  2. float intint if else , mTitle.length(), mBounds);  
  3. float intint }  


现在我们修改下布局文件:

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <RelativeLayout=  
  2. =  
  3. =  
  4. =  
  5. = > <com.example.customview01.view.CustomTitleView =  
  6. =  
  7. =  
  8. =  
  9. =  
  10. =  
  11. = /> </RelativeLayout>  


现在的效果是:

 


完全复合我们的预期,现在我们可以对高度、宽度进行随便的设置了,基本可以满足我们的需求。

当然了,这样下来我们这个自定义View与TextView相比岂不是没什么优势,所有我们觉得给自定义View添加一个事件:

在构造中添加:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. thisnew   
  2. publicvoid         });  


[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. private new new while)  
  2. int);  
  3. new for  + i);  
  4. return     }  


下面再来运行:

 


我们添加了一个点击事件,每次让它随机生成一个4位的随机数,有兴趣的可以在onDraw中添加一点噪点,然后改写为验证码,是不是感觉很不错。

原文地址:https://www.cnblogs.com/wxishang1991/p/4914361.html