作为一个有创意的开发者,或者软件对ui设计的要求比较高,你经常会遇到安卓自带的控件无法满足你的需求的情况,这种时候,我们只能去自己去实现适合项目的控件。同时,安卓也允许你去继承已经存在的控件或者实现你自己的控件以便优化界面和创造更加丰富的用户体验。
那么怎样来创建一个新的控件呢?
这得看需求是怎样的了。
1.需要在原生控件的基本功能上进行扩展,这个时候你只需要继承并对控件进行扩展。通过重写它的事件,ondraw ,但是始终都保持都父类方法的调用。如从已有的高级控件上继承,例如继承一个textview。
2.需要几个控件的功能的加和,这个时候要把控件组合起来,就是通过合并几个控件来生成一个新控件。比如在listview中用适配器来将多种控件有机的结合在一起,又如写一个控件是多个控件的组合,一般是自定义布局,可以用一个类继承一个布局。这个布局中包含多个控件。
3.白手起家自己创建一个新的控件。即直接从view,viewgroup开始绘制控件
4.另外大家不要忘了,还有一个好用的东西<include>标签。 在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include
/> 标签来重用layout代码。
作过android 应用开发的朋友都知道,android的ui界面都是由view和viewgroup及其派生类组合而成的。基于安卓ui设计原理,我们作为开发者,完全能够按照自己的意愿开发出项目定制的组件。其中,view是所有ui组件的基类,而viewgroup是容纳这些组件的容器,其本身也是从view派生出来的。androidui界面的一般结构可参见下面的示意图:

