其他分享
首页 > 其他分享> > 自定义View1

自定义View1

作者:互联网

class MyGeometryDraw @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    override fun onFinishInflate() {
        super.onFinishInflate()
        Log.e("=========","onFinishInflate")
    }
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        Log.e("=========","onAttachedToWindow")
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        Log.e("=========","onSizeChanged")
    }

    override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect)
        Log.e("=========","onFocusChanged")
    }
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.e("=========","onMeasure")
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        Log.e("=========","onLayout")
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        Log.e("=========","onDraw")
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Log.e("=========","onDetachedFromWindow")
    }

}

启动执行方法顺序:

2021-03-08 10:54:23.929 13501-13501/com.lixing.myfirstkotlin E/=========: onFinishInflate
2021-03-08 10:54:24.002 13501-13501/com.lixing.myfirstkotlin E/=========: onAttachedToWindow
2021-03-08 10:54:24.007 13501-13501/com.lixing.myfirstkotlin E/=========: onMeasure
2021-03-08 10:54:24.007 13501-13501/com.lixing.myfirstkotlin E/=========: onMeasure
2021-03-08 10:54:24.260 13501-13501/com.lixing.myfirstkotlin E/=========: onMeasure
2021-03-08 10:54:24.260 13501-13501/com.lixing.myfirstkotlin E/=========: onMeasure
2021-03-08 10:54:24.261 13501-13501/com.lixing.myfirstkotlin E/=========: onSizeChanged
2021-03-08 10:54:24.261 13501-13501/com.lixing.myfirstkotlin E/=========: onLayout
2021-03-08 10:54:24.309 13501-13501/com.lixing.myfirstkotlin E/=========: onDraw

其中onMeasure会执行多次,不一定是四次

关闭后执行:

2021-03-08 11:06:29.237 13501-13501/com.lixing.myfirstkotlin E/=========: onDetachedFromWindow

 

标签:03,myfirstkotlin,View1,自定义,Int,08,lixing,13501
来源: https://blog.csdn.net/qq_34707150/article/details/114525887