Android自定义视图
本文最后更新于:2022年12月17日 中午
自定义视图
自定义视图组件 | Android 开发者 | Android Developers (google.cn)
创建视图类 | Android 开发者 | Android Developers (google.cn)
系统如何绘制View
下图是布局树
视图的每一次刷新都是完整地遍历布局树的过程,这个过程比较耗时
Android系统显示原理简介 - 简书 (jianshu.com)
要在16ms
内画完每一个界面
class MyView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
// xml加载完毕时调用
override fun onFinishInflate() {
super.onFinishInflate()
Log.d("yu","onFinishInflate:")
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
Log.d("yu", "onAttachedToWindow: ")
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
Log.d("yu", "onMeasure: ")
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
Log.d("yu", "onLayout: ")
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
Log.d("yu", "onDraw: ")
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
Log.d("yu", "onSizeChanged: ")
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
Log.d("yu", "onDetachedFromWindow: ")
}
}
启动输出
onFinishInflate:
onAttachedToWindow:
onMeasure:
onMeasure:
onSizeChanged:
onLayout:
onDraw:
- DashPathEffect:将Path的线段虚线化,intervals为虚线的ON和OFF的数组,数组中元素数目需要 >= 2; 而phase则为绘制时的偏移量。
Android自定义视图
https://yorick-ryu.github.io/Android/Android自定义视图/