优雅地在Activity启动的时获取View的宽高

有时候我们会有这么的一个需求“在Activity启动时获取某个View的宽高”。这时候我们很容易就会直接在onCreate()或者onResume()里边去获取这个View的宽/高。
实际上在onCreate()、onStart()、onResume()里均无法正确得到某个View的宽/高信息,这是因为View的measure过程和Activity的生命周期方法不是同步执行的,如果View还没有测量完毕,那么获得的宽/高就是0。以下有四种方法来解决这个问题。
1、Activity/View#onWindowFocusChanged
onWindowFocusChanged这个方法的含义是:View已经初始化完毕了,这时候就可以获取到View的宽高。需要注意的是,onWindowFocusChanged会被调用多次,当Activity的窗口得到焦点和失去焦点时均会被调用一次。调用onReusme()和onPause()也会调用该方法。代码如下:

/**
     * 方式1:通过Activity的onWindowFocusChanged方法来获取View的宽高
     * 该方法调用的时候,View已经被加载好了.
     * 注意该方法会被调用多次(获得焦点和失去焦点)
     */
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        loge("hasFocus = $hasFocus")
        loge("View的宽度为${tv_on_window_focusChanged.width} , 高度为${tv_on_window_focusChanged.height}")
        loge("View的宽度为${tv_on_window_focusChanged.measuredWidth} , 高度为${tv_on_window_focusChanged.measuredHeight}")
    }

2、view.post(runnable)
通过post可以将一个runnable投递到消息队列的尾部,然后等待Looper调用此runnable的时候,View也已经初始化好了。代码如下:

/**
     * 方法2:使用View.post(runnable)
     * post可以将一个runnable投递到消息队列的尾部,等待Looper调用此runnable的时候,View已经初始化好了
     */
    private fun usePost(){
        tv_post.post {
            loge("usePost View的宽度为${tv_post.width} , 高度为${tv_post.height}")
        }
    }

3、ViewThreeObserver
使用ViewThreeObserver中的OnGlobalLayoutListener这个接口,当View树的状态发生改变或者View树内部的View的可见性发生改变时,onGlobalLayout方法将被回调,这可以获取到View的宽高。注意:伴随着View树的状态改变等,onGlobalLayout会被调用多次。代码如下:

 /**
     * 方法3:使用ViewTreeObserver
     * ViewThreeObserver中的onGlobalLayoutListener接口,
     * 在View树的状态改变或者View树内部的View的可见性发生改变时,
     * onGlobalLayout方法会被调用,可以在这里获取View的宽高
     */
    private fun useViewTreeObserver(){
        val observer = tv_view_tree_observer.viewTreeObserver
        observer.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                //注意需要在第一次获取的同时顺便把监听给取消掉
                tv_view_tree_observer.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val width = tv_view_tree_observer.measuredWidth
                val height = tv_view_tree_observer.measuredHeight
                loge("useViewTreeObserver View的宽度为${width} , 高度为${height}")
            }

        })
    }

4、view.measure(int widthMeasureSpec,int heightMeasureSpec)
通过手动对View进行measure来得到View的宽/高。这种方法需要分情况处理,根据View的LayoutParams来分:
4.1、match_parent
直接放弃,无法measure出具体的宽/高。原因是根据View的measure过程,构造此种MeasureSpec需要知道parentSize,即父容器的剩余空间,而这个时候我们无法知道parentSize的大小,所以理论上是不可能测量出View的大小。
4.2、具体的数值(dp/px)

 /**
     * 使用具体的数值(dp/px)来测量View的大小
     */
    private fun concreteValue(){
        val widthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY)
        val heightMeasureSpec = MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY)
        tv_view_concrete_value.measure(widthMeasureSpec,heightMeasureSpec)
        loge("concreteValue View的宽度为${tv_view_concrete_value.measuredWidth} , 高度为${tv_view_concrete_value.measuredHeight}")
    }

4.3、wrap_content

/**
     * 使用wrap_content来测量View的大小
     */
    private fun measureWithWrapContent(){
        val widthMeasureSpec = MeasureSpec.makeMeasureSpec(30.shl(1) -1, MeasureSpec.EXACTLY)
        val heightMeasureSpec = MeasureSpec.makeMeasureSpec(30.shl(1) -1,MeasureSpec.EXACTLY)
        tv_view_wrap_content.measure(widthMeasureSpec,heightMeasureSpec)
        loge("measureWithWrapContent View的宽度为${tv_view_wrap_content.measuredWidth} , 高度为${tv_view_wrap_content.measuredHeight}")
    }

注意到30.shl(1) - 1,通过分析MeasureSpec的实现可以知道,View的尺寸使用30位二进制表示,也就是说最大是30个1(即2^30-1),也就是(1<<30)-1,在最大化模式下,我们用View理论上能支持的最大值去构造MeasureSpec是合理的。

GitHub:https://gitee.com/black_heidou/summary-project

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值