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

The following examples show how to use android.view.View#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: ScreenUtils.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap shotCommonViewBp(View v) {
    if (null == v) {
        return null;
    }
    v.setDrawingCacheEnabled(true);
    v.buildDrawingCache();
    if (Build.VERSION.SDK_INT >= 11) {
        v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(v.getHeight(), View.MeasureSpec.EXACTLY));
        v.layout((int) v.getX(), (int) v.getY(), (int) v.getX() + v.getMeasuredWidth(),
            (int) v.getY() + v.getMeasuredHeight());
    } else {
        v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    }
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache(), 0, 0, v.getMeasuredWidth(),
        v.getMeasuredHeight());

    v.setDrawingCacheEnabled(false);
    v.destroyDrawingCache();
    return b;
}
 
Example 2
Source File: IcsListPopupWindow.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
private void measureScrapChild(View child, int position, int widthMeasureSpec) {
    ListView.LayoutParams p = (ListView.LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = new ListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0);
        child.setLayoutParams(p);
    }
    //XXX p.viewType = mAdapter.getItemViewType(position);
    //XXX p.forceAdd = true;

    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec,
            mDropDownList.getPaddingLeft() + mDropDownList.getPaddingRight(), p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}
 
Example 3
Source File: QuickReturnListView.java    From catnut with MIT License 6 votes vote down vote up
public void computeScrollRange() {
	scrollRange = 0;
	itemCount = getAdapter().getCount();
	if (itemsOffsetY == null) {
		itemsOffsetY = new int[itemCount];
	}
	for (int i = 0; i < itemCount; i++) {
		View view = getAdapter().getView(i, null, this);
		view.measure(
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
		);
		itemsOffsetY[i] = scrollRange;
		scrollRange += view.getMeasuredHeight();
	}
	isScrollRangeComputed = true;
}
 
Example 4
Source File: NewsDetailActivity.java    From Social with Apache License 2.0 6 votes vote down vote up
/**
 * 使用自定义的Listview不用调用该方法
 * */
public void setListViewHeightBasedOnChildren(ListView listView) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        // listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        // 计算子项View 的宽高
        listItem.measure(0, 0);
        // 统计所有子项的总高度
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}
 
Example 5
Source File: PullToRefreshView.java    From wallpaper with GNU General Public License v2.0 6 votes vote down vote up
private void measureView(View child) {
	ViewGroup.LayoutParams p = child.getLayoutParams();
	if (p == null) {
		p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
	}

	int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
	int lpHeight = p.height;
	int childHeightSpec;
	if (lpHeight > 0) {
		childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
				MeasureSpec.EXACTLY);
	} else {
		childHeightSpec = MeasureSpec.makeMeasureSpec(0,
				MeasureSpec.UNSPECIFIED);
	}
	child.measure(childWidthSpec, childHeightSpec);
}
 
Example 6
Source File: Workspace.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) {
    int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo, false, true);
    int visibility = layout.getVisibility();
    layout.setVisibility(VISIBLE);

    int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY);
    int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY);
    Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1],
            Bitmap.Config.ARGB_8888);
    mCanvas.setBitmap(b);

    layout.measure(width, height);
    layout.layout(0, 0, unScaledSize[0], unScaledSize[1]);
    layout.draw(mCanvas);
    mCanvas.setBitmap(null);
    layout.setVisibility(visibility);
    return b;
}
 
Example 7
Source File: CommentImageGrid.java    From CommentGallery with Apache License 2.0 6 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    mWidth = MeasureSpec.getSize(widthMeasureSpec);
    mItemWidth = (int) (mWidth - mHorizontalSpace * (mMaxColumnCount - 1)) / mMaxColumnCount;
    int itemHeight = mItemWidth;

    for (int i = 0; i < getChildCount(); ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() == View.GONE) {
            continue;
        }

        int resultMode = MeasureSpec.EXACTLY;
        int resultSize = mItemWidth;
        int childMeasureSpec = MeasureSpec.makeMeasureSpec(resultSize, resultMode);
        child.measure(childMeasureSpec, childMeasureSpec);
    }

    int height = itemHeight * mRowCount + (int) (mVerticalSpace * (mRowCount - 1));

    setMeasuredDimension(mWidth, height);
}
 
