Created
August 20, 2014 13:02
-
-
Save sw1ftc0d3r/0158db56bf9067b65dc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.kontalk.ui; | |
| import android.content.Context; | |
| import android.graphics.drawable.Drawable; | |
| import android.util.AttributeSet; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.FrameLayout; | |
| import java.util.ArrayList; | |
| public class FrameLayoutFixed extends FrameLayout { | |
| private final ArrayList<View> mMatchParentChildren = new ArrayList<View>(1); | |
| public FrameLayoutFixed(Context context) { | |
| super(context); | |
| } | |
| public FrameLayoutFixed(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| } | |
| public FrameLayoutFixed(Context context, AttributeSet attrs, int defStyle) { | |
| super(context, attrs, defStyle); | |
| } | |
| public final int getMeasuredStateFixed(View view) { | |
| return (view.getMeasuredWidth()&0xff000000) | |
| | ((view.getMeasuredHeight()>>16) | |
| & (0xff000000>>16)); | |
| } | |
| public static int resolveSizeAndStateFixed(int size, int measureSpec, int childMeasuredState) { | |
| int result = size; | |
| int specMode = MeasureSpec.getMode(measureSpec); | |
| int specSize = MeasureSpec.getSize(measureSpec); | |
| switch (specMode) { | |
| case MeasureSpec.UNSPECIFIED: | |
| result = size; | |
| break; | |
| case MeasureSpec.AT_MOST: | |
| if (specSize < size) { | |
| result = specSize | 0x01000000; | |
| } else { | |
| result = size; | |
| } | |
| break; | |
| case MeasureSpec.EXACTLY: | |
| result = specSize; | |
| break; | |
| } | |
| return result | (childMeasuredState&0xff000000); | |
| } | |
| @Override | |
| protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
| try { | |
| int count = getChildCount(); | |
| final boolean measureMatchParentChildren = | |
| MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || | |
| MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; | |
| mMatchParentChildren.clear(); | |
| int maxHeight = 0; | |
| int maxWidth = 0; | |
| int childState = 0; | |
| for (int i = 0; i < count; i++) { | |
| final View child = getChildAt(i); | |
| if (child.getVisibility() != GONE) { | |
| measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); | |
| final LayoutParams lp = (LayoutParams) child.getLayoutParams(); | |
| maxWidth = Math.max(maxWidth, | |
| child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); | |
| maxHeight = Math.max(maxHeight, | |
| child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); | |
| childState |= getMeasuredStateFixed(child); | |
| if (measureMatchParentChildren) { | |
| if (lp.width == LayoutParams.MATCH_PARENT || | |
| lp.height == LayoutParams.MATCH_PARENT) { | |
| mMatchParentChildren.add(child); | |
| } | |
| } | |
| } | |
| } | |
| // Account for padding too | |
| maxWidth += getPaddingLeft() + getPaddingRight(); | |
| maxHeight += getPaddingTop() + getPaddingBottom(); | |
| // Check against our minimum height and width | |
| maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); | |
| maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); | |
| // Check against our foreground's minimum height and width | |
| final Drawable drawable = getForeground(); | |
| if (drawable != null) { | |
| maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); | |
| maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); | |
| } | |
| setMeasuredDimension(resolveSizeAndStateFixed(maxWidth, widthMeasureSpec, childState), | |
| resolveSizeAndStateFixed(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT)); | |
| count = mMatchParentChildren.size(); | |
| if (count > 1) { | |
| for (int i = 0; i < count; i++) { | |
| final View child = mMatchParentChildren.get(i); | |
| final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); | |
| int childWidthMeasureSpec; | |
| int childHeightMeasureSpec; | |
| if (lp.width == LayoutParams.MATCH_PARENT) { | |
| childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth() - | |
| getPaddingLeft() - getPaddingRight() - | |
| lp.leftMargin - lp.rightMargin, | |
| MeasureSpec.EXACTLY); | |
| } else { | |
| childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, | |
| getPaddingLeft() + getPaddingRight() + | |
| lp.leftMargin + lp.rightMargin, | |
| lp.width); | |
| } | |
| if (lp.height == LayoutParams.MATCH_PARENT) { | |
| childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight() - | |
| getPaddingTop() - getPaddingBottom() - | |
| lp.topMargin - lp.bottomMargin, | |
| MeasureSpec.EXACTLY); | |
| } else { | |
| childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, | |
| getPaddingTop() + getPaddingBottom() + | |
| lp.topMargin + lp.bottomMargin, | |
| lp.height); | |
| } | |
| child.measure(childWidthMeasureSpec, childHeightMeasureSpec); | |
| } | |
| } | |
| } catch (Exception e) { | |
| Log.e("Kontalk", String.valueOf(e)); | |
| super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment