Skip to content

Commit 65340c1

Browse files
Philip MilneAndroid (Google) Code Review
authored andcommitted
Merge "Fixes for optical bounds feature." into jb-dev
2 parents f613da0 + 7a23b49 commit 65340c1

File tree

13 files changed

+104
-129
lines changed

13 files changed

+104
-129
lines changed

api/current.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24493,12 +24493,12 @@ package android.view {
2449324493
method public void startLayoutAnimation();
2449424494
method public void startViewTransition(android.view.View);
2449524495
method public void updateViewLayout(android.view.View, android.view.ViewGroup.LayoutParams);
24496+
field public static final int CLIP_BOUNDS = 0; // 0x0
2449624497
field protected static final int CLIP_TO_PADDING_MASK = 34; // 0x22
24497-
field public static final int COMPONENT_BOUNDS = 0; // 0x0
2449824498
field public static final int FOCUS_AFTER_DESCENDANTS = 262144; // 0x40000
2449924499
field public static final int FOCUS_BEFORE_DESCENDANTS = 131072; // 0x20000
2450024500
field public static final int FOCUS_BLOCK_DESCENDANTS = 393216; // 0x60000
24501-
field public static final int LAYOUT_BOUNDS = 1; // 0x1
24501+
field public static final int OPTICAL_BOUNDS = 1; // 0x1
2450224502
field public static final int PERSISTENT_ALL_CACHES = 3; // 0x3
2450324503
field public static final int PERSISTENT_ANIMATION_CACHE = 1; // 0x1
2450424504
field public static final int PERSISTENT_NO_CACHE = 0; // 0x0

core/java/android/view/View.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13853,7 +13853,7 @@ public boolean isPaddingRelative() {
1385313853
/**
1385413854
* @hide
1385513855
*/
13856-
public Insets getLayoutInsets() {
13856+
public Insets getOpticalInsets() {
1385713857
if (mLayoutInsets == null) {
1385813858
mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
1385913859
}

core/java/android/view/ViewGroup.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,9 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
173173
protected int mGroupFlags;
174174

175175
/*
176-
* THe layout mode: either {@link #UNDEFINED_LAYOUT_MODE}, {@link #COMPONENT_BOUNDS} or
177-
* {@link #LAYOUT_BOUNDS}
176+
* The layout mode: either {@link #CLIP_BOUNDS} or {@link #OPTICAL_BOUNDS}
178177
*/
179-
private int mLayoutMode = UNDEFINED_LAYOUT_MODE;
178+
private int mLayoutMode = CLIP_BOUNDS;
180179

181180
/**
182181
* NOTE: If you change the flags below make sure to reflect the changes
@@ -345,19 +344,20 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
345344

346345
// Layout Modes
347346

348-
private static final int UNDEFINED_LAYOUT_MODE = -1;
349-
350347
/**
351348
* This constant is a {@link #setLayoutMode(int) layoutMode}.
352-
* Component bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
349+
* Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
353350
* {@link #getRight() right} and {@link #getBottom() bottom}.
354351
*/
355-
public static final int COMPONENT_BOUNDS = 0;
352+
public static final int CLIP_BOUNDS = 0;
356353

357354
/**
358355
* This constant is a {@link #setLayoutMode(int) layoutMode}.
356+
* Optical bounds describe where a widget appears to be. They sit inside the clip
357+
* bounds which need to cover a larger area to allow other effects,
358+
* such as shadows and glows, to be drawn.
359359
*/
360-
public static final int LAYOUT_BOUNDS = 1;
360+
public static final int OPTICAL_BOUNDS = 1;
361361

362362
/**
363363
* We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
@@ -2690,10 +2690,10 @@ protected void onDebugDrawMargins(Canvas canvas) {
26902690
*/
26912691
protected void onDebugDraw(Canvas canvas) {
26922692
// Draw optical bounds
2693-
if (getLayoutMode() == LAYOUT_BOUNDS) {
2693+
if (getLayoutMode() == OPTICAL_BOUNDS) {
26942694
for (int i = 0; i < getChildCount(); i++) {
26952695
View c = getChildAt(i);
2696-
Insets insets = c.getLayoutInsets();
2696+
Insets insets = c.getOpticalInsets();
26972697
drawRect(canvas,
26982698
c.getLeft() + insets.left,
26992699
c.getTop() + insets.top,
@@ -4591,37 +4591,22 @@ public void setPersistentDrawingCache(int drawingCacheToKeep) {
45914591
}
45924592

45934593
/**
4594-
* Returns the basis of alignment during the layout of this view group:
4595-
* either {@link #COMPONENT_BOUNDS} or {@link #LAYOUT_BOUNDS}.
4594+
* Returns the basis of alignment during layout operations on this view group:
4595+
* either {@link #CLIP_BOUNDS} or {@link #OPTICAL_BOUNDS}.
45964596
*
45974597
* @return the layout mode to use during layout operations
45984598
*
45994599
* @see #setLayoutMode(int)
46004600
*/
46014601
public int getLayoutMode() {
4602-
if (mLayoutMode == UNDEFINED_LAYOUT_MODE) {
4603-
ViewParent parent = getParent();
4604-
if (parent instanceof ViewGroup) {
4605-
ViewGroup viewGroup = (ViewGroup) parent;
4606-
return viewGroup.getLayoutMode();
4607-
} else {
4608-
int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4609-
boolean preJellyBean = targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN;
4610-
return preJellyBean ? COMPONENT_BOUNDS : LAYOUT_BOUNDS;
4611-
}
4612-
4613-
}
46144602
return mLayoutMode;
46154603
}
46164604

46174605
/**
4618-
* Sets the basis of alignment during alignment of this view group.
4619-
* Valid values are either {@link #COMPONENT_BOUNDS} or {@link #LAYOUT_BOUNDS}.
4606+
* Sets the basis of alignment during the layout of this view group.
4607+
* Valid values are either {@link #CLIP_BOUNDS} or {@link #OPTICAL_BOUNDS}.
46204608
* <p>
4621-
* The default is to query the property of the parent if this view group has a parent.
4622-
* If this ViewGroup is the root of the view hierarchy the default
4623-
* value is {@link #LAYOUT_BOUNDS} for target SDK's greater than JellyBean,
4624-
* {@link #LAYOUT_BOUNDS} otherwise.
4609+
* The default is {@link #CLIP_BOUNDS}.
46254610
*
46264611
* @param layoutMode the layout mode to use during layout operations
46274612
*

core/java/android/widget/GridLayout.java

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,10 @@ private int getDefaultMargin(View c, boolean horizontal, boolean leading) {
581581
}
582582

583583
private int getDefaultMargin(View c, boolean isAtEdge, boolean horizontal, boolean leading) {
584-
return isAtEdge ? DEFAULT_CONTAINER_MARGIN : getDefaultMargin(c, horizontal, leading);
584+
return /*isAtEdge ? DEFAULT_CONTAINER_MARGIN :*/ getDefaultMargin(c, horizontal, leading);
585585
}
586586

587-
private int getDefaultMarginValue(View c, LayoutParams p, boolean horizontal, boolean leading) {
587+
private int getDefaultMargin(View c, LayoutParams p, boolean horizontal, boolean leading) {
588588
if (!useDefaultMargins) {
589589
return 0;
590590
}
@@ -602,7 +602,7 @@ int getMargin1(View view, boolean horizontal, boolean leading) {
602602
int margin = horizontal ?
603603
(leading ? lp.leftMargin : lp.rightMargin) :
604604
(leading ? lp.topMargin : lp.bottomMargin);
605-
return margin == UNDEFINED ? getDefaultMarginValue(view, lp, horizontal, leading) : margin;
605+
return margin == UNDEFINED ? getDefaultMargin(view, lp, horizontal, leading) : margin;
606606
}
607607

608608
private int getMargin(View view, boolean horizontal, boolean leading) {
@@ -777,11 +777,12 @@ protected void onDebugDrawMargins(Canvas canvas) {
777777
LayoutParams lp = new LayoutParams();
778778
for (int i = 0; i < getChildCount(); i++) {
779779
View c = getChildAt(i);
780+
Insets insets = getLayoutMode() == OPTICAL_BOUNDS ? c.getOpticalInsets() : Insets.NONE;
780781
lp.setMargins(
781-
getMargin1(c, true, true),
782-
getMargin1(c, false, true),
783-
getMargin1(c, true, false),
784-
getMargin1(c, false, false));
782+
getMargin1(c, true, true) - insets.left,
783+
getMargin1(c, false, true) - insets.top,
784+
getMargin1(c, true, false) - insets.right,
785+
getMargin1(c, false, false) - insets.bottom);
785786
lp.onDebugDraw(c, canvas);
786787
}
787788
}
@@ -946,7 +947,12 @@ protected void onMeasure(int widthSpec, int heightSpec) {
946947
}
947948

948949
private int getMeasurement(View c, boolean horizontal) {
949-
return horizontal ? c.getMeasuredWidth() : c.getMeasuredHeight();
950+
int result = horizontal ? c.getMeasuredWidth() : c.getMeasuredHeight();
951+
if (getLayoutMode() == OPTICAL_BOUNDS) {
952+
Insets insets = c.getOpticalInsets();
953+
return result - (horizontal ? insets.left + insets.right : insets.top + insets.bottom);
954+
}
955+
return result;
950956
}
951957

952958
final int getMeasurementIncludingMargin(View c, boolean horizontal) {
@@ -1052,6 +1058,14 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
10521058
targetWidth - width - paddingRight - rightMargin - dx;
10531059
int cy = paddingTop + y1 + gravityOffsetY + alignmentOffsetY + topMargin;
10541060

1061+
boolean useLayoutBounds = getLayoutMode() == OPTICAL_BOUNDS;
1062+
if (useLayoutBounds) {
1063+
Insets insets = c.getOpticalInsets();
1064+
cx -= insets.left;
1065+
cy -= insets.top;
1066+
width += (insets.left + insets.right);
1067+
height += (insets.top + insets.bottom);
1068+
}
10551069
if (width != c.getMeasuredWidth() || height != c.getMeasuredHeight()) {
10561070
c.measure(makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY));
10571071
}
@@ -2135,21 +2149,8 @@ protected int size(boolean min) {
21352149
return before + after;
21362150
}
21372151

2138-
private int getAlignmentValue(GridLayout gl, View c, int size, Alignment a, boolean horiz) {
2139-
boolean useLayoutBounds = gl.getLayoutMode() == LAYOUT_BOUNDS;
2140-
if (!useLayoutBounds) {
2141-
return a.getAlignmentValue(c, size);
2142-
} else {
2143-
Insets insets = c.getLayoutInsets();
2144-
int leadingInset = horiz ? insets.left : insets.top; // RTL?
2145-
int trailingInset = horiz ? insets.right : insets.bottom; // RTL?
2146-
int totalInset = leadingInset + trailingInset;
2147-
return leadingInset + a.getAlignmentValue(c, size - totalInset);
2148-
}
2149-
}
2150-
21512152
protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean horizontal) {
2152-
return before - getAlignmentValue(gl, c, size, a, horizontal);
2153+
return before - a.getAlignmentValue(c, size, gl.getLayoutMode());
21532154
}
21542155

21552156
protected final void include(GridLayout gl, View c, Spec spec, Axis axis) {
@@ -2158,7 +2159,7 @@ protected final void include(GridLayout gl, View c, Spec spec, Axis axis) {
21582159
int size = gl.getMeasurementIncludingMargin(c, horizontal);
21592160
Alignment alignment = gl.getAlignment(spec.alignment, horizontal);
21602161
// todo test this works correctly when the returned value is UNDEFINED
2161-
int before = getAlignmentValue(gl, c, size, alignment, horizontal);
2162+
int before = alignment.getAlignmentValue(c, size, gl.getLayoutMode());
21622163
include(before, size - before);
21632164
}
21642165

@@ -2441,9 +2442,10 @@ public static abstract class Alignment {
24412442
*
24422443
* @param view the view to which this alignment should be applied
24432444
* @param viewSize the measured size of the view
2445+
* @param mode the basis of alignment: CLIP or OPTICAL
24442446
* @return the alignment value
24452447
*/
2446-
abstract int getAlignmentValue(View view, int viewSize);
2448+
abstract int getAlignmentValue(View view, int viewSize, int mode);
24472449

24482450
/**
24492451
* Returns the size of the view specified by this alignment.
@@ -2473,7 +2475,7 @@ int getGravityOffset(View view, int cellDelta) {
24732475
}
24742476

24752477
@Override
2476-
public int getAlignmentValue(View view, int viewSize) {
2478+
public int getAlignmentValue(View view, int viewSize, int mode) {
24772479
return UNDEFINED;
24782480
}
24792481
};
@@ -2489,7 +2491,7 @@ int getGravityOffset(View view, int cellDelta) {
24892491
}
24902492

24912493
@Override
2492-
public int getAlignmentValue(View view, int viewSize) {
2494+
public int getAlignmentValue(View view, int viewSize, int mode) {
24932495
return 0;
24942496
}
24952497
};
@@ -2505,7 +2507,7 @@ int getGravityOffset(View view, int cellDelta) {
25052507
}
25062508

25072509
@Override
2508-
public int getAlignmentValue(View view, int viewSize) {
2510+
public int getAlignmentValue(View view, int viewSize, int mode) {
25092511
return viewSize;
25102512
}
25112513
};
@@ -2542,8 +2544,8 @@ int getGravityOffset(View view, int cellDelta) {
25422544
}
25432545

25442546
@Override
2545-
public int getAlignmentValue(View view, int viewSize) {
2546-
return (!view.isLayoutRtl() ? ltr : rtl).getAlignmentValue(view, viewSize);
2547+
public int getAlignmentValue(View view, int viewSize, int mode) {
2548+
return (!view.isLayoutRtl() ? ltr : rtl).getAlignmentValue(view, viewSize, mode);
25472549
}
25482550
};
25492551
}
@@ -2572,7 +2574,7 @@ int getGravityOffset(View view, int cellDelta) {
25722574
}
25732575

25742576
@Override
2575-
public int getAlignmentValue(View view, int viewSize) {
2577+
public int getAlignmentValue(View view, int viewSize, int mode) {
25762578
return viewSize >> 1;
25772579
}
25782580
};
@@ -2591,9 +2593,16 @@ int getGravityOffset(View view, int cellDelta) {
25912593
}
25922594

25932595
@Override
2594-
public int getAlignmentValue(View view, int viewSize) {
2596+
public int getAlignmentValue(View view, int viewSize, int mode) {
25952597
int baseline = view.getBaseline();
2596-
return (baseline == -1) ? UNDEFINED : baseline;
2598+
if (baseline == -1) {
2599+
return UNDEFINED;
2600+
} else {
2601+
if (mode == OPTICAL_BOUNDS) {
2602+
return baseline - view.getOpticalInsets().top;
2603+
}
2604+
return baseline;
2605+
}
25972606
}
25982607

25992608
@Override
@@ -2644,7 +2653,7 @@ int getGravityOffset(View view, int cellDelta) {
26442653
}
26452654

26462655
@Override
2647-
public int getAlignmentValue(View view, int viewSize) {
2656+
public int getAlignmentValue(View view, int viewSize, int mode) {
26482657
return UNDEFINED;
26492658
}
26502659

core/res/res/values/dimens.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
<dimen name="activity_chooser_popup_min_width">200dip</dimen>
195195

196196
<!-- The default gap between components in a layout. -->
197-
<dimen name="default_gap">16dip</dimen>
197+
<dimen name="default_gap">8dip</dimen>
198198

199199
<!-- Text padding for dropdown items -->
200200
<dimen name="dropdownitem_text_padding_left">8dip</dimen>

graphics/java/android/graphics/drawable/NinePatchDrawable.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class NinePatchDrawable extends Drawable {
5656
private NinePatchState mNinePatchState;
5757
private NinePatch mNinePatch;
5858
private Rect mPadding;
59+
private Insets mLayoutInsets = Insets.NONE;
5960
private Paint mPaint;
6061
private boolean mMutated;
6162

@@ -180,12 +181,21 @@ public void setTargetDensity(int density) {
180181
}
181182
}
182183

184+
private Insets scaleFromDensity(Insets insets, int sdensity, int tdensity) {
185+
int left = Bitmap.scaleFromDensity(insets.left, sdensity, tdensity);
186+
int top = Bitmap.scaleFromDensity(insets.top, sdensity, tdensity);
187+
int right = Bitmap.scaleFromDensity(insets.right, sdensity, tdensity);
188+
int bottom = Bitmap.scaleFromDensity(insets.bottom, sdensity, tdensity);
189+
return Insets.of(left, top, right, bottom);
190+
}
191+
183192
private void computeBitmapSize() {
184193
final int sdensity = mNinePatch.getDensity();
185194
final int tdensity = mTargetDensity;
186195
if (sdensity == tdensity) {
187196
mBitmapWidth = mNinePatch.getWidth();
188197
mBitmapHeight = mNinePatch.getHeight();
198+
mLayoutInsets = mNinePatchState.mLayoutInsets;
189199
} else {
190200
mBitmapWidth = Bitmap.scaleFromDensity(mNinePatch.getWidth(),
191201
sdensity, tdensity);
@@ -202,6 +212,7 @@ private void computeBitmapSize() {
202212
dest.right = Bitmap.scaleFromDensity(src.right, sdensity, tdensity);
203213
dest.bottom = Bitmap.scaleFromDensity(src.bottom, sdensity, tdensity);
204214
}
215+
mLayoutInsets = scaleFromDensity(mNinePatchState.mLayoutInsets, sdensity, tdensity);
205216
}
206217
}
207218

@@ -226,7 +237,7 @@ public boolean getPadding(Rect padding) {
226237
*/
227238
@Override
228239
public Insets getLayoutInsets() {
229-
return mNinePatchState.mLayoutInsets;
240+
return mLayoutInsets;
230241
}
231242

232243
@Override

tests/GridLayoutTest/res/drawable/btn_default.xml

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/GridLayoutTest/res/drawable/my_btn_default_normal.9.png renamed to tests/GridLayoutTest/res/drawable/btn_default_normal.9.png

File renamed without changes.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)