Example 8
Source File: HeadView.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
/**
 * 测量view高度的方法
 *
 * @param view  目标视图
 */
private void measureView(View view){
    if(view == null){
        return;
    }
    int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    view.measure(w, h);
}
 
Example 9
Source File: ViewUtils.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
public static Bitmap loadBitmapFromView(View view) {
  if (view.getMeasuredHeight() <= 0) {
    view.measure(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
    view.draw(canvas);
    return bitmap;
  }
  return null;
}
 
Example 10
Source File: TouchInterceptHorizontalScrollView.java    From VinylMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void measureChild(View child, int parentWidthMeasureSpec,
                            int parentHeightMeasureSpec) {
    ViewGroup.LayoutParams lp = child.getLayoutParams();

    final int horizontalPadding = getPaddingLeft() + getPaddingRight();
    final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
            Math.max(0, MeasureSpec.getSize(parentWidthMeasureSpec) - horizontalPadding),
            MeasureSpec.UNSPECIFIED);

    final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
            getPaddingTop() + getPaddingBottom(), lp.height);
    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
 
Example 11
Source File: WrappingPager.java    From SwipeSelector with Apache License 2.0 5 votes vote down vote up
/**
* Copy-paste coding made possible by http://stackoverflow.com/a/20784791
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if (h > height) height = h;
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
 
Example 12
Source File: BaseLayoutHelper.java    From vlayout with MIT License 5 votes vote down vote up
@Override
public void bindLayoutView(@NonNull final View layoutView) {
    layoutView.measure(View.MeasureSpec.makeMeasureSpec(mLayoutRegion.width(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(mLayoutRegion.height(), View.MeasureSpec.EXACTLY));
    layoutView.layout(mLayoutRegion.left, mLayoutRegion.top, mLayoutRegion.right, mLayoutRegion.bottom);
    layoutView.setBackgroundColor(mBgColor);

    if (mLayoutViewBindListener != null) {
        mLayoutViewBindListener.onBind(layoutView, this);
    }

    // reset region rectangle
    mLayoutRegion.set(0, 0, 0, 0);
}
 
Example 13
Source File: FixedHeightLinearLayoutManager.java    From AndroidPlayground with MIT License 5 votes vote down vote up
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
        int heightSpec, int[] measuredDimension) {

    View view = recycler.getViewForPosition(position);
    if (view.getVisibility() == View.GONE) {
        measuredDimension[0] = 0;
        measuredDimension[1] = 0;
        return;
    }
    // For adding Item Decor Insets to view
    super.measureChildWithMargins(view, 0, 0);
    RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
    int childWidthSpec = ViewGroup.getChildMeasureSpec(
            widthSpec,
            getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(
                    view),
            p.width);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(
            heightSpec,
            getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(
                    view),
            p.height);
    view.measure(childWidthSpec, childHeightSpec);

    // Get decorated measurements
    measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
    measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
    recycler.recycleView(view);
}
 
Example 14
Source File: DetailSharedElementEnterCallback.java    From android-instant-apps with Apache License 2.0 5 votes vote down vote up
private void forceSharedElementLayout(View view) {
    int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(),
            View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(view.getHeight(),
            View.MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}
 
Example 15
Source File: BubbleCloudView.java    From BubbleCloudView with MIT License 5 votes vote down vote up
private void addAndMeasureChild(View child) {
    LayoutParams params = child.getLayoutParams();
    if (params == null) {
        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
    addViewInLayout(child, -1, params, true);
    child.measure(MeasureSpec.EXACTLY | itemSize, MeasureSpec.EXACTLY | itemSize);
}
 
Example 16
Source File: FlowLayout.java    From cannonball-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int childLeft = getPaddingLeft();
    int childTop = getPaddingTop();
    int lineHeight = 0;
    // 100 is a dummy number, widthMeasureSpec should always be EXACTLY for FlowLayout
    final int myWidth = resolveSize(100, widthMeasureSpec);
    int wantedHeight = 0;
    for (int i = 0; i < getChildCount(); i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == View.GONE) {
            continue;
        }
        // let the child measure itself
        child.measure(
                getChildMeasureSpec(widthMeasureSpec, getPaddingLeft() + getPaddingRight(),
                        child.getLayoutParams().width),
                getChildMeasureSpec(heightMeasureSpec, getPaddingTop() + getPaddingBottom(),
                        child.getLayoutParams().height));
        final int childWidth = child.getMeasuredWidth();
        final int childHeight = child.getMeasuredHeight();
        // lineheight is the height of current line, should be the height of the heightest view
        lineHeight = Math.max(childHeight, lineHeight);
        if (childWidth + childLeft + getPaddingRight() > myWidth) {
            // wrap this line
            childLeft = getPaddingLeft();
            childTop += paddingVertical + lineHeight;
            lineHeight = childHeight;
        }
        childLeft += childWidth + paddingHorizontal;
    }
    wantedHeight += childTop + lineHeight + getPaddingBottom();
    setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));
}
 
Example 17
Source File: TableViewUtils.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Gets the exact width value before the view drawing by main thread.
 */
