直接用view.getheightMesure获取不到(因为还没绘制出来)
用这个
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); imageView.measure(w, h); int height = imageView.getMeasuredHeight(); int width = imageView.getMeasuredWidth();或者
ViewTreeObserver vto = imageView.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { vto.removeOnPreDrawListener(this); int height = imageView.getMeasuredHeight(); int width = imageView.getMeasuredWidth(); return true; } });
时间: 2024-10-08 11:22:24