FileDocCategorySizeDatePackage
ScaleDrawable.javaAPI DocAndroid 1.5 API9151Wed May 06 22:42:00 BST 2009android.graphics.drawable

ScaleDrawable

public class ScaleDrawable extends Drawable implements Drawable.Callback
A Drawable that changes the size of another Drawable based on its current level value. You can control how much the child Drawable changes 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 <scale> element.

attr
ref android.R.styleable#ScaleDrawable_scaleWidth
attr
ref android.R.styleable#ScaleDrawable_scaleHeight
attr
ref android.R.styleable#ScaleDrawable_scaleGravity
attr
ref android.R.styleable#ScaleDrawable_drawable

Fields Summary
private ScaleState
mScaleState
private boolean
mMutated
private final Rect
mTmpRect
Constructors Summary
ScaleDrawable()


     
        this(null);
    
public ScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight)

        this(null);

        mScaleState.mDrawable = drawable;
        mScaleState.mGravity = gravity;
        mScaleState.mScaleWidth = scaleWidth;
        mScaleState.mScaleHeight = scaleHeight;

        if (drawable != null) {
            drawable.setCallback(this);
        }
    
private ScaleDrawable(ScaleState state)

        mScaleState = new ScaleState(state, this);
    
Methods Summary
public voiddraw(Canvas canvas)

        if (mScaleState.mDrawable.getLevel() != 0)
            mScaleState.mDrawable.draw(canvas);
    
public intgetChangingConfigurations()

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

        if (mScaleState.canConstantState()) {
            mScaleState.mChangingConfigurations = super.getChangingConfigurations();
            return mScaleState;
        }
        return null;
    
public DrawablegetDrawable()
Returns the drawable scaled by this ScaleDrawable.

        return mScaleState.mDrawable;
    
public intgetIntrinsicHeight()

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

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

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

        // XXX need to adjust padding!
        return mScaleState.mDrawable.getPadding(padding);
    
private static floatgetPercent(android.content.res.TypedArray a, int name)

        String s = a.getString(name);
        if (s != null) {
            if (s.endsWith("%")) {
                String f = s.substring(0, s.length() - 1);
                return Float.parseFloat(f) / 100.0f;
            }
        }
        return -1;
    
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.ScaleDrawable);

        float sw = getPercent(a, com.android.internal.R.styleable.ScaleDrawable_scaleWidth);
        float sh = getPercent(a, com.android.internal.R.styleable.ScaleDrawable_scaleHeight);
        int g = a.getInt(com.android.internal.R.styleable.ScaleDrawable_scaleGravity, Gravity.LEFT);
        Drawable dr = a.getDrawable(com.android.internal.R.styleable.ScaleDrawable_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 <scale>");
        }

        mScaleState.mDrawable = dr;
        mScaleState.mScaleWidth = sw;
        mScaleState.mScaleHeight = sh;
        mScaleState.mGravity = g;
        if (dr != null) {
            dr.setCallback(this);
        }
    
public voidinvalidateDrawable(Drawable who)

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

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

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

        final Rect r = mTmpRect;
        int level = getLevel();
        int w = bounds.width();
        final int iw = 0; //mScaleState.mDrawable.getIntrinsicWidth();
        if (mScaleState.mScaleWidth > 0) {
            w -= (int) ((w - iw) * (10000 - level) * mScaleState.mScaleWidth / 10000);
        }
        int h = bounds.height();
        final int ih = 0; //mScaleState.mDrawable.getIntrinsicHeight();
        if (mScaleState.mScaleHeight > 0) {
            h -= (int) ((h - ih) * (10000 - level) * mScaleState.mScaleHeight / 10000);
        }
        Gravity.apply(mScaleState.mGravity, w, h, bounds, r);

        if (w > 0 && h > 0) {
            mScaleState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom);
        }
    
protected booleanonLevelChange(int level)

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

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

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

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

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

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

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