diff options
Diffstat (limited to 'library/main/src/com/android/setupwizardlib/view/FillContentLayout.java')
-rw-r--r-- | library/main/src/com/android/setupwizardlib/view/FillContentLayout.java | 145 |
1 files changed, 71 insertions, 74 deletions
diff --git a/library/main/src/com/android/setupwizardlib/view/FillContentLayout.java b/library/main/src/com/android/setupwizardlib/view/FillContentLayout.java index 2c28090..b72d4d2 100644 --- a/library/main/src/com/android/setupwizardlib/view/FillContentLayout.java +++ b/library/main/src/com/android/setupwizardlib/view/FillContentLayout.java @@ -21,13 +21,12 @@ import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; - import com.android.setupwizardlib.R; /** - * A layout that will measure its children size based on the space it is given, by using its - * {@code android:minWidth}, {@code android:minHeight}, {@code android:maxWidth}, and - * {@code android:maxHeight} values. + * A layout that will measure its children size based on the space it is given, by using its {@code + * android:minWidth}, {@code android:minHeight}, {@code android:maxWidth}, and {@code + * android:maxHeight} values. * * <p>Typically this is used to show an illustration image or video on the screen. For optimal UX, * those assets typically want to occupy the remaining space available on screen within a certain @@ -42,84 +41,82 @@ import com.android.setupwizardlib.R; */ public class FillContentLayout extends FrameLayout { - private int mMaxWidth; - private int mMaxHeight; - - public FillContentLayout(Context context) { - this(context, null); - } - - public FillContentLayout(Context context, AttributeSet attrs) { - this(context, attrs, R.attr.suwFillContentLayoutStyle); - } - - public FillContentLayout(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - init(context, attrs, defStyleAttr); - } + private int maxWidth; + private int maxHeight; - private void init(Context context, AttributeSet attrs, int defStyleAttr) { - TypedArray a = context.obtainStyledAttributes( - attrs, - R.styleable.SuwFillContentLayout, - defStyleAttr, - 0); + public FillContentLayout(Context context) { + this(context, null); + } - mMaxHeight = a.getDimensionPixelSize( - R.styleable.SuwFillContentLayout_android_maxHeight, -1); - mMaxWidth = a.getDimensionPixelSize(R.styleable.SuwFillContentLayout_android_maxWidth, -1); + public FillContentLayout(Context context, AttributeSet attrs) { + this(context, attrs, R.attr.suwFillContentLayoutStyle); + } - a.recycle(); - } + public FillContentLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(context, attrs, defStyleAttr); + } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - // Measure this view with the minWidth and minHeight, without asking the children. - // (Children size is the drawable's intrinsic size, and we don't want that to influence - // the size of the illustration). - setMeasuredDimension( - getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), - getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); - - int childCount = getChildCount(); - for (int i = 0; i < childCount; i++) { - measureIllustrationChild(getChildAt(i), getMeasuredWidth(), getMeasuredHeight()); - } - } + private void init(Context context, AttributeSet attrs, int defStyleAttr) { + TypedArray a = + context.obtainStyledAttributes(attrs, R.styleable.SuwFillContentLayout, defStyleAttr, 0); - private void measureIllustrationChild(View child, int parentWidth, int parentHeight) { - // Modified from ViewGroup#measureChildWithMargins - final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); + maxHeight = a.getDimensionPixelSize(R.styleable.SuwFillContentLayout_android_maxHeight, -1); + maxWidth = a.getDimensionPixelSize(R.styleable.SuwFillContentLayout_android_maxWidth, -1); - // Create measure specs that are no bigger than min(parentSize, maxSize) + a.recycle(); + } - int childWidthMeasureSpec = getMaxSizeMeasureSpec( - Math.min(mMaxWidth, parentWidth), - getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin, - lp.width); - int childHeightMeasureSpec = getMaxSizeMeasureSpec( - Math.min(mMaxHeight, parentHeight), - getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, - lp.height); + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + // Measure this view with the minWidth and minHeight, without asking the children. + // (Children size is the drawable's intrinsic size, and we don't want that to influence + // the size of the illustration). + setMeasuredDimension( + getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), + getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); - child.measure(childWidthMeasureSpec, childHeightMeasureSpec); + int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + measureIllustrationChild(getChildAt(i), getMeasuredWidth(), getMeasuredHeight()); } - - private static int getMaxSizeMeasureSpec(int maxSize, int padding, int childDimension) { - // Modified from ViewGroup#getChildMeasureSpec - int size = Math.max(0, maxSize - padding); - - if (childDimension >= 0) { - // Child wants a specific size... so be it - return MeasureSpec.makeMeasureSpec(childDimension, MeasureSpec.EXACTLY); - } else if (childDimension == LayoutParams.MATCH_PARENT) { - // Child wants to be our size. So be it. - return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); - } else if (childDimension == LayoutParams.WRAP_CONTENT) { - // Child wants to determine its own size. It can't be - // bigger than us. - return MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST); - } - return 0; + } + + private void measureIllustrationChild(View child, int parentWidth, int parentHeight) { + // Modified from ViewGroup#measureChildWithMargins + final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); + + // Create measure specs that are no bigger than min(parentSize, maxSize) + + int childWidthMeasureSpec = + getMaxSizeMeasureSpec( + Math.min(maxWidth, parentWidth), + getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin, + lp.width); + int childHeightMeasureSpec = + getMaxSizeMeasureSpec( + Math.min(maxHeight, parentHeight), + getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, + lp.height); + + child.measure(childWidthMeasureSpec, childHeightMeasureSpec); + } + + private static int getMaxSizeMeasureSpec(int maxSize, int padding, int childDimension) { + // Modified from ViewGroup#getChildMeasureSpec + int size = Math.max(0, maxSize - padding); + + if (childDimension >= 0) { + // Child wants a specific size... so be it + return MeasureSpec.makeMeasureSpec(childDimension, MeasureSpec.EXACTLY); + } else if (childDimension == LayoutParams.MATCH_PARENT) { + // Child wants to be our size. So be it. + return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); + } else if (childDimension == LayoutParams.WRAP_CONTENT) { + // Child wants to determine its own size. It can't be + // bigger than us. + return MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST); } + return 0; + } } |