FileDocCategorySizeDatePackage
ClipDrawable.javaAPI DocAndroid 1.5 API8264Wed May 06 22:42:00 BST 2009android.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.

It can be defined in an XML file with the <clip> element.

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
mClipState
private final Rect
mTmpRect
public static final int
HORIZONTAL
public static final int
VERTICAL
Constructors Summary
ClipDrawable()

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

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

        this(null);

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

        if (drawable != null) {
            drawable.setCallback(this);
        }
    
private ClipDrawable(ClipState state)

        mClipState = new ClipState(state, this);
    
Methods Summary
public voiddraw(Canvas canvas)

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

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

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

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

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

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

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

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

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

        super.inflate(r, parser, attrs);

        int type;

        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ClipDrawable);

        int orientation = a.getInt(
                com.android.internal.R.styleable.ClipDrawable_clipOrientation,
                HORIZONTAL);
        int g = a.getInt(com.android.internal.R.styleable.ClipDrawable_gravity, Gravity.LEFT);
        Drawable dr = a.getDrawable(com.android.internal.R.styleable.ClipDrawable_drawable);

        a.recycle();

        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);
        }

        if (dr == null) {
            throw new IllegalArgumentException("No drawable specified for <clip>");
        }

        mClipState.mDrawable = dr;
        mClipState.mOrientation = orientation;
        mClipState.mGravity = g;

        dr.setCallback(this);
    
public voidinvalidateDrawable(Drawable who)

        if (mCallback != null) {
            mCallback.invalidateDrawable(this);
        }
    
public booleanisStateful()

        return mClipState.mDrawable.isStateful();
    
protected voidonBoundsChange(Rect bounds)

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

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

        return mClipState.mDrawable.setState(state);
    
public voidscheduleDrawable(Drawable who, java.lang.Runnable what, long when)

        if (mCallback != null) {
            mCallback.scheduleDrawable(this, what, when);
        }
    
public voidsetAlpha(int alpha)

        mClipState.mDrawable.setAlpha(alpha);
    
public voidsetColorFilter(ColorFilter cf)

        mClipState.mDrawable.setColorFilter(cf);
    
public booleansetVisible(boolean visible, boolean restart)

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

        if (mCallback != null) {
            mCallback.unscheduleDrawable(this, what);
        }