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

ShapeDrawable

public class ShapeDrawable extends Drawable
A Drawable object that draws primitive shapes. A ShapeDrawable takes a {@link android.graphics.drawable.shapes.Shape} object and manages its presence on the screen. If no Shape is given, then the ShapeDrawable will default to a {@link android.graphics.drawable.shapes.RectShape}.
attr
ref android.R.styleable#ShapeDrawablePadding_left
attr
ref android.R.styleable#ShapeDrawablePadding_top
attr
ref android.R.styleable#ShapeDrawablePadding_right
attr
ref android.R.styleable#ShapeDrawablePadding_bottom
attr
ref android.R.styleable#ShapeDrawable_color
attr
ref android.R.styleable#ShapeDrawable_width
attr
ref android.R.styleable#ShapeDrawable_height

Fields Summary
private ShapeState
mShapeState
private boolean
mMutated
Constructors Summary
public ShapeDrawable()
ShapeDrawable constructor.

        this((ShapeState) null);
    
public ShapeDrawable(android.graphics.drawable.shapes.Shape s)
Creates a ShapeDrawable with a specified Shape.

param
s the Shape that this ShapeDrawable should be

        this((ShapeState) null);
        
        mShapeState.mShape = s;
    
private ShapeDrawable(ShapeState state)

        mShapeState = new ShapeState(state);
    
Methods Summary
public voiddraw(Canvas canvas)

        Rect r = getBounds();
        Paint paint = mShapeState.mPaint;

        int prevAlpha = paint.getAlpha();
        paint.setAlpha(modulateAlpha(prevAlpha, mShapeState.mAlpha));

        if (mShapeState.mShape != null) {
            // need the save both for the translate, and for the (unknown) Shape
            int count = canvas.save();
            canvas.translate(r.left, r.top);
            onDraw(mShapeState.mShape, canvas, paint);
            canvas.restoreToCount(count);
        } else {
            canvas.drawRect(r, paint);
        }
        
        // restore
        paint.setAlpha(prevAlpha);
    
public intgetChangingConfigurations()

        return super.getChangingConfigurations()
                | mShapeState.mChangingConfigurations;
    
public ConstantStategetConstantState()

        mShapeState.mChangingConfigurations = super.getChangingConfigurations();
        return mShapeState;
    
public intgetIntrinsicHeight()

        return mShapeState.mIntrinsicHeight;
    
public intgetIntrinsicWidth()

        return mShapeState.mIntrinsicWidth;
    
public intgetOpacity()

        if (mShapeState.mShape == null) {
            final Paint p = mShapeState.mPaint;
            if (p.getXfermode() == null) {
                final int alpha = p.getAlpha();
                if (alpha == 0) {
                    return PixelFormat.TRANSPARENT;
                }
                if (alpha == 255) {
                    return PixelFormat.OPAQUE;
                }
            }
        }
        // not sure, so be safe
        return PixelFormat.TRANSLUCENT;
    
public booleangetPadding(Rect padding)

        if (mShapeState.mPadding != null) {
            padding.set(mShapeState.mPadding);
            return true;
        } else {
            return super.getPadding(padding);
        }
    
public PaintgetPaint()
Returns the Paint used to draw the shape.

        return mShapeState.mPaint;
    
public android.graphics.drawable.ShapeDrawable$ShaderFactorygetShaderFactory()
Returns the ShaderFactory used by this ShapeDrawable for requesting a {@link android.graphics.Shader}.

        return mShapeState.mShaderFactory;
    
public android.graphics.drawable.shapes.ShapegetShape()
Returns the Shape of this ShapeDrawable.

        return mShapeState.mShape;
    
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)

        super.inflate(r, parser, attrs);

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

        int color = mShapeState.mPaint.getColor();
        color = a.getColor(com.android.internal.R.styleable.ShapeDrawable_color, color);
        mShapeState.mPaint.setColor(color);
            
        setIntrinsicWidth((int)
                a.getDimension(com.android.internal.R.styleable.ShapeDrawable_width, 0f));
        setIntrinsicHeight((int)
                a.getDimension(com.android.internal.R.styleable.ShapeDrawable_height, 0f));

        a.recycle();

        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;
            }
            
            final String name = parser.getName();
            // call our subclass
            if (!inflateTag(name, r, parser, attrs)) {
                android.util.Log.w("drawable", "Unknown element: " + name +
                        " for ShapeDrawable " + this);
            }
        }
    
