如题,
这里上传一段主要代码,求解view在什么条件下才能取到宽度和高度,要getWidth()和getHeight()
private WindowManager wm = null;
private WindowManager.LayoutParams params = new WindowManager.LayoutParams();
private FrameLayout mFrameLayout;
public void initView() {
mFrameLayout = new FrameLayout(getContext());
wm = (WindowManager) getContext().getSystemService(Activity.WINDOW_SERVICE);
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.width = WindowManager.LayoutParams.FILL_PARENT;
params.height = WindowManager.LayoutParams.FILL_PARENT;
params.alpha = 80;
params.gravity = Gravity.LEFT | Gravity.TOP;
params.format = PixelFormat.TRANSLUCENT;
}
class MyViewTouchListener implements View.OnTouchListener {
//在进入touch事件前,已经调用过initView()函数了
int eventaction = event.getAction();
public boolean onTouch(View v, MotionEvent event) {
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
wm.addView(mFrameLayout, params);
mFrameLayout.removeAllViews();
mFrameLayout.setVisibility(INVISIBLE);
//从log里可以看出在第一次touch down的时候,取到的大小总是0, 以后的touch down事件就能取到正常值
Log.i(TAG,"...mFrameLayout width........"+mFrameLayout.getWidth()+"....mFrameLayout height..."+mFrameLayout.getHeight());
break;
case MotionEvent.ACTION_MOVE:
。。。。
case MotionEvent.ACTION_UP:
。。。。
case MotionEvent.ACTION_CANCEL: