FileDocCategorySizeDatePackage
InsetDrawable.javaAPI DocAndroid 5.1 API16010Thu Mar 12 22:22:30 GMT 2015android.graphics.drawable

InsetDrawable

public class InsetDrawable extends Drawable implements Drawable.Callback
A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View's actual bounds.

It can be defined in an XML file with the <inset> element. For more information, see the guide to Drawable Resources.

attr
ref android.R.styleable#InsetDrawable_visible
attr
ref android.R.styleable#InsetDrawable_drawable
attr
ref android.R.styleable#InsetDrawable_insetLeft
attr
ref android.R.styleable#InsetDrawable_insetRight
attr
ref android.R.styleable#InsetDrawable_insetTop
attr
ref android.R.styleable#InsetDrawable_insetBottom

Fields Summary
private final android.graphics.Rect
mTmpRect
private final InsetState
mState
private boolean
mMutated
Constructors Summary
InsetDrawable()


    /*package*/  
        this(null, null);
    
public InsetDrawable(Drawable drawable, int inset)

        this(drawable, inset, inset, inset, inset);
    
public InsetDrawable(Drawable drawable, int insetLeft, int insetTop, int insetRight, int insetBottom)

        this(null, null);

        mState.mDrawable = drawable;
        mState.mInsetLeft = insetLeft;
        mState.mInsetTop = insetTop;
        mState.mInsetRight = insetRight;
        mState.mInsetBottom = insetBottom;

        if (drawable != null) {
            drawable.setCallback(this);
        }
    
private InsetDrawable(InsetState state, android.content.res.Resources res)

        mState = new InsetState(state, this, res);
    
Methods Summary
public voidapplyTheme(android.content.res.Resources.Theme t)

        super.applyTheme(t);

        final InsetState state = mState;
        if (state == null) {
            return;
        }

        if (state.mThemeAttrs != null) {
            final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.InsetDrawable);
            try {
                updateStateFromTypedArray(a);
                verifyRequiredAttributes(a);
            } catch (XmlPullParserException e) {
                throw new RuntimeException(e);
            } finally {
                a.recycle();
            }
        }

        if (state.mDrawable != null && state.mDrawable.canApplyTheme()) {
            state.mDrawable.applyTheme(t);
        }
    
public booleancanApplyTheme()

        return (mState != null && mState.canApplyTheme()) || super.canApplyTheme();
    
public voidclearMutated()

hide

        super.clearMutated();
        mState.mDrawable.clearMutated();
        mMutated = false;
    
public voiddraw(android.graphics.Canvas canvas)

        mState.mDrawable.draw(canvas);
    
public intgetAlpha()

        return mState.mDrawable.getAlpha();
    
public intgetChangingConfigurations()

        return super.getChangingConfigurations()
                | mState.mChangingConfigurations
                | mState.mDrawable.getChangingConfigurations();
    
public android.graphics.drawable.Drawable.ConstantStategetConstantState()

        if (mState.canConstantState()) {
            mState.mChangingConfigurations = getChangingConfigurations();
            return mState;
        }
        return null;
    
public DrawablegetDrawable()
Returns the drawable wrapped by this InsetDrawable. May be null.

        return mState.mDrawable;
    
public voidgetHotspotBounds(android.graphics.Rect outRect)

hide

        mState.mDrawable.getHotspotBounds(outRect);
    
public intgetIntrinsicHeight()

        return mState.mDrawable.getIntrinsicHeight()
                + mState.mInsetTop + mState.mInsetBottom;
    
public intgetIntrinsicWidth()

        return mState.mDrawable.getIntrinsicWidth()
                + mState.mInsetLeft + mState.mInsetRight;
    
public intgetOpacity()

        final InsetState state = mState;
        final int opacity = state.mDrawable.getOpacity();
        if (opacity == PixelFormat.OPAQUE && (state.mInsetLeft > 0 || state.mInsetTop > 0
                || state.mInsetRight > 0 || state.mInsetBottom > 0)) {
            return PixelFormat.TRANSLUCENT;
        }
        return opacity;
    
public android.graphics.InsetsgetOpticalInsets()

hide

        final Insets contentInsets = super.getOpticalInsets();
        return Insets.of(contentInsets.left + mState.mInsetLeft,
                contentInsets.top + mState.mInsetTop,
                contentInsets.right + mState.mInsetRight,
                contentInsets.bottom + mState.mInsetBottom);
    
public voidgetOutline(android.graphics.Outline outline)

        mState.mDrawable.getOutline(outline);
    
public booleangetPadding(android.graphics.Rect padding)

        boolean pad = mState.mDrawable.getPadding(padding);

        padding.left += mState.mInsetLeft;
        padding.right += mState.mInsetRight;
        padding.top += mState.mInsetTop;
        padding.bottom += mState.mInsetBottom;

        return pad || (mState.mInsetLeft | mState.mInsetRight |
                mState.mInsetTop | mState.mInsetBottom) != 0;
    
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.content.res.Resources.Theme theme)

        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.InsetDrawable);
        super.inflateWithAttributes(r, parser, a, R.styleable.InsetDrawable_visible);

        // Reset mDrawable to preserve old multiple-inflate behavior. This is
        // silly, but we have CTS tests that rely on it.
        mState.mDrawable = null;

        updateStateFromTypedArray(a);
        inflateChildElements(r, parser, attrs, theme);
        verifyRequiredAttributes(a);
        a.recycle();
    