public static int getWidth(View view) {
    view.measure(LinearLayout.LayoutParams.WRAP_CONTENT, View.MeasureSpec.makeMeasureSpec
            (view.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
    return view.getMeasuredWidth();
}
 
Example 18
Source File: GroupCreateActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int maxWidth = width - AndroidUtilities.dp(32);
    int currentLineWidth = 0;
    int y = AndroidUtilities.dp(12);
    int allCurrentLineWidth = 0;
    int allY = AndroidUtilities.dp(12);
    int x;
    for (int a = 0; a < count; a++) {
        View child = getChildAt(a);
        if (!(child instanceof GroupCreateSpan)) {
            continue;
        }
        child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(32), MeasureSpec.EXACTLY));
        if (child != removingSpan && currentLineWidth + child.getMeasuredWidth() > maxWidth) {
            y += child.getMeasuredHeight() + AndroidUtilities.dp(12);
            currentLineWidth = 0;
        }
        if (allCurrentLineWidth + child.getMeasuredWidth() > maxWidth) {
            allY += child.getMeasuredHeight() + AndroidUtilities.dp(12);
            allCurrentLineWidth = 0;
        }
        x = AndroidUtilities.dp(16) + currentLineWidth;
        if (!animationStarted) {
            if (child == removingSpan) {
                child.setTranslationX(AndroidUtilities.dp(16) + allCurrentLineWidth);
                child.setTranslationY(allY);
            } else if (removingSpan != null) {
                if (child.getTranslationX() != x) {
                    animators.add(ObjectAnimator.ofFloat(child, "translationX", x));
                }
                if (child.getTranslationY() != y) {
                    animators.add(ObjectAnimator.ofFloat(child, "translationY", y));
                }
            } else {
                child.setTranslationX(x);
                child.setTranslationY(y);
            }
        }
        if (child != removingSpan) {
            currentLineWidth += child.getMeasuredWidth() + AndroidUtilities.dp(9);
        }
        allCurrentLineWidth += child.getMeasuredWidth() + AndroidUtilities.dp(9);
    }
    int minWidth;
    if (AndroidUtilities.isTablet()) {
        minWidth = AndroidUtilities.dp(530 - 32 - 18 - 57 * 2) / 3;
    } else {
        minWidth = (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(32 + 18 + 57 * 2)) / 3;
    }
    if (maxWidth - currentLineWidth < minWidth) {
        currentLineWidth = 0;
        y += AndroidUtilities.dp(32 + 12);
    }
    if (maxWidth - allCurrentLineWidth < minWidth) {
        allY += AndroidUtilities.dp(32 + 12);
    }
    editText.measure(MeasureSpec.makeMeasureSpec(maxWidth - currentLineWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(32), MeasureSpec.EXACTLY));
    if (!animationStarted) {
        int currentHeight = allY + AndroidUtilities.dp(32 + 12);
        int fieldX = currentLineWidth + AndroidUtilities.dp(16);
        fieldY = y;
        if (currentAnimation != null) {
            int resultHeight = y + AndroidUtilities.dp(32 + 12);
            if (containerHeight != resultHeight) {
                animators.add(ObjectAnimator.ofInt(GroupCreateActivity.this, "containerHeight", resultHeight));
            }
            if (editText.getTranslationX() != fieldX) {
                animators.add(ObjectAnimator.ofFloat(editText, "translationX", fieldX));
            }
            if (editText.getTranslationY() != fieldY) {
                animators.add(ObjectAnimator.ofFloat(editText, "translationY", fieldY));
            }
            editText.setAllowDrawCursor(false);
            currentAnimation.playTogether(animators);
            currentAnimation.start();
            animationStarted = true;
        } else {
            containerHeight = currentHeight;
            editText.setTranslationX(fieldX);
            editText.setTranslationY(fieldY);
        }
    } else if (currentAnimation != null) {
        if (!ignoreScrollEvent && removingSpan == null) {
            editText.bringPointIntoView(editText.getSelectionStart());
        }
    }
    setMeasuredDimension(width, containerHeight);
}
 
