下面列出了android.view.ViewGroup#getMeasuredWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void layoutDecorated(@NonNull View child, int left, int top, int right, int bottom) {
if (!mCenter) {
super.layoutDecorated(child, left, top, right, bottom);
return;
}
final int leftDecorationWidth = getLeftDecorationWidth(child);
final int topDecorationHeight = getTopDecorationHeight(child);
final int rightDecorationWidth = getRightDecorationWidth(child);
final int bottomDecorationHeight = getBottomDecorationHeight(child);
final ViewGroup parent = (ViewGroup) child.getParent();
final int offset;
if (getOrientation() == HORIZONTAL) {
final int contentHeight = parent.getMeasuredHeight() -
parent.getPaddingTop() - parent.getPaddingBottom();
offset = (contentHeight - (bottom - top)) / 2;
child.layout(left + leftDecorationWidth, top + topDecorationHeight + offset,
right - rightDecorationWidth, bottom - bottomDecorationHeight + offset);
} else {
final int contentWidth = parent.getMeasuredWidth() -
parent.getPaddingLeft() - parent.getPaddingRight();
offset = (contentWidth - (right - left)) / 2;
child.layout(left + leftDecorationWidth + offset, top + topDecorationHeight,
right - rightDecorationWidth + offset, bottom - bottomDecorationHeight);
}
}
/**
* 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);
}
/**
* viewGroup 转换为 bitmap集合
*
* @param viewGroup
* @return
*/
public static List<BitmapWithHeight> getWholeViewToBitmap(final ViewGroup viewGroup, List<BitmapWithHeight> list) {
int width = viewGroup.getMeasuredWidth();
if (viewGroup instanceof ListView) {
list.addAll(getWholeListViewItemsToBitmap((ListView) viewGroup));
} else if (viewGroup instanceof RecyclerView) {
// RecyclerView recyclerView = (RecyclerView) child;
// list.addAll(getWholeRecyclerViewItemsToBitmap(recyclerView));
list.add(getWholeViewToBitmap(viewGroup, width));
} else if (viewGroup instanceof AbsListView) {
// TODO scrollView, GridView等
list.add(getWholeViewToBitmap(viewGroup, width));
} else {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View child = viewGroup.getChildAt(i);
if (isScrollableView(child)) {
getWholeViewToBitmap((ViewGroup) child, list);
} else {
list.add(getWholeViewToBitmap(child, width));
}
}
}
return list;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (moveable) {
ViewGroup parentView = ((ViewGroup) getParent());
MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
viewWidth = getRight() - getLeft();
viewHight = getBottom() - getTop();
parentWidth = parentView.getMeasuredWidth();
parentHeight = parentView.getMeasuredHeight();
minLeftMargin = lp.leftMargin;
leftPadding = parentView.getPaddingLeft();
rightDistance = lp.rightMargin + parentView.getPaddingRight();
maxLeftMargin = parentWidth - rightDistance - viewWidth - leftPadding;
minTopMargin = lp.topMargin;
topPadding = parentView.getPaddingTop();
bottomDistance = lp.bottomMargin + parentView.getPaddingBottom();
maxTopMargin = parentHeight - bottomDistance - viewHight - topPadding;
}
}
public void setCanvasSize(final Bitmap b, final ViewGroup imageGroup) {
if (imageGroup.getMeasuredWidth() == 0) {
imageGroup.post(new Runnable() {
@Override
public void run() {
setSize(b, imageGroup);
}
});
} else {
setSize(b, imageGroup);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
if(parent.getMeasuredHeight()>parent.getMeasuredWidth()) {
viewHeight =(int)((parent.getMeasuredWidth()/spanCount)*4*1.0/3);
}
else viewHeight =(int)((parent.getMeasuredWidth()/spanCount)*4*1.0/3);
return new ViewHolder(view);
}
public void setCanvasSize(final Bitmap b, final ViewGroup imageGroup) {
if (imageGroup.getMeasuredWidth() == 0) {
imageGroup.post(new Runnable() {
@Override
public void run() {
setSize(b, imageGroup);
}
});
} else {
setSize(b, imageGroup);
}
}
private void setSize(Bitmap b, ViewGroup v) {
int bW = b.getWidth();
int bH = b.getHeight();
int vW = v.getMeasuredWidth();
int vH = v.getMeasuredHeight();
float scalW = (float) vW / (float) bW;
float scalH = (float) vH / (float) bH;
ViewGroup.LayoutParams params = v.getLayoutParams();
//如果图片小于viewGroup的宽高则把viewgroup设置为图片宽高
// if (bW < vW && bH < vH) {
// params.width = bW;
// params.height = bH;
// v.setLayoutParams(params);
// return;
// }
if (bW >= bH) {
params.width = vW;
params.height = (int) (scalW * bH);
} else {
params.width = (int) (scalH * bW);
params.height = vH;
}
if (params.width > vW) {
float tempScaleW = (float) vW / (float) params.width;
params.width = vW;
params.height = (int) (params.height * tempScaleW);
}
if (params.height > vH) {
float tempScaleH = (float) vH / (float) params.height;
params.height = vH;
params.width = (int) (params.width * tempScaleH);
}
v.setLayoutParams(params);
}
public static int stringToDimensionPixelSize(String dimension, DisplayMetrics metrics, ViewGroup parent, boolean horizontal) {
if (dimension.endsWith("%")) {
float pct = Float.parseFloat(dimension.substring(0, dimension.length() - 1)) / 100.0f;
return (int) (pct * (horizontal ? parent.getMeasuredWidth() : parent.getMeasuredHeight()));
}
return stringToDimensionPixelSize(dimension, metrics);
}
private void initSize(ViewGroup parent)
{
int measuredWidth = parent.getMeasuredWidth();
itemView.setMinimumHeight(measuredWidth / 3);
itemView.setPadding(measuredWidth / 9, measuredWidth / 9, measuredWidth / 9, measuredWidth / 9);
int rgb = Color.rgb(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255));
itemView.setBackgroundColor(rgb);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.parent_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
parentWidth = parent.getMeasuredWidth();
itemWidth = (parentWidth -
(holder.textView.getPaddingLeft() + holder.textView.getPaddingRight()) * (GROUPS_COLUMN_COUNT))
/ GROUPS_COLUMN_COUNT + 1;
return holder;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.child_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
parentWidth = parent.getMeasuredWidth();
itemWidth = (parentWidth -
(holder.textView.getPaddingLeft() + holder.textView.getPaddingRight()) * (EPISODES_COLUMN_COUNT))
/ EPISODES_COLUMN_COUNT + 1;
return holder;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!allowWidthFull)
return;
ViewGroup tabsLayout = getTabsLayout();
if (tabsLayout == null
|| tabsLayout.getMeasuredWidth() >= getMeasuredWidth())
return;
if (tabsLayout.getChildCount() <= 0)
return;
if (tabViews == null) {
tabViews = new ArrayList<View>();
} else {
tabViews.clear();
}
for (int w = 0; w < tabsLayout.getChildCount(); w++) {
tabViews.add(tabsLayout.getChildAt(w));
}
adjustChildWidthWithParent(
tabViews,
getMeasuredWidth() - tabsLayout.getPaddingLeft()
- tabsLayout.getPaddingRight(), widthMeasureSpec,
heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public SimpleImageAdapter(ViewGroup container, boolean isDelete, int rowCount) {
super(container.getContext(), null, R.layout.activity_gradview_item);
this.containerWidth = container.getMeasuredWidth();
this.isDelete = isDelete;
this.rowCount = rowCount;
initImgSize();
}
private static void createCircle(Context context, ViewGroup rootView,
SpringSystem springSystem,
SpringConfig coasting,
SpringConfig gravity,
int diameter,
Drawable backgroundDrawable) {
final Spring xSpring = springSystem.createSpring().setSpringConfig(coasting);
final Spring ySpring = springSystem.createSpring().setSpringConfig(gravity);
// create view
View view = new View(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(diameter, diameter);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
view.setLayoutParams(params);
view.setBackgroundDrawable(backgroundDrawable);
rootView.addView(view);
// generate random direction and magnitude
double magnitude = Math.random() * 1000 + 3000;
double angle = Math.random() * Math.PI / 2 + Math.PI / 4;
xSpring.setVelocity(magnitude * Math.cos(angle));
ySpring.setVelocity(-magnitude * Math.sin(angle));
int maxX = rootView.getMeasuredWidth() / 2 + diameter;
xSpring.addListener(new Destroyer(rootView, view, -maxX, maxX));
int maxY = rootView.getMeasuredHeight() / 2 + diameter;
ySpring.addListener(new Destroyer(rootView, view, -maxY, maxY));
xSpring.addListener(new Performer(view, View.TRANSLATION_X));
ySpring.addListener(new Performer(view, View.TRANSLATION_Y));
// set a different end value to cause the animation to play
xSpring.setEndValue(2);
ySpring.setEndValue(9001);
}
private Point calculateTopRightCoordinates(@NonNull ViewGroup parent) {
return new Point(parent.getMeasuredWidth() - pipWidth - framePadding,
framePadding + extraPaddingTop);
}
private Point calculateBottomRightCoordinates(@NonNull ViewGroup parent) {
return new Point(parent.getMeasuredWidth() - pipWidth - framePadding,
parent.getMeasuredHeight() - pipHeight - framePadding - extraPaddingBottom);
}
@SuppressWarnings("deprecation")
@Override
public DefaultAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TabTextView tabTextView = new TabTextView(parent.getContext());
if (mTabSelectedTextColorSet) {
tabTextView.setTextColor(tabTextView.createColorStateList(
tabTextView.getCurrentTextColor(), mTabSelectedTextColor));
}
ViewCompat.setPaddingRelative(tabTextView, mTabPaddingStart, mTabPaddingTop,
mTabPaddingEnd, mTabPaddingBottom);
tabTextView.setTextAppearance(parent.getContext(), mTabTextAppearance);
tabTextView.setGravity(Gravity.CENTER);
tabTextView.setMaxLines(MAX_TAB_TEXT_LINES);
tabTextView.setEllipsize(TextUtils.TruncateAt.END);
if (mTabOnScreenLimit > 0) {
int width = parent.getMeasuredWidth() / mTabOnScreenLimit;
tabTextView.setMaxWidth(width);
tabTextView.setMinWidth(width);
} else {
if (mTabMaxWidth > 0) {
tabTextView.setMaxWidth(mTabMaxWidth);
}
tabTextView.setMinWidth(mTabMinWidth);
}
tabTextView.setTextAppearance(tabTextView.getContext(), mTabTextAppearance);
if (mTabSelectedTextColorSet) {
tabTextView.setTextColor(tabTextView.createColorStateList(
tabTextView.getCurrentTextColor(), mTabSelectedTextColor));
}
if (mTabBackgroundResId != 0) {
tabTextView.setBackgroundDrawable(
AppCompatResources.getDrawable(tabTextView.getContext(), mTabBackgroundResId));
}
tabTextView.setLayoutParams(createLayoutParamsForTabs());
return new ViewHolder(tabTextView);
}
public void onCreateViewHolder(ViewGroup parent, View itemView) {
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) itemView.getLayoutParams();
lp.width = parent.getMeasuredWidth() - UiUtils.dip2px(itemView.getContext(), 2 * (mPagePadding + mShowLeftCardWidth));
itemView.setLayoutParams(lp);
}
@Override
public MenuNavItem onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_nav_item, parent, false);
v.getLayoutParams().width = parent.getMeasuredWidth() / navItems.size();
return new MenuNavItem(v);
}