标签:
此文章是在网易博客上发表的,觉得比较不错,所以总结过来学习一下!
开发自定义控件的步骤:
1了解View的工作原理
2编写继承自View的子类
3为自定义View类增加属性
4绘制控件
5响应用户消息
6自定义回调函数
一、View的工作原理
Android系统的视图结构的设计也采用了组合模式,即view作为所有图形的基类,viewgroup对view继承扩展为视图容器类。
view定义了绘图的基本操作:
基本操作由三个函数完成:measure(),layout(),draw(),其内部又分别包含了onMeasure(),onLayout(),onDraw()三个自方法,具体操作:
1、measure操作主要用于计算视图大小,即视图的宽度和长度。在view中定义为final类型,要求子类不能修改,measure方法实现自己的计算视图大小的方式,并通过setMeasuredDimension(width,height)保存计算结果。
2、layout操作用于设置视图在屏幕中显示的位置。在view中定义也是final类型,要求子类不能修改,layout()函数有两个基本操作:
(1)setFrame(l,t,r,b)这四个小参数就是子视图在父视图的具体位置,该函数用于将这些参数保存起来,
(2)onLayout在View中这个参数什么都不会做的, 提供该函数主要是为ViewGroup类型布局子布局用。
3、draw操作利用前两个函数得到的参数将视图显示在屏幕上,到这里也就完成了整个视图的绘制工作,子类也不应该修改该方法,以为其内部定义了绘图的基本操作:
(1)绘制背景:
(2)如果要视图显示渐变框,这里会做一些准备工作
(3)绘制视图本身,调用onDraw()函数。在view中onDraw方法是一个空函数,也就是说具体的视图都要覆写该函数来实现自己的显示,例如TextView在这里实现了绘制文字的过程。而对于ViewGroup则不需要实现该函数,因为作为容器是没有内容的,其包含了多个子View,而子View已经实现了自己的绘图,因此只需要告诉子View绘制自己就可以了也就是dispatchDraw方法。
(4)绘制子视图,即dispatchDraw方法,在view中是一个空函数,具体的视图不需要实现该方法,它是专门为容器类准备的,也就是容器类必须实现该方法。
(5)如果需要,开始绘制渐变框。
(6)绘制滚动条,从上面可以看出自定义view需要至少覆写onMeasure和onDraw方法。
二、View类的构造方法
创建自定义控件的3种主要实现方式:
1)继承已有的控件来实现自定义控件: 主要是当要实现的控件和已有的控件在很多方面比较类似, 通过对已有控件的扩展来满足要求。
2)通过继承一个布局文件实现自定义控件,一般来说做组合控件时可以通过这个方式来实现。
注意此时不用onDraw方法,在构造广告中通过inflater加载自定义控件的布局文件,再addView(view),自定义控件的图形界面就加载进来了。
3)通过继承view类来实现自定义控件,使用GDI绘制出组件界面,一般无法通过上述两种方式来实现时用该方式。
View(Context context) Simple constructor to use when creating a view from code. View(Context context, AttributeSet attrs) Constructor that is called when inflating a view from XML. View(Context context, AttributeSet attrs, int defStyle) Perform inflation from XML and apply a class-specific base style.三、自定义View增加属性的两种方法:
public class MyView extends View { private String mtext; private int msrc; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub int resourceId = 0; int textId = attrs.getAttributeResourceValue(null, "Text",0); int srcId = attrs.getAttributeResourceValue(null, "Src", 0); mtext = context.getResources().getText(textId).toString(); msrc = srcId; } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub Paint paint = new Paint(); paint.setColor(Color.RED); InputStream is = getResources().openRawResource(msrc); Bitmap mBitmap = BitmapFactory.decodeStream(is); int bh = mBitmap.getHeight(); int bw = mBitmap.getWidth(); canvas.drawBitmap(mBitmap, 0,0, paint); //canvas.drawCircle(40, 90, 15, paint); canvas.drawText(mtext, bw/2, 30, paint); } } 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.myimageview2.MyView android:id="@+id/myView1" android:layout_width="wrap_content" android:layout_height="wrap_content" Text="@string/hello_world" Src="@drawable/xh"/> </LinearLayout> 属性Text, Src在自定义View类的构造方法中读取2)通过XML为View注册属性。与Android提供的标准属性写法一样。
public class MyImageView extends LinearLayout { public MyImageView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub int resourceId = -1; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyImageView); ImageView iv = new ImageView(context); TextView tv = new TextView(context); int N = typedArray.getIndexCount(); for (int i = 0; i < N; i++) { int attr = typedArray.getIndex(i); switch (attr) { case R.styleable.MyImageView_Oriental: resourceId = typedArray.getInt( R.styleable.MyImageView_Oriental, 0); this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); break; case R.styleable.MyImageView_Text: resourceId = typedArray.getResourceId( R.styleable.MyImageView_Text, 0); tv.setText(resourceId > 0 ? typedArray.getResources().getText( resourceId) : typedArray .getString(R.styleable.MyImageView_Text)); break; case R.styleable.MyImageView_Src: resourceId = typedArray.getResourceId( R.styleable.MyImageView_Src, 0); iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher); break; } } addView(iv); addView(tv); typedArray.recycle(); } } attrs.xml进行属性声明, 文件放在values目录下 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyImageView"> <attr name="Text" format="reference|string"></attr> <attr name="Oriental" > <enum name="Horizontal" value="1"></enum> <enum name="Vertical" value="0"></enum> </attr> <attr name="Src" format="reference|integer"></attr> </declare-styleable> </resources> MainActivity的布局文件:先定义命名空间 xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2" 然后可以像使用系统的属性一样使用:uview:Oriental="Vertical" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.example.myimageview2.MyImageView android:id="@+id/myImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" uview:Text="这是一个图片说明" uview:Src="@drawable/tw" uview:Oriental="Vertical"> </com.example.myimageview2.MyImageView> </LinearLayout>
五、自定义View的方法
onFinishInflate() 回调方法,当应用从XML加载该组件并用它构建界面之后调用的方法
onMeasure() 检测View组件及其子组件的大小
onLayout() 当该组件需要分配其子组件的位置、大小时
onSizeChange() 当该组件的大小被改变时
onDraw() 当组件将要绘制它的内容时
onKeyDown 当按下某个键盘时
onKeyUp 当松开某个键盘时
onTrackballEvent 当发生轨迹球事件时
onTouchEvent 当发生触屏事件时
onWindowFocusChanged(boolean) 当该组件得到、失去焦点时
onAtrrachedToWindow() 当把该组件放入到某个窗口时
onDetachedFromWindow() 当把该组件从某个窗口上分离时触发的方法
onWindowVisibilityChanged(int): 当包含该组件的窗口的可见性发生改变时触发的方法
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/kfuxydnod6/article/details/46926717