Example 19
Source File: HourlyTrendWidgetIMP.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressLint("WrongThread")
@WorkerThread
private static RemoteViews getRemoteViews(Context context, @Nullable View drawableView,
                                          Location location, int width,
                                          boolean darkCard, int cardAlpha) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_remote);
    if (drawableView == null) {
        return views;
    }

    WidgetItemView[] items = new WidgetItemView[] {
            drawableView.findViewById(R.id.widget_trend_hourly_item_1),
            drawableView.findViewById(R.id.widget_trend_hourly_item_2),
            drawableView.findViewById(R.id.widget_trend_hourly_item_3),
            drawableView.findViewById(R.id.widget_trend_hourly_item_4),
            drawableView.findViewById(R.id.widget_trend_hourly_item_5),
    };
    for (WidgetItemView i : items) {
        i.setSize(width / 5f);
    }
    drawableView.measure(
            View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
    );
    drawableView.layout(
            0,
            0,
            drawableView.getMeasuredWidth(),
            drawableView.getMeasuredHeight()
    );

    Bitmap cache = Bitmap.createBitmap(
            drawableView.getMeasuredWidth(),
            drawableView.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888
    );
    Canvas canvas = new Canvas(cache);
    drawableView.draw(canvas);

    views.setImageViewBitmap(R.id.widget_remote_drawable, cache);
    views.setViewVisibility(R.id.widget_remote_progress, View.GONE);

    views.setImageViewResource(
            R.id.widget_remote_card,
            getCardBackgroundId(context, darkCard, cardAlpha)
    );

    setOnClickPendingIntent(
            context,
            views,
            location,
            SettingsOptionManager.getInstance(context).isWidgetClickToRefreshEnabled()
    );

    return views;
}
 
Example 20
Source File: FunLoginActivity.java    From RePlugin-GameSdk with Apache License 2.0 3 votes vote down vote up
public void setListViewHeightBasedOnChildren(ListView mListView) {

		ListAdapter listAdapter = mListView.getAdapter();

		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;

		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, mListView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = mListView.getLayoutParams();

		params.height = totalHeight
				+ (mListView.getDividerHeight() * (listAdapter.getCount() - 1));

		int margin = HWUtils.dip2px(this, 10);

		((MarginLayoutParams) params)
				.setMargins(margin, margin, margin, margin); // 可删除

		mListView.setLayoutParams(params);
	}