protected booleaninflateTag(java.lang.String name, android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)
Subclasses override this to parse custom subelements. If you handle it, return true, else return super.inflateTag(...).


        if (name.equals("padding")) {
            TypedArray a = r.obtainAttributes(attrs,
                    com.android.internal.R.styleable.ShapeDrawablePadding);
            setPadding(a.getInt(com.android.internal.R.styleable.ShapeDrawablePadding_left, 0),
                       a.getInt(com.android.internal.R.styleable.ShapeDrawablePadding_top, 0),
                       a.getInt(com.android.internal.R.styleable.ShapeDrawablePadding_right, 0),
                       a.getInt(com.android.internal.R.styleable.ShapeDrawablePadding_bottom, 0));
            a.recycle();
            return true;
        }

        return false;
    
private static intmodulateAlpha(int paintAlpha, int alpha)

        int scale = alpha + (alpha >>> 7);  // convert to 0..256
        return paintAlpha * scale >>> 8;
    
public Drawablemutate()

        if (!mMutated && super.mutate() == this) {
            mShapeState.mPaint = new Paint(mShapeState.mPaint);
            mShapeState.mPadding = new Rect(mShapeState.mPadding);
            try {
                mShapeState.mShape = mShapeState.mShape.clone();
            } catch (CloneNotSupportedException e) {
                return null;
            }
            mMutated = true;
        }
        return this;
    
protected voidonBoundsChange(Rect bounds)

        super.onBoundsChange(bounds);
        updateShape();
    
protected voidonDraw(android.graphics.drawable.shapes.Shape shape, Canvas canvas, Paint paint)
Called from the drawable's draw() method after the canvas has been set to draw the shape at (0,0). Subclasses can override for special effects such as multiple layers, stroking, etc.

        shape.draw(canvas, paint);
    
public voidsetAlpha(int alpha)
Set the alpha level for this drawable [0..255]. Note that this drawable also has a color in its paint, which has an alpha as well. These two values are automatically combined during drawing. Thus if the color's alpha is 75% (i.e. 192) and the drawable's alpha is 50% (i.e. 128), then the combined alpha that will be used during drawing will be 37.5% (i.e. 96).

        mShapeState.mAlpha = alpha;
    
public voidsetColorFilter(ColorFilter cf)

        mShapeState.mPaint.setColorFilter(cf);
    
public voidsetDither(boolean dither)

        mShapeState.mPaint.setDither(dither);
    
public voidsetIntrinsicHeight(int height)
Sets the intrinsic (default) height for this shape.

param
height the intrinsic height (in pixels)

        mShapeState.mIntrinsicHeight = height;
    
public voidsetIntrinsicWidth(int width)
Sets the intrinsic (default) width for this shape.

param
width the intrinsic width (in pixels)

        mShapeState.mIntrinsicWidth = width;
    
public voidsetPadding(Rect padding)
Sets padding for this shape, defined by a Rect object. Define the padding in the Rect object as: left, top, right, bottom.

        if (padding == null) {
            mShapeState.mPadding = null;
        } else {
            if (mShapeState.mPadding == null) {
                mShapeState.mPadding = new Rect();
            }
            mShapeState.mPadding.set(padding);
        }
    
public voidsetPadding(int left, int top, int right, int bottom)
Sets padding for the shape.

param
left padding for the left side (in pixels)
param
top padding for the top (in pixels)
param
right padding for the right side (in pixels)
param
bottom padding for the bottom (in pixels)

        if ((left | top | right | bottom) == 0) {
            mShapeState.mPadding = null;
        } else {
            if (mShapeState.mPadding == null) {
                mShapeState.mPadding = new Rect();
            }
            mShapeState.mPadding.set(left, top, right, bottom);
        }
    
public voidsetShaderFactory(android.graphics.drawable.ShapeDrawable$ShaderFactory fact)
Sets a ShaderFactory to which requests for a {@link android.graphics.Shader} object will be made.

param
fact an instance of your ShaderFactory implementation

        mShapeState.mShaderFactory = fact;
    
public voidsetShape(android.graphics.drawable.shapes.Shape s)
Sets the Shape of this ShapeDrawable.

        mShapeState.mShape = s;
        updateShape();
    
private voidupdateShape()

        if (mShapeState.mShape != null) {
            final Rect r = getBounds();
            final int w = r.width();
            final int h = r.height();

            mShapeState.mShape.resize(w, h);
            if (mShapeState.mShaderFactory != null) {
                mShapeState.mPaint.setShader(mShapeState.mShaderFactory.resize(w, h));
            }
        }