android.view.View#measure ( )源码实例Demo

下面列出了android.view.View#measure ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Tok-Android   文件: ScreenUtils.java
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;
}
 
源代码2 项目: catnut   文件: QuickReturnListView.java
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;
}
 
源代码3 项目: Social   文件: NewsDetailActivity.java
/**
 * 使用自定义的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);
}
 
源代码4 项目: CommentGallery   文件: CommentImageGrid.java
@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);
}
 
源代码5 项目: LaunchEnr   文件: Workspace.java
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;
}
 
源代码6 项目: wallpaper   文件: PullToRefreshView.java
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);
}
 
源代码7 项目: CSipSimple   文件: IcsListPopupWindow.java
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);
}
 
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;
}
 
源代码9 项目: cannonball-android   文件: FlowLayout.java
@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));
}
 
源代码10 项目: BubbleCloudView   文件: BubbleCloudView.java
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);
}
 
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());
}
 
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);
}
 
源代码13 项目: vlayout   文件: BaseLayoutHelper.java
@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);
}
 
源代码14 项目: SwipeSelector   文件: WrappingPager.java
/**
* 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);
}
 
@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);
}
 
源代码16 项目: android-open-project-demo   文件: HeadView.java
/**
 * 测量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);
}
 
源代码17 项目: GeometricWeather   文件: HourlyTrendWidgetIMP.java
@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;
}
 
源代码18 项目: dhis2-android-capture-app   文件: TableViewUtils.java
/**
 * 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();
}
 
源代码19 项目: TelePlus-Android   文件: GroupCreateActivity.java
@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);
}
 
源代码20 项目: RePlugin-GameSdk   文件: FunLoginActivity.java
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);
	}
 
 方法所在类
 同类方法