Drawable.createFromXml(): implement InsetDrawable inflation

This commit is contained in:
Julian Winkler
2025-10-04 17:33:23 +02:00
parent a01c5e482a
commit 8dcaf3e1ef
3 changed files with 28 additions and 9 deletions

View File

@@ -272,6 +272,11 @@ public class Drawable {
drawable.inflate(resources, parser, attrs, theme); drawable.inflate(resources, parser, attrs, theme);
return drawable; return drawable;
} }
case "inset": {
InsetDrawable drawable = new InsetDrawable();
drawable.inflate(resources, parser, attrs, theme);
return drawable;
}
} }
return null; return null;

View File

@@ -126,11 +126,9 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb
return mDrawable; return mDrawable;
} }
/*@Override
public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser, public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
@NonNull AttributeSet attrs, @Nullable Theme theme) @NonNull AttributeSet attrs, @Nullable Theme theme)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
super.inflate(r, parser, attrs, theme);
final DrawableWrapperState state = mState; final DrawableWrapperState state = mState;
if (state == null) { if (state == null) {
@@ -142,14 +140,14 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb
final int densityDpi = r.getDisplayMetrics().densityDpi; final int densityDpi = r.getDisplayMetrics().densityDpi;
final int targetDensity = densityDpi == 0 ? DisplayMetrics.DENSITY_DEFAULT : densityDpi; final int targetDensity = densityDpi == 0 ? DisplayMetrics.DENSITY_DEFAULT : densityDpi;
state.setDensity(targetDensity); state.setDensity(targetDensity);
state.mSrcDensityOverride = mSrcDensityOverride; // state.mSrcDensityOverride = mSrcDensityOverride;
final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.DrawableWrapper); final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.DrawableWrapper);
updateStateFromTypedArray(a); updateStateFromTypedArray(a);
a.recycle(); a.recycle();
inflateChildDrawable(r, parser, attrs, theme); inflateChildDrawable(r, parser, attrs, theme);
}*/ }
/*@Override /*@Override
public void applyTheme(@NonNull Theme t) { public void applyTheme(@NonNull Theme t) {
@@ -451,7 +449,7 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb
* child element will take precedence over any other child elements or * child element will take precedence over any other child elements or
* explicit drawable attribute. * explicit drawable attribute.
*/ */
/* void inflateChildDrawable(@NonNull Resources r, @NonNull XmlPullParser parser, void inflateChildDrawable(@NonNull Resources r, @NonNull XmlPullParser parser,
@NonNull AttributeSet attrs, @Nullable Theme theme) @NonNull AttributeSet attrs, @Nullable Theme theme)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
// Seek to the first child element. // Seek to the first child element.
@@ -460,15 +458,14 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb
final int outerDepth = parser.getDepth(); final int outerDepth = parser.getDepth();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
if (type == XmlPullParser.START_TAG) { if (type == XmlPullParser.START_TAG) {
dr = Drawable.createFromXmlInnerForDensity(r, parser, attrs, dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
mState.mSrcDensityOverride, theme);
} }
} }
if (dr != null) { if (dr != null) {
setDrawable(dr); setDrawable(dr);
} }
}*/ }
abstract static class DrawableWrapperState extends Drawable.ConstantState { abstract static class DrawableWrapperState extends Drawable.ConstantState {
private int[] mThemeAttrs; private int[] mThemeAttrs;

View File

@@ -1,7 +1,7 @@
package android.graphics.drawable; package android.graphics.drawable;
import android.content.res.Resources;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.DrawableWrapper;
public class InsetDrawable extends DrawableWrapper { public class InsetDrawable extends DrawableWrapper {
@@ -13,5 +13,22 @@ public class InsetDrawable extends DrawableWrapper {
super(drawable); super(drawable);
} }
InsetDrawable() {
super(new InsetState(null, null), null);
}
public boolean getPadding(Rect padding) { return false; } public boolean getPadding(Rect padding) { return false; }
static final class InsetState extends DrawableWrapper.DrawableWrapperState {
InsetState(DrawableWrapperState orig, Resources res) {
super(orig, res);
}
@Override
public Drawable newDrawable(Resources res) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'newDrawable'");
}
}
} }