private voidinflateChildElements(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.content.res.Resources.Theme theme)

        // Load inner XML elements.
        if (mState.mDrawable == null) {
            int type;
            while ((type=parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(
                        parser.getPositionDescription()
                                + ": <inset> tag requires a 'drawable' attribute or "
                                + "child tag defining a drawable");
            }
            final Drawable dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
            mState.mDrawable = dr;
            dr.setCallback(this);
        }
    
public voidinvalidateDrawable(Drawable who)

        final Callback callback = getCallback();
        if (callback != null) {
            callback.invalidateDrawable(this);
        }
    
public booleanisStateful()

        return mState.mDrawable.isStateful();
    
public Drawablemutate()

        if (!mMutated && super.mutate() == this) {
            mState.mDrawable.mutate();
            mMutated = true;
        }
        return this;
    
protected voidonBoundsChange(android.graphics.Rect bounds)

        final Rect r = mTmpRect;
        r.set(bounds);

        r.left += mState.mInsetLeft;
        r.top += mState.mInsetTop;
        r.right -= mState.mInsetRight;
        r.bottom -= mState.mInsetBottom;

        mState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom);
    
protected booleanonLevelChange(int level)

        return mState.mDrawable.setLevel(level);
    
protected booleanonStateChange(int[] state)

        boolean changed = mState.mDrawable.setState(state);
        onBoundsChange(getBounds());
        return changed;
    
public voidscheduleDrawable(Drawable who, java.lang.Runnable what, long when)

        final Callback callback = getCallback();
        if (callback != null) {
            callback.scheduleDrawable(this, what, when);
        }
    
public voidsetAlpha(int alpha)

        mState.mDrawable.setAlpha(alpha);
    
public voidsetColorFilter(android.graphics.ColorFilter cf)

        mState.mDrawable.setColorFilter(cf);
    
public voidsetHotspot(float x, float y)

        mState.mDrawable.setHotspot(x, y);
    
public voidsetHotspotBounds(int left, int top, int right, int bottom)

        mState.mDrawable.setHotspotBounds(left, top, right, bottom);
    
public voidsetLayoutDirection(int layoutDirection)
{@hide}

        mState.mDrawable.setLayoutDirection(layoutDirection);
    
public voidsetTintList(android.content.res.ColorStateList tint)

        mState.mDrawable.setTintList(tint);
    
public voidsetTintMode(android.graphics.PorterDuff.Mode tintMode)

        mState.mDrawable.setTintMode(tintMode);
    
public booleansetVisible(boolean visible, boolean restart)

        mState.mDrawable.setVisible(visible, restart);
        return super.setVisible(visible, restart);
    
public voidunscheduleDrawable(Drawable who, java.lang.Runnable what)

        final Callback callback = getCallback();
        if (callback != null) {
            callback.unscheduleDrawable(this, what);
        }
    
private voidupdateStateFromTypedArray(android.content.res.TypedArray a)

        final InsetState state = mState;

        // Account for any configuration changes.
        state.mChangingConfigurations |= a.getChangingConfigurations();

        // Extract the theme attributes, if any.
        state.mThemeAttrs = a.extractThemeAttrs();

        final int N = a.getIndexCount();
        for (int i = 0; i < N; i++) {
            final int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.InsetDrawable_drawable:
                    final Drawable dr = a.getDrawable(attr);
                    if (dr != null) {
                        state.mDrawable = dr;
                        dr.setCallback(this);
                    }
                    break;
                case R.styleable.InsetDrawable_inset:
                    final int inset = a.getDimensionPixelOffset(attr, Integer.MIN_VALUE);
                    if (inset != Integer.MIN_VALUE) {
                        state.mInsetLeft = inset;
                        state.mInsetTop = inset;
                        state.mInsetRight = inset;
                        state.mInsetBottom = inset;
                    }
                    break;
                case R.styleable.InsetDrawable_insetLeft:
                    state.mInsetLeft = a.getDimensionPixelOffset(attr, state.mInsetLeft);
                    break;
                case R.styleable.InsetDrawable_insetTop:
                    state.mInsetTop = a.getDimensionPixelOffset(attr, state.mInsetTop);
                    break;
                case R.styleable.InsetDrawable_insetRight:
                    state.mInsetRight = a.getDimensionPixelOffset(attr, state.mInsetRight);
                    break;
                case R.styleable.InsetDrawable_insetBottom:
                    state.mInsetBottom = a.getDimensionPixelOffset(attr, state.mInsetBottom);
                    break;
            }
        }
    
private voidverifyRequiredAttributes(android.content.res.TypedArray a)

        // If we're not waiting on a theme, verify required attributes.
        if (mState.mDrawable == null && (mState.mThemeAttrs == null
                || mState.mThemeAttrs[R.styleable.InsetDrawable_drawable] == 0)) {
            throw new XmlPullParserException(a.getPositionDescription()
                    + ": <inset> tag requires a 'drawable' attribute or "
                    + "child tag defining a drawable");
        }