如何正确的获得一个view的宽和高

转载自http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0802/1641.html

我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例:

首先我们自己写一个控件,这个控件非常简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MyImageView extends ImageView {
     public MyImageView(Context context, AttributeSet attrs) {
         super (context, attrs);
     }
     public MyImageView(Context context) {
         super (context);
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super .onMeasure(widthMeasureSpec, heightMeasureSpec);
         System.out.println( "onMeasure 我被调用了" +System.currentTimeMillis());
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
     @Override
     protected void onDraw(Canvas canvas) {
         super .onDraw(canvas);
         System.out.println( "onDraw 我被调用了" +System.currentTimeMillis());
     }
}

布局文件:

1
2
3
4
5
<com.test.MyImageView
     android:id= "@+id/imageview"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:src= "@drawable/test" />

测试的Activity的onCreate():

1
2
3
4
5
6
@Override
public void onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
     setContentView(R.layout.main);      
     System.out.println( "执行完毕.." +System.currentTimeMillis());
}

现在我们现在来看一下结果:


说明等onCreate方法执行完了,我们定义的控件才会被度量(measure),所以我们在onCreate方法里面通过view.getHeight()获取控件的高度或者宽度肯定是0,因为它自己还没有被度量,也就是说他自己都不知道自己有多高,而你这时候去获取它的尺寸,肯定是不行的.

有如下两种方法可以解决这个问题:

----------------------------------

方法一:使用view的measure方法。

------------------------------

优点:可以立即获得宽和高

缺点:人为的多了一次测量过程

这种方法适用于需要在onCreate完成之前就获得一个view的宽和高的情况。

比如获得一个LinearLayout宽和高

1
2
3
4
5
6
7
8
9
10
//宽
public int getViewWidth(LinearLayout view){
     view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     return view.getMeasuredWidth();
}
//高
public int getViewHeight(LinearLayout view){
     view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     return view.getMeasuredHeight();
}

这种方法的原理是直接调用一个view或者viewgroup的measure方法去测量,测量之后该view的getMeasuredHeight()就会返回刚才测量所得的高,getMeasuredWidth返回测量所得宽。本来在布局加载的过程中,view的measure方法一定会被系统调用,但这发生在我们所不知道的某个时间点,为了在这之前提前得到测量结果,我们主动调用measure方法,但是这样做的好处是可以立即获得宽和高,坏处是多了一次测量过程。

至于为什么参数是LayoutParams.WRAP_CONTENT,那是因为我假设这个view的layout_width和layout_height为wrap_content,因为如果为一个确切的值,还有必要测量吗?

-------------------------------------------------------------------

方法二:布局监听类ViewTreeObserver的OnGlobalLayoutListener

-------------------------------------------------------------

当一个view的布局加载完成或者布局发生改变时OnGlobalLayoutListener可以监听到,利用这点我们可以在布局加载完成的瞬间获得一个view的宽高。

1
2
3
4
5
6
7
8
9
10
11
int mHeaderViewHeight;
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
         new OnGlobalLayoutListener() {
             @Override
             public void onGlobalLayout() {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                 mHeaderViewHeight = mHeaderView.getHeight();
                 getViewTreeObserver()
                         .removeGlobalOnLayoutListener( this );
             }
});

这种方法无法像第一种方法那样通过一个函数返回值,因为他是基于listener的,OnGlobalLayoutListener的onGlobalLayout被回调之前是没有值的。由于布局状态可能会发生多次改变,因此OnGlobalLayoutListener的onGlobalLayout可能被回调多次,所以我们在 第一次获得值之后就将listener注销掉。

优点:不需要额外的测量过程

缺点:只有在布局加载完成后,才能得到宽和高


其实在activity的onResume中可以直接调用view.getWidth获得宽,那是不是第二种方法就失去意义了呢?

当然不是,如果我们自定义一个view,需要在view的内部获得某个子view的宽和高,而view本身又没有onResume这样的生命周期方法,这时OnGlobalLayoutListener的onGlobalLayout就起作用了, 可以认为onGlobalLayout就是相当于一个view的生命周期。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值