FileDocCategorySizeDatePackage
ClipDrawable.javaAPI DocAndroid 5.1 API12905Thu Mar 12 22:22:30 GMT 2015android.graphics.drawable

ClipDrawable

public class ClipDrawable extends Drawable implements Drawable.Callback
A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars, by increasing the drawable's level with {@link android.graphics.drawable.Drawable#setLevel(int) setLevel()}.

Note: The drawable is clipped completely and not visible when the level is 0 and fully revealed when the level is 10,000.

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

attr
ref android.R.styleable#ClipDrawable_clipOrientation
attr
ref android.R.styleable#ClipDrawable_gravity
attr
ref android.R.styleable#ClipDrawable_drawable

Fields Summary
private ClipState
mState
private final Rect
mTmpRect
public static final int
HORIZONTAL
public static final int
VERTICAL
private boolean
mMutated
Constructors Summary
ClipDrawable()


     
        this(null, null);
    
public ClipDrawable(Drawable drawable, int gravity, int orientation)

param
orientation Bitwise-or of {@link #HORIZONTAL} and/or {@link #VERTICAL}

        this(null, null);

        mState.mDrawable = drawable;
        mState.mGravity = gravity;
        mState.mOrientation = orientation;

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

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

        super.applyTheme(t);

        final ClipState state = mState;
        if (state == null || state.mThemeAttrs == null) {
            return;
        }

        final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.ClipDrawable);
        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(Canvas canvas)


        if (mState.mDrawable.getLevel() == 0) {
            return;
        }

        final Rect r = mTmpRect;
        final Rect bounds = getBounds();
        int level = getLevel();
        int w = bounds.width();
        final int iw = 0; //mState.mDrawable.getIntrinsicWidth();
        if ((mState.mOrientation & HORIZONTAL) != 0) {
            w -= (w - iw) * (10000 - level) / 10000;
        }
        int h = bounds.height();
        final int ih = 0; //mState.mDrawable.getIntrinsicHeight();
        if ((mState.mOrientation & VERTICAL) != 0) {
            h -= (h - ih) * (10000 - level) / 10000;
        }
        final int layoutDirection = getLayoutDirection();
        Gravity.apply(mState.mGravity, w, h, bounds, r, layoutDirection);

        if (w > 0 && h > 0) {
            canvas.save();
            canvas.clipRect(r);
            mState.mDrawable.draw(canvas);
            canvas.restore();
        }
    
public intgetAlpha()

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

        return super.getChangingConfigurations()
                | mState.mChangingConfigurations
                | mState.mDrawable.getChangingConfigurations();
    
public ConstantStategetConstantState()

        if (mState.canConstantState()) {
            mState.mChangingConfigurations = getChangingConfigurations();
            return mState;
        }
        return null;
    
public intgetIntrinsicHeight()

        return mState.mDrawable.getIntrinsicHeight();
    
public intgetIntrinsicWidth()

        return mState.mDrawable.getIntrinsicWidth();
    
public intgetOpacity()

        return mState.mDrawable.getOpacity();
    
public booleangetPadding(Rect padding)

        // XXX need to adjust padding!
        return mState.mDrawable.getPadding(padding);
    
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.content.res.Resources.Theme theme)

        super.inflate(r, parser, attrs, theme);

        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ClipDrawable);

        // 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)

        Drawable dr = null;
        int type;
        final int outerDepth = parser.getDepth();
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type != XmlPullParser.START_TAG) {
                continue;
            }
            dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
        }

        if (dr != null) {
            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(Rect bounds)

        mState.mDrawable.setBounds(bounds);
    
protected booleanonLevelChange(int level)

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

        return mState.mDrawable.setState(state);
    
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(ColorFilter cf)

        mState.mDrawable.setColorFilter(cf);
    
public voidsetLayoutDirection(int layoutDirection)

hide

        mState.mDrawable.setLayoutDirection(layoutDirection);
        super.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 ClipState state = mState;

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

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

        state.mOrientation = a.getInt(R.styleable.ClipDrawable_clipOrientation, state.mOrientation);
        state.mGravity = a.getInt(R.styleable.ClipDrawable_gravity, state.mGravity);

        final Drawable dr = a.getDrawable(R.styleable.ClipDrawable_drawable);
        if (dr != null) {
            state.mDrawable = dr;
            dr.setCallback(this);
        }
    
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.ClipDrawable_drawable] == 0)) {
            throw new XmlPullParserException(a.getPositionDescription()
                    + ": <clip> tag requires a 'drawable' attribute or "
                    + "child tag defining a drawable");
        }