View自定义控件系列——获取View最新(实时)宽高

获取View最新宽高

addOnGlobalLayoutListener

​ 视图树的观察者,监听view的全局变化事件,包括layout,draw
​ 应用场景,onCreate(),onResume()中控件未加载完全的情况下获取view的宽高等属性
注意:需要移除监听事件

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT >= 16) {
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }else {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
        int width = view.getWidth();
    }
});

addOnLayoutChangeListener

​ 监听Layout事件,可以获取view的最新宽高,但是会有延迟
注意:需要移除监听事件

view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        view.removeOnLayoutChangeListener(this);
        int width = view.getWidth();
    }
});

addOnPreDrawListener

​ 监听draw事件
注意:需要移除监听事件

view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        view.getViewTreeObserver().removeOnPreDrawListener(this);
        int width = view.getWidth();
        return false;
    }
});

onLayout()/onSizeChanged()

​ 监听Layout事件

view = new View(this) {
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int width = view.getWidth(); 
    }
};
View view1 = new View(this){
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		int width = w; 
	}
};

view.post()

​ 利用Hanlder消息进制,当View的Layout处理完成时,自动发送消息,通知UI线程。不需要移除监听事件,相对简洁

view.post(new Runnable() {
    @Override
    public void run() {
        int width = view.getWidth();
    }
});

ViewCompat.isLaidOut(view)

​ 当view至少经历一次layout时,此判断返回true。应用场景较少,不能获取实时宽高

if (ViewCompat.isLaidOut(view)) {
    int width = view.getWidth();
}

getMeasuredWidth()

int width = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(width,height);
int height=view.getMeasuredHeight(); 
int width=view.getMeasuredWidth();

onWindowFocusChanged()

//自定义类继承Activity
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
	super.onWindowFocusChanged(hasWindowFocus);
	if(hasWindowFocus){
		int width = view.getMeasuredWidth();
		int height = view.getMeasuredHeight();
	}
}

问题

如何在View为GONE状态时获取其宽高?
​ 先将其状态设置为INVISIBLE,获取宽高后,在设置其状态为GONE。
​ INVISIBLE状态,系统会计算View的宽高不渲染,并保留位置
​ GONE状态,系统不会计算View的宽高,也不渲染

你可能感兴趣的:(Android动画与自定义控件,onLayout,view.post,getWidth)