Skip to content

Commit eaa0a04

Browse files
Philip MilneAndroid (Google) Code Review
authored andcommitted
Merge "Share Insets instances between views that have the same background (Drawable)"
2 parents 08ee43d + bbd51f1 commit eaa0a04

File tree

6 files changed

+21
-30
lines changed

6 files changed

+21
-30
lines changed

core/java/android/view/View.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13844,13 +13844,7 @@ public boolean isPaddingRelative() {
1384413844
*/
1384513845
public Insets getLayoutInsets() {
1384613846
if (mLayoutInsets == null) {
13847-
if (mBackground == null) {
13848-
mLayoutInsets = Insets.NONE;
13849-
} else {
13850-
Rect insetRect = new Rect();
13851-
boolean hasInsets = mBackground.getLayoutInsets(insetRect);
13852-
mLayoutInsets = hasInsets ? Insets.of(insetRect) : Insets.NONE;
13853-
}
13847+
mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
1385413848
}
1385513849
return mLayoutInsets;
1385613850
}

graphics/java/android/graphics/Insets.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static Insets of(int left, int top, int right, int bottom) {
6767
* @return an Insets instance with the appropriate values
6868
*/
6969
public static Insets of(Rect r) {
70-
return of(r.left, r.top, r.right, r.bottom);
70+
return (r == null) ? NONE : of(r.left, r.top, r.right, r.bottom);
7171
}
7272

7373
/**

graphics/java/android/graphics/drawable/Drawable.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package android.graphics.drawable;
1818

19+
import android.graphics.Insets;
1920
import org.xmlpull.v1.XmlPullParser;
2021
import org.xmlpull.v1.XmlPullParserException;
2122

@@ -706,16 +707,12 @@ public boolean getPadding(Rect padding) {
706707

707708
/**
708709
* Return in insets the layout insets suggested by this Drawable for use with alignment
709-
* operations during layout. Positive values move toward the
710-
* center of the Drawable. Returns true if this drawable
711-
* actually has a layout insets, else false. When false is returned, the padding
712-
* is always set to 0.
710+
* operations during layout.
713711
*
714712
* @hide
715713
*/
716-
public boolean getLayoutInsets(Rect insets) {
717-
insets.set(0, 0, 0, 0);
718-
return false;
714+
public Insets getLayoutInsets() {
715+
return Insets.NONE;
719716
}
720717

721718
/**

graphics/java/android/graphics/drawable/DrawableContainer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.res.Resources;
2020
import android.graphics.Canvas;
2121
import android.graphics.ColorFilter;
22+
import android.graphics.Insets;
2223
import android.graphics.PixelFormat;
2324
import android.graphics.Rect;
2425
import android.os.SystemClock;
@@ -94,12 +95,8 @@ public boolean getPadding(Rect padding) {
9495
* @hide
9596
*/
9697
@Override
97-
public boolean getLayoutInsets(Rect insets) {
98-
if (mCurrDrawable != null) {
99-
return mCurrDrawable.getLayoutInsets(insets);
100-
} else {
101-
return super.getLayoutInsets(insets);
102-
}
98+
public Insets getLayoutInsets() {
99+
return (mCurrDrawable == null) ? Insets.NONE : mCurrDrawable.getLayoutInsets();
103100
}
104101

105102
@Override

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.graphics.BitmapFactory;
2323
import android.graphics.Canvas;
2424
import android.graphics.ColorFilter;
25+
import android.graphics.Insets;
2526
import android.graphics.NinePatch;
2627
import android.graphics.Paint;
2728
import android.graphics.PixelFormat;
@@ -224,13 +225,8 @@ public boolean getPadding(Rect padding) {
224225
* @hide
225226
*/
226227
@Override
227-
public boolean getLayoutInsets(Rect insets) {
228-
Rect layoutInsets = mNinePatchState.mLayoutInsets;
229-
if (layoutInsets == null) {
230-
return super.getLayoutInsets(insets);
231-
}
232-
insets.set(layoutInsets);
233-
return true;
228+
public Insets getLayoutInsets() {
229+
return mNinePatchState.mLayoutInsets;
234230
}
235231

236232
@Override
@@ -390,7 +386,7 @@ public Drawable mutate() {
390386
private final static class NinePatchState extends ConstantState {
391387
final NinePatch mNinePatch;
392388
final Rect mPadding;
393-
final Rect mLayoutInsets;
389+
final Insets mLayoutInsets;
394390
final boolean mDither;
395391
int mChangingConfigurations;
396392
int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
@@ -406,7 +402,7 @@ private final static class NinePatchState extends ConstantState {
406402
NinePatchState(NinePatch ninePatch, Rect rect, Rect layoutInsets, boolean dither) {
407403
mNinePatch = ninePatch;
408404
mPadding = rect;
409-
mLayoutInsets = layoutInsets;
405+
mLayoutInsets = Insets.of(layoutInsets);
410406
mDither = dither;
411407
}
412408

tests/GridLayoutTest/src/com/android/test/layout/LayoutInsetsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public static View create(Context context) {
3333
c.setText("Email setup");
3434
p.addView(c);
3535
}
36+
{
37+
Button c = new Button(context);
38+
c.setBackgroundResource(R.drawable.btn_default);
39+
c.setText("Test");
40+
p.addView(c);
41+
}
42+
3643
{
3744
Button c = new Button(context);
3845
c.setBackgroundResource(R.drawable.btn_default);

0 commit comments

Comments
 (0)