一.View结构原理

Android系统对视图结构的设计采用了组合模式,即View作为所有图形的基类,ViewGoup对View进行扩展为视图容器类。

View定义了绘图的基本操作:measure(),layout(),draw()。其内部又分别包含了onMeasure(),onLayout(),onDraw()三个子方法。

1.measure操作

用于计算视图的长宽,是final类型的。measure函数又会调用onMeasure().

onMeasure(),视图大小将最终在这里确定。即measure是对onMeasure的包装,子类可以覆

盖onMeasure()来实现计算视图大小的方式,并通过setMeasuredDimension(width,height)

保存结果。

2.layout操作

设置视图在屏幕中显示的位置,final类型,有以下2个基本操作

(1)setFrame(l,t,r,b),参数即子视图在父视图中的具体位置,该函数用于保存这些参数。

(2)onLayout(),在View函数中什么都不做,它是为ViewGroup布局子视图用的。

3.draw操作

draw操作利用前2步得到的参数将视图显示在屏幕上。子类也不应该修改该方法,因为其内部

定义了绘图的基本操作:

(1)绘制背景;(2)如果要视图显示渐变框,这里会做一些准备工作;(3)绘制视图本身

即调用onDraw()。ViewGroup不需要实现该函数,因为容器是“没有内容”的,它只需要告诉子view绘制自己就行了,也就是下面的dispatchDraw()方法;(4)绘制子视图,即

dispatchDraw()函数。View不需要实现该方法,容器类必须实现。(5)如果需要(应用程序调

用了setVerticalFadingEdge之类),开始绘制渐变框;(6)绘制滚动条;

从上面看出自定义View最少应覆盖onMeasure()和onDraw()方法。



二.View类的构造方法

(1)继承已有控件(要实现的控件与已有控件类似时采用该方法)

(2)继承布局文件(要组合控件时用),注意此时不用onDraw方法,它是通过inflater视图然后addView(view)

(3)继承View类,使用GDI绘制出组件界面


三.自定义View增加属性的2种方法:

(1)在View类中定义。通过构造函数引入的AttributeSet去查找XML布局中的属性名称,然后找到它对应引用的资源ID去找值。

<com.example.myp_w_picpathview2.MyView

android:id="@+id/myView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

Text="@string/hello_world"

Src="@drawable/xh"/>

自定义Text和Src属性。

public class MyView extends View {


public MyView(Context context, AttributeSet attrs) {

super(context, attrs);

int resourceId = 0;

//获取xml中自定义的Text和Src属性

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) {


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);

}

}


(2)通过XML为View注册属性。与Android提供的标准属性写法一样。

<resources>

<declare-styleable name="MyImageView">

<attr name="Text" format="reference|string"/>

<attr name="Oriental">

<enum name="Horizontal" value="1"/>

<enum name="Vertical" value="0"/>

</attr>

<attr name="Src" format="reference|integer"/>

</declare-styleable>

</resources>


public class MyImageView extends LinearLayout {


public MyImageView(Context context, AttributeSet attrs) {

super(context, attrs);

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();

}

}