Add layout to size illustrations

- Add FillContentLayout, which is a (frame)layout which, when set
  to fill the remaining space of its parent, will make sure its
  children are sized between minWidth/minHeight and
  maxWidth/maxHeight.
- Renamed styleable SuwIntrinsicSizeFrameLayout to be consistent with
  the name of the view that uses it.

Test: ./gradlew connectedAndroidTest test
Bug: 38210310
Change-Id: I5b2aa6cfe8b4a05843de25d39cae776609f3d161
This commit is contained in:
Maurice Lam 2017-05-12 14:32:26 -07:00
parent e4aaa62e8f
commit 2da78450d5
8 changed files with 252 additions and 4 deletions

View file

@ -35,6 +35,7 @@
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="colorAccent">@color/suw_color_accent_dark</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="listPreferredItemPaddingLeft">?attr/suwMarginSides</item>
<item name="listPreferredItemPaddingRight">?attr/suwMarginSides</item>
<item name="suwCardBackground">@drawable/suw_card_bg_dark</item>
@ -65,6 +66,7 @@
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="colorAccent">@color/suw_color_accent_light</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="listPreferredItemPaddingLeft">?attr/suwMarginSides</item>
<item name="listPreferredItemPaddingRight">?attr/suwMarginSides</item>
<item name="suwCardBackground">@drawable/suw_card_bg_light</item>
@ -100,6 +102,7 @@
<item name="listPreferredItemPaddingLeft">?attr/suwMarginSides</item>
<item name="listPreferredItemPaddingRight">?attr/suwMarginSides</item>
<item name="suwColorPrimary">?attr/colorPrimary</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="suwDividerInsetEnd">0dp</item>
<item name="suwDividerInsetStart">@dimen/suw_items_glif_icon_divider_inset</item>
<item name="suwDividerInsetStartNoIcon">@dimen/suw_items_glif_text_divider_inset</item>
@ -134,6 +137,7 @@
<item name="listPreferredItemPaddingLeft">?attr/suwMarginSides</item>
<item name="listPreferredItemPaddingRight">?attr/suwMarginSides</item>
<item name="suwColorPrimary">?attr/colorPrimary</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="suwDividerInsetEnd">0dp</item>
<item name="suwDividerInsetStart">@dimen/suw_items_glif_icon_divider_inset</item>
<item name="suwDividerInsetStartNoIcon">@dimen/suw_items_glif_text_divider_inset</item>

View file

@ -39,6 +39,7 @@
</attr>
<attr name="suwCardBackground" format="color|reference" />
<attr name="suwFillContentLayoutStyle" format="reference" />
<attr name="suwDividerCondition">
<enum name="either" value="0" />
<enum name="both" value="1" />
@ -103,11 +104,16 @@
<attr name="suwStatusBarBackground" format="color|reference" />
</declare-styleable>
<declare-styleable name="SuwMaxSizeFrameLayout">
<declare-styleable name="SuwIntrinsicSizeFrameLayout">
<attr name="android:height" />
<attr name="android:width" />
</declare-styleable>
<declare-styleable name="SuwFillContentLayout">
<attr name="android:maxHeight" />
<attr name="android:maxWidth" />
</declare-styleable>
<declare-styleable name="SuwSetupWizardLayout">
<attr name="suwBackground" format="color|reference" />
<attr name="suwBackgroundTile" format="color|reference" />

View file

@ -49,6 +49,12 @@
<dimen name="suw_description_glif_margin_top">3dp</dimen>
<dimen name="suw_description_glif_margin_bottom_lists">24dp</dimen>
<dimen name="suw_content_illustration_max_height">312dp</dimen>
<dimen name="suw_content_illustration_max_width">312dp</dimen>
<dimen name="suw_content_illustration_min_height">172dp</dimen>
<dimen name="suw_content_illustration_min_width">172dp</dimen>
<dimen name="suw_content_illustration_padding_vertical">24dp</dimen>
<!-- Margin on the start to offset for margin in the drawable -->
<dimen name="suw_radio_button_margin_start">-6dp</dimen>
<dimen name="suw_radio_button_margin_top">0dp</dimen>

View file

@ -123,6 +123,21 @@
<item name="android:gravity">top</item>
</style>
<style name="SuwFillContentLayout">
<item name="android:minWidth">@dimen/suw_content_illustration_min_width</item>
<item name="android:minHeight">@dimen/suw_content_illustration_min_height</item>
<item name="android:maxWidth">@dimen/suw_content_illustration_max_width</item>
<item name="android:maxHeight">@dimen/suw_content_illustration_max_height</item>
<item name="android:paddingTop">@dimen/suw_content_illustration_padding_vertical</item>
<item name="android:paddingBottom">@dimen/suw_content_illustration_padding_vertical</item>
</style>
<!-- Ignore UnusedResources: used by clients -->
<style name="SuwContentIllustration" tools:ignore="UnusedResources">
<item name="android:layout_gravity">center</item>
<item name="android:scaleType">fitCenter</item>
</style>
<!-- Card layout (for tablets) -->
<style name="SuwBaseCardTitle">

