====================================================================
简单介绍一下自定义View
分类:
-
组合控件,继承自已有的
layout
,比如LinearLayout,然后通过LayoutInflater
引入布局,然后处理相关事件,这种方式的好处在于,不需要过度关注view内部的绘制机制,而且扩展性也很强。 -
继承自现有的系统控件,修改或扩展某些属性或方法即可,比如
AppCompatTextView
-
继承自
view
或viewgroup
,这种相对要复杂一些,因为我们要自己控制绘制流程,但是相对的,也有更大的想象空间。
=============================================================
先分析一下上图中的效果:
-
带颜色的矩形背景
-
居中的文本
比较简单,老手稍微想一下就已经有思路了:
-
1.自定义属性
-
2.添加构造方法
-
3.在构造里获取自定义样式
-
4.重写
onDraw
计算坐标绘制 -
5.重写
onMeasure
测量宽高 -
6.设置点击事件
先分析效果图,然后构思,随后不断的调整优化。
1.自定义属性
这一步也不一定非要写在前面,可能有些人觉得不一定就能事先知道会用到哪些属性,由于例子比较简单,暂且放在前面吧,看个人习惯。
- 在
res/values/
下建立一个attrs.xml
文件 , 在里面定义我们的属性
和声明我们的整个样式
format
是值该属性的取值类型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag
- 在
xml
布局中的引用:
<com.yechaoa.customviews.randomtext.RandomTextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:padding=“20dp”
app:randomText=“1234”
app:randomTextColor=“@color/colorAccent”
app:randomTextSize=“50sp” />
注意引入命名空间
:
xmlns:app=“http://schemas.android.com/apk/res-auto”
2.添加构造方法
新建一个RandomTextView
类,继承View
,并添加3
个构造方法
class RandomTextView : View {
//文本
private var mRandomText: String
//文本颜色
private var mRandomTextColor: Int = 0
//文本字体大小
private var mRandomTextSize: Int = 0
private var paint = Paint()
private var bounds = Rect()
//调用两个参数的构造
constructor(context: Context) : this(context, null)
//xml默认调用两个参数的构造,再调用三个参数的构造,在三个参数构造里获取自定义属性
constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(context, attributeSet, defStyle) {
…
}
…
}
这里要注意的是,所有的构造方法,都指向的是第三个构造方法,前两个构造的继承是this
,而不是super
。
第一个构造比如我们可以是new创建的,第二个是xml中默认调用的,我们在第三个构造中去获取自定义属性。
3.在构造里获取自定义样式
constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(context, attributeSet, defStyle) {
//获取自定义属性
val typedArray = context.theme.obtainStyledAttributes(
attributeSet,
R.styleable.RandomTextView,
defStyle,
0
)
mRandomText = typedArray.getString(R.styleable.RandomTextView_randomText).toString()
mRandomTextColor = typedArray.getColor(R.styleable.RandomTextView_randomTextColor, Color.BLACK)//默认黑色
mRandomTextSize = typedArray.getDimensionPixelSize(
R.styleable.RandomTextView_randomTextSize,
TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16F, resources.displayMetrics ).toInt()
)
//获取完回收
typedArray.recycle()
paint.textSize = mRandomTextSize.toFloat()
//返回文本边界,即包含文本的最小矩形,没有所谓“留白”,返回比measureText()更精确的text宽高,数据保存在bounds里
paint.getTextBounds(mRandomText, 0, mRandomText.length, bounds)
}
通过obtainStyledAttributes
获取自定义属性,返回一个TypedArray
,这里用到了我们在attrs.xml
文件中声明的样式(R.styleable.RandomTextView),返回的TypedArray即包含了这里面的属性。
拿到自定义view属性集合,然后赋值,赋值之后就可以用paint
去画了。
然后用到了paint的getTextBounds
方法:
paint.getTextBounds(mRandomText, 0, mRandomText.length, bounds)
简单理解就是,把文字放在一个矩形里,通过矩形的宽高即可知道文字的宽高,所以宽高会保存在bounds
里,bounds是一个矩形Rect
,为什么要这么做呢,因为后面我们要计算文字居中的时候会用到。
ok,接下来开始画布局。
4.重写onDraw计算坐标绘制
@SuppressLint(“DrawAllocation”)
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
/**
-
自定义View时,需要我们自己在onDraw中处理padding,否则是不生效的
-
自定义ViewGroup时,子view的padding放在onMeasure中处理
*/
/**
- 矩形背景
*/
paint.color = Color.YELLOW
//计算坐标,因为原点是在文字的左下角,左边要是延伸出去就还要往左边去,所以是减,右边和下边是正,所以是加
canvas?.drawRect(
(0 - paddingLeft).toFloat(),
(0 - paddingTop).toFloat(),
(measuredWidth + paddingRight).toFloat(),
(measuredHeight + paddingBottom).toFloat(),
paint
)
/**
- 文本
*/
paint.color = mRandomTextColor
//注意这里的坐标xy不是左上角,而是左下角,所以高度是相加的,在自定义view中,坐标轴右下为正
//getWidth 等于 measuredWidth
canvas?.drawText(
mRandomText,
(width / 2 - bounds.width() / 2).toFloat(),
(height / 2 + bounds.height() / 2).toFloat(),
paint
)
}
上面的代码就是在onDraw
里面显示绘制了一个YELLOW颜色的矩形背景,然后绘制了一个自定义属性颜色的居中的文本。
这里要注意我们计算位置时的坐标
,在自定义view中,原点是view的左上角
,而在数学坐标系中,原点(0,0)是在中间
的,二者是有区别的。
其次,假如xml布局中有padding
,或者预判会使用到padding,在重写onDraw
的时候也要把padding的数据加上,否则padding是不生效的。如果是继承ViewGroup
时,子view
的padding放在onMeasure
中处理。
来看此时的效果:
此时是不是有疑惑,xml里面的宽高明明是wrap_content
,为什么会充满父布局呢?
这就涉及到onMeasure
的知识点了,往下看。
5.重写onMeasure测量宽高
我们在xml设置view
宽高有3种方式:
-
match_parent
-
wrap_content
-
具体数据,比如100dp
onMeasure
中MeasureSpec
的 mode
也有3种模式:
-
EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
-
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
-
UNSPECIFIED:表示子布局想要多大就多大,很少使用
由于我们xml用的是wrap_content
,也就是对应AT_MOST
,所以效果就是会占满父布局中的可用空间
,而父布局是填充屏幕,所以我们自定义的view也会占满全屏。
而我们实际想要的效果是view包裹自己,而不是铺满全屏,所以我们需要在onMeasure
中进行处理
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
/**
-
EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
-
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
-
UNSPECIFIED:表示子布局想要多大就多大,很少使用
*/
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
val heightSize = MeasureSpec.getSize(heightMeasureSpec)
var width = 0
var height = 0
//如果指定了宽度,或不限制宽度,用可用宽度即可,如果是WARP_CONTENT,则用文本宽度,再加上左右padding
when (widthMode) {
MeasureSpec.UNSPECIFIED,