可见,作为容器的viewgroup可以包含作为叶子节点的view,也可以包含作为更低层次的子viewgroup,而子viewgroup又可以包含下一层的叶子节点的view和viewgroup。事实上,这种灵活的view层次结构可以形成非常复杂的ui布局,开发者可据此设计、开发非常精致的ui界面。
viewgroup可以通过重写onmeasure,onlayout为加入其中的view进行布局和处理,功能十分强大,我们这次先学习view类派生自定义组件:
view组件的作用类似于java中swing里的panel,是一个矩形的空白区域,不带有任何内容,对于android应用的其他ui控件来说,都是继承了view组件,然后绘制出来的。所以我们通过view子类并重写view类的方法来派生我们自己的控件。
android自定义view实现很简单:
继承view,重写构造函数、ondraw,(onmeasure)等函数,下面会逐一列举。
如果自定义的view需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。在使用到自定义view的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义view所在的包路径".在使用自定义属性的时候,使用前缀:属性名,如my:textcolor="……"。
让我们先看一下view类的方法:
category
methods
description
creation
constructors
there is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. the second form should parse and apply any attributes defined in the
layout file.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onfinishinflate()">onfinishinflate()</a>
called after a view and all of its children has been inflated from xml.
layout
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onmeasure(int,%20int)">onmeasure(int, int)</a>
called to determine the size requirements for this view and all of its children.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onlayout(boolean,%20int,%20int,%20int,%20int)">onlayout(boolean, int, int, int, int)</a>
called when this view should assign a size and position to all of its children.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onsizechanged(int,%20int,%20int,%20int)">onsizechanged(int, int, int, int)</a>
called when the size of this view has changed.
drawing
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#ondraw(android.graphics.canvas)">ondraw(android.graphics.canvas)</a>
called when the view should render its content.
event processing
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onkeydown(int,%20android.view.keyevent)">onkeydown(int, keyevent)</a>
called when a new hardware key event occurs.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onkeyup(int,%20android.view.keyevent)">onkeyup(int, keyevent)</a>
called when a hardware key up event occurs.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#ontrackballevent(android.view.motionevent)">ontrackballevent(motionevent)</a>
called when a trackball motion event occurs.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#ontouchevent(android.view.motionevent)">ontouchevent(motionevent)</a>
called when a touch screen motion event occurs.
focus
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onfocuschanged(boolean,%20int,%20android.graphics.rect)">onfocuschanged(boolean, int, android.graphics.rect)</a>
called when the view gains or loses focus.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onwindowfocuschanged(boolean)">onwindowfocuschanged(boolean)</a>
called when the window containing the view gains or loses focus.
attaching
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onattachedtowindow()">onattachedtowindow()</a>
called when the view is attached to a window.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#ondetachedfromwindow()">ondetachedfromwindow()</a>
called when the view is detached from its window.
<a target="_blank" href="http://developer.android.com/reference/android/view/view.html#onwindowvisibilitychanged(int)">onwindowvisibilitychanged(int)</a>
called when the visibility of the window containing the view has changed.
通常可能需要重写以下方法:
1.构造器,至少用来获取context
2.onfinishlnflate()这是一个回调方法, 当应用从 xml 布局文件加载该组件并利用
它来构建界面之后, 该方法就会被回调。
3.onmeasure(int,int):调用该方法来检测view组件及它所包含的所有子组件的大小.
4.onlayout(boolean,int,int,int,int):当该组件需要分配其子组件的位置、大小时,
该方法就会被回调. view类中布局发生改变时会调用的方法,这个方法是所有view、viewgroup及其派生类都具有的方法,重载该类可以在布局发生改变时作定制处理,这在实现一些特效时非常有用。
6.ondraw(canves): 当该组件将要绘制它的内容时回调该方法迸行绘制. view类中用于重绘的方法,这个方法是所有view、viewgroup及其派生类都具有的方法,也是android ui绘制最重要的方法。开发者可重载该方法,并在重载的方法内部基于参数canvas绘制自己的各种图形、图像效果。
7.onkeydown(int,keyevent): 当某个键被按下时触发该方法.
8.onkayup(int,keyevent), 当松开某个键时触发该方法.
9.ontrackballevent (motionevent): 当发生轨迹球事件时触发该方法.
10.ontouchevent (motionevent): 当发生触摸屏事件时触发该方法.
11.onwindowfocuschanged(boolean): 当该组件得到、失去焦点时触发该方法.
方法.
另外再补充两个viewgroup类经常重载的方法:
1.protected void dispatchdraw(canvas canvas):viewgroup类及其派生类具有的方法,这个方法主要用于控制子view的绘制分发,重载该方法可改变子view的绘制,进而实现一些复杂的视效。
2.protected boolean drawchild(canvas canvas, view child, long drawingtime)):viewgroup类及其派生类具有的方法,这个方法直接控制绘制某局具体的子view,重载该方法可控制具体某个具体子view。
在需要开发自定义view的时候,我们不需要列举出上面所有的方法,,而是可以根据业务需要来有选择的使用·上面的方法,下面我们看一个简单的示例程序,在这个示例程序里面我们只需要重写ondraw方法就可以了!
示例程序一:
我们要写一个跟随手指移动的小球,思路很简单,只要获取到用户点击屏幕的位置,并且在该位置处重绘小球即可:
下面我们看一下程序:
我注释写的比较清楚,我就说的简略一点:
首先我们写一个类drawview,也就是我们自定义的控件,继承自view
然后我们先写出构造器,获取到context,这里如果用只含有context的构造器会在xml里调用控件的时候出错,详情请看我的另外一篇博文:
http://blog.csdn.net/sunmc1204953974/article/details/38101057
下面我们开始写:
然后不要忘了设置这些数据的setter和getter,因为我们需要再使用这个view的时候加上监听才可以:
这样我们的一个简单地自定控件就大功告成了,下面是该类的完整代码:
之后我们就是像平时使用安卓原生控件那样使用就可以了,我们看一下activity的代码:
以及xml格式的布局文件:
源代码上面已经很详细了,我在最后一篇的最后还会发一个工程上来,欢迎大家一起学习!
我也还是学生,写的不好或者有问题的地方还请多多指教~