View file

@ -0,0 +1,125 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.setupwizardlib.view;
import android.content.Context;
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.
*
* <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
* range, and then stop scaling beyond the min/max size attributes. Therefore this view is typically
* used inside a ScrollView with {@code fillViewport} set to true, together with a linear layout
* weight or relative layout to fill the remaining space visible on screen.
*
* <p>When measuring, this view ignores its children and simply layout according to the minWidth /
* minHeight given. Therefore it is common for children of this layout to have width / height set to
* {@code match_parent}. The maxWidth / maxHeight values will then be applied to the children to
* make sure they are not too big.
*/
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 void init(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.SuwFillContentLayout,
defStyleAttr,
0);
mMaxHeight = a.getDimensionPixelSize(
R.styleable.SuwFillContentLayout_android_maxHeight, -1);
mMaxWidth = a.getDimensionPixelSize(R.styleable.SuwFillContentLayout_android_maxWidth, -1);
a.recycle();
}
@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 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(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);
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;
}
}

View file

@ -56,11 +56,11 @@ public class IntrinsicSizeFrameLayout extends FrameLayout {
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SuwMaxSizeFrameLayout, defStyleAttr, 0);
R.styleable.SuwIntrinsicSizeFrameLayout, defStyleAttr, 0);
mIntrinsicHeight =
a.getDimensionPixelSize(R.styleable.SuwMaxSizeFrameLayout_android_height, 0);
a.getDimensionPixelSize(R.styleable.SuwIntrinsicSizeFrameLayout_android_height, 0);
mIntrinsicWidth =
a.getDimensionPixelSize(R.styleable.SuwMaxSizeFrameLayout_android_width, 0);
a.getDimensionPixelSize(R.styleable.SuwIntrinsicSizeFrameLayout_android_width, 0);
a.recycle();
}

View file

@ -41,6 +41,7 @@
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="suwCardBackground">@drawable/suw_card_bg</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="suwDividerInsetEnd">0dp</item>
<item name="suwDividerInsetStart">@dimen/suw_items_icon_divider_inset</item>
<item name="suwDividerInsetStartNoIcon">@dimen/suw_items_text_divider_inset</item>
@ -68,6 +69,7 @@
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="suwCardBackground">@drawable/suw_card_bg</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="suwDividerInsetEnd">0dp</item>
<item name="suwDividerInsetStart">@dimen/suw_items_icon_divider_inset</item>
<item name="suwDividerInsetStartNoIcon">@dimen/suw_items_text_divider_inset</item>
@ -99,6 +101,7 @@
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="suwColorPrimary">?android:attr/colorPrimary</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="suwDividerInsetEnd">0dp</item>
<item name="suwDividerInsetStart">@dimen/suw_items_glif_icon_divider_inset</item>
<item name="suwDividerInsetStartNoIcon">@dimen/suw_items_glif_text_divider_inset</item>
@ -130,6 +133,7 @@
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="suwColorPrimary">?android:attr/colorPrimary</item>
<item name="suwFillContentLayoutStyle">@style/SuwFillContentLayout</item>
<item name="suwDividerInsetEnd">0dp</item>
<item name="suwDividerInsetStart">@dimen/suw_items_glif_icon_divider_inset</item>
<item name="suwDividerInsetStartNoIcon">@dimen/suw_items_glif_text_divider_inset</item>

View file

@ -0,0 +1,88 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.setupwizardlib.view;
import static org.junit.Assert.assertEquals;
import static org.robolectric.RuntimeEnvironment.application;
import android.view.View;
import android.view.View.MeasureSpec;
import com.android.setupwizardlib.BuildConfig;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class FillContentLayoutTest {
@Test
public void testMeasureMinSize() {
FillContentLayout layout = new FillContentLayout(
application,
Robolectric.buildAttributeSet()
.addAttribute(android.R.attr.minWidth, "123dp")
.addAttribute(android.R.attr.minHeight, "123dp")
.build());
layout.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
assertEquals(123, layout.getMeasuredWidth());
assertEquals(123, layout.getMeasuredHeight());
}
@Test
public void testMeasureChildIsSmallerThanMaxSize() {
View child = new View(application);
FillContentLayout layout = new FillContentLayout(
application,
Robolectric.buildAttributeSet()
.addAttribute(android.R.attr.maxWidth, "123dp")
.addAttribute(android.R.attr.maxHeight, "123dp")
.build());
layout.addView(child);
layout.measure(
MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY));
assertEquals(123, child.getMeasuredWidth());
assertEquals(123, child.getMeasuredHeight());
}
@Test
public void testMeasureChildIsSmallerThanParent() {
View child = new View(application);
FillContentLayout layout = new FillContentLayout(
application,
Robolectric.buildAttributeSet()
.addAttribute(android.R.attr.maxWidth, "123dp")
.addAttribute(android.R.attr.maxHeight, "123dp")
.build());
layout.addView(child);
layout.measure(
MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY));
assertEquals(88, child.getMeasuredWidth());
assertEquals(88, child.getMeasuredHeight());
}
}