Java Code Examples for android.view.ViewGroup#measure()

The following examples show how to use android.view.ViewGroup#measure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageEffects.java    From weMessage with GNU Affero General Public License v3.0 6 votes vote down vote up
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);
        }
    });
}
 
Example 2
Source File: WidgetHelper.java    From Android_accordion_view with MIT License 6 votes vote down vote up
/***
 * 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;
}
 
Example 3
Source File: ViewToImageUtil.java    From ViewToImage with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: SwipeableLayout.java    From SwipeableLayout with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: Focus2AndroidTest.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@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);
}
 
Example 6
Source File: VideoPopWindow.java    From QSVideoPlayer with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: FoldableLayout.java    From FoldableLayout with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 8
Source File: LayoutWrapContentUpdater.java    From MaterialScrollBar with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 9
Source File: LayoutWrapContentUpdater.java    From android-layout-wrap-content-updater with MIT License 5 votes vote down vote up
/**
 * 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;
}