自定义view 学习笔记
实现一个自定义view 一般需要重写 onMeasure()(测量view的尺寸),onDraw(画出view的样式),实现构造方法。
重写onMeasure
重写onMeasure来设置view实际所用的空间,获取xml中配置的尺寸或者不具体的参数,在onMeasure中处理。只能控制大小。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
两个参数是2bit测量模式+30bit尺寸数据可以通过获得具体数据
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Mode对应三种模式
mode | |
---|---|
unspecified | 父类没有限制,可以去任意尺寸 |
exactly | 尺寸是view的应取得尺寸,match_parent 取父类的剩余空间,固定尺寸100dp就是100dp |
at_most | 尺寸是view能取到最大尺寸,wrap_content具体尺寸根据需求确定 |
一个例子
private int getMySize(int defaultSize, int measureSpec) {
int mySize = defaultSize;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
switch (mode) {
case MeasureSpec.UNSPECIFIED: {//如果没有指定大小,就设置为默认大小
mySize = defaultSize;
break;
}
case MeasureSpec.AT_MOST: {//如果测量模式是最大取值为size
//我们将大小取最大值,你也可以取其他值
mySize = size;
break;
}
case MeasureSpec.EXACTLY: {//如果是固定的大小,那就不要去改变它
mySize = size;
break;
}
}
return mySize;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMySize(100, widthMeasureSpec);
int height = getMySize(100, heightMeasureSpec);
if (width < height) {
height = width;
} else {
width = height;
}
setMeasuredDimension(width, height);
}
布局文件
<com.hc.studyview.MyView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ff0000" />
重写onDraw
对onMeasure所规定的尺寸中绘制view的颜色形状背景图片
@Override
protected void onDraw(Canvas canvas) {
//调用父View的onDraw函数,因为View这个类帮我们实现了一些
// 基本的而绘制功能,比如绘制背景颜色、背景图片等
super.onDraw(canvas);
int r = getMeasuredWidth() / 2;//也可以是getMeasuredHeight()/2,本例中我们已经将宽高设置相等了
//圆心的横坐标为当前的View的左边起始位置+半径
int centerX = getLeft() + r;
//圆心的纵坐标为当前的View的顶部起始位置+半径
int centerY = getTop() + r;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
//开始绘制
canvas.drawCircle(centerX, centerY, r, paint);
}
实现构造类
存在三种构造方法
首先最常用的
public CustomView(Context context, AttributeSet attrs)
圆形的默认尺寸,颜色都可以通过xml设置的属性实现自定义
在res/values/styles.xml
声明自定义属性 属性类型这里查看
<resources>
<!--name为声明的"属性集合",但是最好是设置为跟我们的View一样的名称-->
<declare-styleable name="MyView">
<!--声明我们的属性,名称为default_size,取值类型为尺寸类型(dp,px等)-->
<attr name="default_size" format="dimension" />
<attr name="circle_color" format="color" />
</declare-styleable>
</resources>
在布局文件中使用自定义属性
要在根标签设置命名空间,空间名随意,自定义属性使用是保持一致就行,值是固定的http://schemas.android.com/apk/res-auto
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hc="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.hc.studyview.MyView
android:layout_width="match_parent"
android:layout_height="100dp"
hc:default_size="100dp" />
</LinearLayout>
private int defalutSize;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//第二个参数就是我们在styles.xml文件中的<declare-styleable>标签
//即属性集合的标签,在R文件中名称为R.styleable+name
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);
//第一个参数为属性集合里面的属性,R文件名称:R.styleable+属性集合名称+下划线+属性名称
//第二个参数为,如果没有设置这个属性,则设置的默认的值
defalutSize = a.getDimensionPixelSize(R.styleable.MyView_default_size, 100);
//最后记得将TypedArray对象回收
a.recycle();
}
还必须要实现
public CustomView(Context context)
在代码中创建view的时候回被调用
public CustomView(Context context, AttributeSet attrs, int defStyleAttr)
这个构造函数一般是被第一个或者第二个构造函数来调用,它的作用是当没有为自定义的属性赋值的时候,就可以使用defStyleAttr里面定义的默认属性值。
1、AttributeSet attrs
这个参数里面存放的就是上面布局文件中CustomView所定义的属性。例如:Android:layout_width、android:layout_height、app:text、app:num。
所以在布局文件中使用CustomView的时候,会调用第二个构造函数,并且将CustomView里面所赋值的属性封装在AttributeSet里面传递给第二个构造函数。
2、int defStyleAttr
我们自定义的CustomView的属性值也可以在Theme中进行指定,我们知道,我们在AndroidManifest文件中会为整个应用设置一个主题,在这个主题里面就可以为自定义的View定义默认的属性值
还有可以传样式的构造方法
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
它们三个参数直接的优先级别为:
attrs > defStyleAttr > defStyleRes