FileDocCategorySizeDatePackage
ColorDrawable.javaAPI DocAndroid 5.1 API9526Thu Mar 12 22:22:30 GMT 2015android.graphics.drawable

ColorDrawable

public class ColorDrawable extends Drawable
A specialized Drawable that fills the Canvas with a specified color. Note that a ColorDrawable ignores the ColorFilter.

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

attr
ref android.R.styleable#ColorDrawable_color

Fields Summary
private final Paint
mPaint
private ColorState
mColorState
private PorterDuffColorFilter
mTintFilter
private boolean
mMutated
Constructors Summary
public ColorDrawable()
Creates a new black ColorDrawable.

        mColorState = new ColorState();
    
public ColorDrawable(int color)
Creates a new ColorDrawable with the specified color.

param
color The color to draw.

        mColorState = new ColorState();

        setColor(color);
    
private ColorDrawable(ColorState state)

        mColorState = state;
        mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
    
Methods Summary
public voidapplyTheme(android.content.res.Resources.Theme t)

        super.applyTheme(t);

        final ColorState state = mColorState;
        if (state == null || state.mThemeAttrs == null) {
            return;
        }

        final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.ColorDrawable);
        updateStateFromTypedArray(a);
        a.recycle();
    
public booleancanApplyTheme()

        return mColorState.canApplyTheme() || super.canApplyTheme();
    
public voidclearMutated()

hide

        super.clearMutated();
        mMutated = false;
    
public voiddraw(Canvas canvas)

        final ColorFilter colorFilter = mPaint.getColorFilter();
        if ((mColorState.mUseColor >>> 24) != 0 || colorFilter != null || mTintFilter != null) {
            if (colorFilter == null) {
                mPaint.setColorFilter(mTintFilter);
            }

            mPaint.setColor(mColorState.mUseColor);
            canvas.drawRect(getBounds(), mPaint);

            // Restore original color filter.
            mPaint.setColorFilter(colorFilter);
        }
    
public intgetAlpha()
Returns the alpha value of this drawable's color.

return
A value between 0 and 255.

        return mColorState.mUseColor >>> 24;
    
public intgetChangingConfigurations()

        return super.getChangingConfigurations() | mColorState.mChangingConfigurations;
    
public intgetColor()
Gets the drawable's color value.

return
int The color to draw.

        return mColorState.mUseColor;
    
public ConstantStategetConstantState()

        return mColorState;
    
public intgetOpacity()

        if (mTintFilter != null || mPaint.getColorFilter() != null) {
            return PixelFormat.TRANSLUCENT;
        }

        switch (mColorState.mUseColor >>> 24) {
            case 255:
                return PixelFormat.OPAQUE;
            case 0:
                return PixelFormat.TRANSPARENT;
        }
        return PixelFormat.TRANSLUCENT;
    
public voidgetOutline(Outline outline)

        outline.setRect(getBounds());
        outline.setAlpha(getAlpha() / 255.0f);
    
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.ColorDrawable);
        updateStateFromTypedArray(a);
        a.recycle();
    
public booleanisStateful()

        return mColorState.mTint != null && mColorState.mTint.isStateful();
    
public Drawablemutate()
A mutable BitmapDrawable still shares its Bitmap with any other Drawable that comes from the same resource.

return
This drawable.

        if (!mMutated && super.mutate() == this) {
            mColorState = new ColorState(mColorState);
            mMutated = true;
        }
        return this;
    
protected booleanonStateChange(int[] stateSet)

        final ColorState state = mColorState;
        if (state.mTint != null && state.mTintMode != null) {
            mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
            return true;
        }
        return false;
    
public voidsetAlpha(int alpha)
Sets the color's alpha value.

param
alpha The alpha value to set, between 0 and 255.

        alpha += alpha >> 7;   // make it 0..256
        final int baseAlpha = mColorState.mBaseColor >>> 24;
        final int useAlpha = baseAlpha * alpha >> 8;
        final int useColor = (mColorState.mBaseColor << 8 >>> 8) | (useAlpha << 24);
        if (mColorState.mUseColor != useColor) {
            mColorState.mUseColor = useColor;
            invalidateSelf();
        }
    
public voidsetColor(int color)
Sets the drawable's color value. This action will clobber the results of prior calls to {@link #setAlpha(int)} on this object, which side-affected the underlying color.

param
color The color to draw.

        if (mColorState.mBaseColor != color || mColorState.mUseColor != color) {
            mColorState.mBaseColor = mColorState.mUseColor = color;
            invalidateSelf();
        }
    
public voidsetColorFilter(ColorFilter colorFilter)
Sets the color filter applied to this color.

Only supported on version {@link android.os.Build.VERSION_CODES#LOLLIPOP} and above. Calling this method has no effect on earlier versions.

see
android.graphics.drawable.Drawable#setColorFilter(ColorFilter)

        mPaint.setColorFilter(colorFilter);
    
public voidsetTintList(android.content.res.ColorStateList tint)

        mColorState.mTint = tint;
        mTintFilter = updateTintFilter(mTintFilter, tint, mColorState.mTintMode);
        invalidateSelf();
    
public voidsetTintMode(android.graphics.PorterDuff.Mode tintMode)

        mColorState.mTintMode = tintMode;
        mTintFilter = updateTintFilter(mTintFilter, mColorState.mTint, tintMode);
        invalidateSelf();
    
private voidupdateStateFromTypedArray(android.content.res.TypedArray a)
Updates the constant state from the values in the typed array.

        final ColorState state = mColorState;

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

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

        state.mBaseColor = a.getColor(R.styleable.ColorDrawable_color, state.mBaseColor);
        state.mUseColor = state.mBaseColor;