Share Insets instances between views that have the same background (Drawable)

Change-Id: I47d93ccca6f553b678d25966d10d7a0a97cfa5ea
This commit is contained in:
Philip Milne
2012-04-18 15:09:05 -07:00
parent f341e5545c
commit bbd51f1e36
6 changed files with 21 additions and 30 deletions

View File

@@ -13846,13 +13846,7 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal
*/
public Insets getLayoutInsets() {
if (mLayoutInsets == null) {
if (mBackground == null) {
mLayoutInsets = Insets.NONE;
} else {
Rect insetRect = new Rect();
boolean hasInsets = mBackground.getLayoutInsets(insetRect);
mLayoutInsets = hasInsets ? Insets.of(insetRect) : Insets.NONE;
}
mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
}
return mLayoutInsets;
}

View File

@@ -67,7 +67,7 @@ public class Insets {
* @return an Insets instance with the appropriate values
*/
public static Insets of(Rect r) {
return of(r.left, r.top, r.right, r.bottom);
return (r == null) ? NONE : of(r.left, r.top, r.right, r.bottom);
}
/**

View File

@@ -16,6 +16,7 @@
package android.graphics.drawable;
import android.graphics.Insets;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@@ -706,16 +707,12 @@ public abstract class Drawable {
/**
* Return in insets the layout insets suggested by this Drawable for use with alignment
* operations during layout. Positive values move toward the
* center of the Drawable. Returns true if this drawable
* actually has a layout insets, else false. When false is returned, the padding
* is always set to 0.
* operations during layout.
*
* @hide
*/
public boolean getLayoutInsets(Rect insets) {
insets.set(0, 0, 0, 0);
return false;
public Insets getLayoutInsets() {
return Insets.NONE;
}
/**

View File

@@ -19,6 +19,7 @@ package android.graphics.drawable;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Insets;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.SystemClock;
@@ -94,12 +95,8 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
* @hide
*/
@Override
public boolean getLayoutInsets(Rect insets) {
if (mCurrDrawable != null) {
return mCurrDrawable.getLayoutInsets(insets);
} else {
return super.getLayoutInsets(insets);
}
public Insets getLayoutInsets() {
return (mCurrDrawable == null) ? Insets.NONE : mCurrDrawable.getLayoutInsets();
}
@Override

View File

@@ -22,6 +22,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Insets;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.PixelFormat;
@@ -224,13 +225,8 @@ public class NinePatchDrawable extends Drawable {
* @hide
*/
@Override
public boolean getLayoutInsets(Rect insets) {
Rect layoutInsets = mNinePatchState.mLayoutInsets;
if (layoutInsets == null) {
return super.getLayoutInsets(insets);
}
insets.set(layoutInsets);
return true;
public Insets getLayoutInsets() {
return mNinePatchState.mLayoutInsets;
}
@Override
@@ -390,7 +386,7 @@ public class NinePatchDrawable extends Drawable {
private final static class NinePatchState extends ConstantState {
final NinePatch mNinePatch;
final Rect mPadding;
final Rect mLayoutInsets;
final Insets mLayoutInsets;
final boolean mDither;
int mChangingConfigurations;
int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
@@ -406,7 +402,7 @@ public class NinePatchDrawable extends Drawable {
NinePatchState(NinePatch ninePatch, Rect rect, Rect layoutInsets, boolean dither) {
mNinePatch = ninePatch;
mPadding = rect;
mLayoutInsets = layoutInsets;
mLayoutInsets = Insets.of(layoutInsets);
mDither = dither;
}

View File

@@ -33,6 +33,13 @@ public class LayoutInsetsTest extends Activity {
c.setText("Email setup");
p.addView(c);
}
{
Button c = new Button(context);
c.setBackgroundResource(R.drawable.btn_default);
c.setText("Test");
p.addView(c);
}
{
Button c = new Button(context);
c.setBackgroundResource(R.drawable.btn_default);