下面列出了android.view.ViewGroup#getMeasuredHeight ( ) 实例代码,或者点击链接到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);
}
}
public ImageHolder onCreate(@NonNull ViewGroup parent, int count, FlowableProcessor<String> imageSelector) {
FormImageBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.form_image, parent, false);
Integer height = null;
if (renderType.equals(ProgramStageSectionRenderingType.SEQUENTIAL.name())) {
height = parent.getMeasuredHeight() / (count > 2 ? 3 : count);
} else if (renderType.equals(ProgramStageSectionRenderingType.MATRIX.name())) {
height = parent.getMeasuredHeight() / (count > 2 ? 2 : count);
}
View rootView = binding.getRoot();
if (height != null) {
ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
layoutParams.height = height;
rootView.setLayoutParams(layoutParams);
}
return new ImageHolder(binding, processor, isBackgroundTransparent, renderType, rootView, imageSelector);
}
@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;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
switch (viewType) {
case HEADER:
final View numpadView = inflater.inflate(app.outlay.R.layout.recycler_numpad, parent, false);
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) numpadView.getLayoutParams();
params.height = parent.getMeasuredHeight() - (context.getResources().getDimensionPixelSize(app.outlay.R.dimen.category_item_height) * 2);
final NumpadViewHolder viewHolder = new NumpadViewHolder(numpadView);
return viewHolder;
default:
final View catView = inflater.inflate(app.outlay.R.layout.item_category, parent, false);
final CategoryViewHolder categoryViewHolder = new CategoryViewHolder(catView);
return categoryViewHolder;
}
}
/**
* Animate the change in progress of this view
*
* @param end The value to set it to, between 0-100, if -1, there is no bar for null value, otherwise
* a circle will be animated.
* @param duration The the time to run the animation over,
*/
public void animateProgress(int end, int duration, int delay) {
mProgress = end;
ViewGroup parent = (ViewGroup) getParent();
int heightToReach = (parent.getMeasuredHeight() * end) / 102;
int initialHeight = (int) mBarWidth;
heightToReach = (heightToReach < initialHeight) ? initialHeight : heightToReach;
if (end == -1) {
heightToReach = 0;
}
setVisibility(View.INVISIBLE);
setPivotY(heightToReach);
setMinimumHeight(heightToReach);
AnimatorSet set = new AnimatorSet();
setVisibility(VISIBLE);
set.playTogether(Glider.glide(Skill.BounceEaseOut, duration, ObjectAnimator.ofFloat(this, "scaleY", 0, 1)));
set.setDuration(duration);
set.setStartDelay(delay);
set = addListenersToSet(set);
set.start();
}
@Override
public void layoutDecoratedWithMargins(@NonNull View child, int left, int top, int right, int bottom) {
if (!mCenter) {
super.layoutDecoratedWithMargins(child, left, top, right, bottom);
return;
}
final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
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() == RecyclerView.HORIZONTAL) {
final int contentHeight = parent.getMeasuredHeight() -
parent.getPaddingTop() - parent.getPaddingBottom();
offset = (contentHeight - (bottom - top)) / 2;
child.layout(left + leftDecorationWidth + lp.leftMargin,
top + topDecorationHeight + lp.topMargin + offset,
right - rightDecorationWidth - lp.rightMargin,
bottom - bottomDecorationHeight - lp.bottomMargin + offset);
} else {
final int contentWidth = parent.getMeasuredWidth() -
parent.getPaddingLeft() - parent.getPaddingRight();
offset = (contentWidth - (right - left)) / 2;
child.layout(left + leftDecorationWidth + offset + lp.leftMargin,
top + topDecorationHeight + lp.topMargin,
right - rightDecorationWidth - lp.rightMargin + offset,
bottom - bottomDecorationHeight - lp.bottomMargin);
}
}
private boolean isScrollable(ViewGroup listView) {
int totalHeight = 0;
for (int i = 0; i < listView.getChildCount(); i++) {
totalHeight += listView.getChildAt(i).getMeasuredHeight();
}
return listView.getMeasuredHeight() < totalHeight;
}
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);
}
@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 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);
}
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);
}
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 View createFooterView(ViewGroup parent) {
int screenHeight = parent.getMeasuredHeight();
int itemHeight = (int) parent.getContext().getResources().getDimension(R.dimen.recyclerview_item_height);
View view = new View(parent.getContext());
parent.addView(view);
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.height = screenHeight - itemHeight;
view.setLayoutParams(lp);
return view;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_boxing_simple_media_item, parent, false);
int height = parent.getMeasuredHeight() / 4;
view.setMinimumHeight(height);
return new MediaViewHolder(view);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View calendarItemView = viewList.get(position);
int realHeight = parent.getMeasuredHeight() - parent.getPaddingBottom() - parent.getPaddingTop();
AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, realHeight / (viewList.size() / 7));
calendarItemView.setLayoutParams(params);
((CalendarView2) parent).bindView(position, calendarItemView);
return calendarItemView;
}
private boolean isScrollable(ViewGroup listView) {
int totalHeight = 0;
for (int i = 0; i < listView.getChildCount(); i++) {
totalHeight += listView.getChildAt(i).getMeasuredHeight();
}
return listView.getMeasuredHeight() < totalHeight;
}
/**
* Find the view touching the bottom of this ViewGroup. Non visible children are ignored,
* however getChildDrawingOrder is not taking into account for simplicity and because it behaves
* inconsistently across platform versions.
*
* @return View touching the bottom of this ViewGroup or null
*/
@Nullable
private static View getBottomView(ViewGroup viewGroup) {
if (viewGroup == null || viewGroup.getChildCount() == 0)
return null;
View bottomView = null;
for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) {
View child = viewGroup.getChildAt(i);
if (child.getVisibility() == View.VISIBLE && child.getBottom() == viewGroup.getMeasuredHeight()) {
bottomView = child;
break;
}
}
return bottomView;
}
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);
}
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);
}
public BaseHolder(LayoutInflater inflater, ViewGroup parent, @LayoutRes int layoutId, boolean isFillParent) {
super(inflate(inflater, parent, layoutId));
if (isFillParent && parent != null) {
getItemView().getLayoutParams().height = parent.getMeasuredHeight() - (parent.getPaddingTop() + parent.getPaddingBottom());
}
}