下面列出了android.view.ViewGroup#measure ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static void toggleInvisibleInk(final WebView invisibleInkView, final ViewGroup bubble, TextView text, LinearLayout replay){
bubble.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
text.setVisibility(View.GONE);
replay.setVisibility(View.GONE);
invisibleInkView.setVisibility(View.VISIBLE);
String invisibleInk = "<html><head><style type='text/css' rel='stylesheet'> html { margin: 0; padding: 0em; }</style></head><body><span class='ink'>" + text.getText() + "</span> <script src='jquery.js' type='text/javascript'></script> <script src='invisibleink.js' type='text/javascript'></script></body></html>";
invisibleInkView.loadDataWithBaseURL("file:///android_asset/html/", invisibleInk, "text/html", "UTF-8", null);
invisibleInkView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
invisibleInkView.loadUrl("javascript:weMessage.resizeInvisibleInk(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
});
}
/***
* This function returns the actual height the layout. The getHeight() function returns the current height which might be zero if
* the layout's visibility is GONE
* @param layout
* @return
*/
public static int getFullHeight(ViewGroup layout) {
int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
int specHeight = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
layout.measure(specWidth,specHeight);
int totalHeight = 0;//layout.getMeasuredHeight();
int initialVisibility = layout.getVisibility();
layout.setVisibility(View.VISIBLE);
int numberOfChildren = layout.getChildCount();
for(int i = 0;i<numberOfChildren;i++) {
View child = layout.getChildAt(i);
if(child instanceof ViewGroup) {
totalHeight+=getFullHeight((ViewGroup)child);
}else {
int desiredWidth = View.MeasureSpec.makeMeasureSpec(layout.getWidth(),
View.MeasureSpec.AT_MOST);
child.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight+=child.getMeasuredHeight();
}
}
layout.setVisibility(initialVisibility);
return totalHeight;
}
/**
* viewGroup转换Bitmap
*
* @param viewGroup
* @return
*/
public static Bitmap generateBigBitmap(final ViewGroup viewGroup, int width) {
if (width > 0) {
viewGroup.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
} else if (viewGroup.getWidth() <= 0) {
viewGroup.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
} else {
width = viewGroup.getWidth();
}
width = width > 0 ? width : viewGroup.getMeasuredWidth();
LogUtils.d(width + "");
LogUtils.d(String.format("width=%s, measuredWidth=%s, height=%s, measuredHeight=%s",
viewGroup.getWidth(), viewGroup.getMeasuredWidth(), viewGroup.getHeight(), viewGroup.getMeasuredHeight()));
int height = 0;
List<BitmapWithHeight> list = getWholeViewToBitmap(viewGroup, new ArrayList<BitmapWithHeight>());
for (BitmapWithHeight item : list) {
height += item.height;
}
return generateBigBitmap(list, width, height);
}
private Bitmap generateCache(ViewGroup aContainer) {
View view = aContainer.getChildAt(0);
int width = 0;
if (view.getMeasuredWidth() <= 0 || view.getMeasuredHeight() <= 0) {
int parentSpec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
aContainer.measure(parentSpec, parentSpec);
}
width = view.getMeasuredWidth();
int spec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
int widthspec = MeasureSpec.makeMeasureSpec(width,MeasureSpec.EXACTLY);
view.measure(widthspec,spec);
view.layout(0, 0, width, view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(width, view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
view.draw(canvas);
return b;
}
@Override
protected void setUp() throws Exception {
super.setUp();
mFocusFinder = FocusFinder.getInstance();
// inflate the layout
final Context context = getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
mRoot = (ViewGroup) inflater.inflate(R.layout.focus_2, null);
// manually measure it, and lay it out
mRoot.measure(500, 500);
mRoot.layout(0, 0, 500, 500);
mLeftButton = (Button) mRoot.findViewById(R.id.leftButton);
mCenterButton = (Button) mRoot.findViewById(R.id.centerButton);
mRightButton = (Button) mRoot.findViewById(R.id.rightButton);
}
public VideoPopWindow(Context context, List<QSVideo> qsVideos, int index) {
ViewGroup popview = (ViewGroup) LayoutInflater.from(context).inflate(
R.layout.pop_definition, new FrameLayout(context), false);
float density = context.getResources().getDisplayMetrics().density;
int padding = (int) (density * 12);
for (int i = 0; i < qsVideos.size(); i++) {
QSVideo qsVideo = qsVideos.get(i);
TextView textView = new TextView(context);
textView.setId(i);
textView.setPadding(padding, padding / 2, padding, padding / 2);
textView.setText(qsVideo.resolution());
textView.setTextSize(14);
textView.setOnClickListener(this);
textView.setTextColor(index == i ? context.getResources().getColor(R.color.colorMain) : 0xffffffff);
popview.addView(textView);
}
int mode = View.MeasureSpec.AT_MOST;
//手动调用计算宽高
popview.measure(View.MeasureSpec.makeMeasureSpec(1080, mode),
View.MeasureSpec.makeMeasureSpec(1920, mode));
h = popview.getMeasuredHeight();
//设置视图
setContentView(popview);
setWidth(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);//设置宽
setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);//设置高
setFocusable(true);
setOutsideTouchable(true);
// 刷新状态
update();
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
setBackgroundDrawable(dw);
}
private Bitmap computeBitmap(ViewGroup viewGroup) {
Bitmap bitmap;
Rect rect = new Rect();
viewGroup.getWindowVisibleDisplayFrame(rect);
viewGroup.destroyDrawingCache();
viewGroup.setDrawingCacheEnabled(true);
viewGroup.buildDrawingCache(true);
bitmap = viewGroup.getDrawingCache(true);
/**
* After rotation, the DecorView has no height and no width. Therefore
* .getDrawingCache() return null. That's why we have to force measure and layout.
*/
if (bitmap == null) {
viewGroup.measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(mCoverHeight * 2, MeasureSpec.EXACTLY)
);
viewGroup.layout(0, 0, viewGroup.getMeasuredWidth(),
viewGroup.getMeasuredHeight());
viewGroup.destroyDrawingCache();
viewGroup.setDrawingCacheEnabled(true);
viewGroup.buildDrawingCache(true);
bitmap = viewGroup.getDrawingCache(true);
}
if (bitmap == null) {
return Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
} else {
return bitmap.copy(Bitmap.Config.ARGB_8888, false);
}
}
/**
* Same as previous, but with given size in case subTreeRoot itself has layout_width or layout_height = "wrap_content"
*/
private static void wrapContentAgain(ViewGroup subTreeRoot, boolean relayoutAllNodes,
int subTreeRootWidthMeasureSpec, int subTreeRootHeightMeasureSpec)
{
assert( "main".equals( Thread.currentThread().getName() ) );
if(subTreeRoot == null)
return;
LayoutParams layoutParams = subTreeRoot.getLayoutParams();
// --- First, we force measure on the subTree
int widthMeasureSpec = subTreeRootWidthMeasureSpec;
// When LayoutParams.MATCH_PARENT and Width > 0, we apply measured width to avoid getting dimensions too big
if( layoutParams.width != LayoutParams.WRAP_CONTENT && subTreeRoot.getWidth() > 0 )
widthMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getWidth(), MeasureSpec.EXACTLY );
int heightMeasureSpec = subTreeRootHeightMeasureSpec;
// When LayoutParams.MATCH_PARENT and Height > 0, we apply measured height to avoid getting dimensions too big
if( layoutParams.height != LayoutParams.WRAP_CONTENT && subTreeRoot.getHeight() > 0 )
heightMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getHeight(), MeasureSpec.EXACTLY );
// This measure recursively the whole sub-tree
subTreeRoot.measure( widthMeasureSpec, heightMeasureSpec );
// --- Then recurse on all children to correct the sizes
recurseWrapContent( subTreeRoot, relayoutAllNodes );
// --- RequestLayout to finish properly
subTreeRoot.requestLayout();
}
/**
* Same as previous, but with given size in case subTreeRoot itself has layout_width or layout_height = "wrap_content"
*/
public static void wrapContentAgain( ViewGroup subTreeRoot, boolean relayoutAllNodes,
int subTreeRootWidthMeasureSpec, int subTreeRootHeightMeasureSpec )
{
Log.d(TAG, "+++ LayoutWrapContentUpdater wrapContentAgain on subTreeRoot=["+ subTreeRoot +"], with w="
+ subTreeRootWidthMeasureSpec +" and h="+ subTreeRootHeightMeasureSpec );
assert( "main".equals( Thread.currentThread().getName() ) );
if (subTreeRoot == null)
return;
LayoutParams layoutParams = subTreeRoot.getLayoutParams();
// --- First, we force measure on the subTree
int widthMeasureSpec = subTreeRootWidthMeasureSpec;
// When LayoutParams.MATCH_PARENT and Width > 0, we apply measured width to avoid getting dimensions too big
if ( layoutParams.width != LayoutParams.WRAP_CONTENT && subTreeRoot.getWidth() > 0 )
widthMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getWidth(), MeasureSpec.EXACTLY );
int heightMeasureSpec = subTreeRootHeightMeasureSpec;
// When LayoutParams.MATCH_PARENT and Height > 0, we apply measured height to avoid getting dimensions too big
if ( layoutParams.height != LayoutParams.WRAP_CONTENT && subTreeRoot.getHeight() > 0 )
heightMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getHeight(), MeasureSpec.EXACTLY );
// This measure recursively the whole sub-tree
subTreeRoot.measure( widthMeasureSpec, heightMeasureSpec );
// --- Then recurse on all children to correct the sizes
recurseWrapContent( subTreeRoot, relayoutAllNodes );
// --- RequestLayout to finish properly
subTreeRoot.requestLayout();
return;
}