ColorDrawable.java revision 5f49c3023a512efbef8bc9515d310c7a72be4af2
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics.drawable;
18
19import android.graphics.*;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
25
26import java.io.IOException;
27
28/**
29 * A specialized Drawable that fills the Canvas with a specified color.
30 * Note that a ColorDrawable ignores the ColorFilter.
31 *
32 * <p>It can be defined in an XML file with the <code>&lt;color></code> element.</p>
33 *
34 * @attr ref android.R.styleable#ColorDrawable_color
35 */
36public class ColorDrawable extends Drawable {
37    private ColorState mState;
38    private final Paint mPaint = new Paint();
39    private boolean mMutated;
40
41    /**
42     * Creates a new black ColorDrawable.
43     */
44    public ColorDrawable() {
45        this(null);
46        mMutated = true;
47    }
48
49    /**
50     * Creates a new ColorDrawable with the specified color.
51     *
52     * @param color The color to draw.
53     */
54    public ColorDrawable(int color) {
55        this(null);
56        setColor(color);
57        mMutated = true;
58    }
59
60    private ColorDrawable(ColorState state) {
61        mState = new ColorState(state);
62    }
63
64    @Override
65    public int getChangingConfigurations() {
66        return super.getChangingConfigurations() | mState.mChangingConfigurations;
67    }
68
69    /**
70     * A mutable BitmapDrawable still shares its Bitmap with any other Drawable
71     * that comes from the same resource.
72     *
73     * @return This drawable.
74     */
75    @Override
76    public Drawable mutate() {
77        if (!mMutated && super.mutate() == this) {
78            mState = new ColorState(mState);
79            mMutated = true;
80        }
81        return this;
82    }
83
84    @Override
85    public void draw(Canvas canvas) {
86        if ((mState.mUseColor >>> 24) != 0) {
87            mPaint.setColor(mState.mUseColor);
88            canvas.drawRect(getBounds(), mPaint);
89        }
90    }
91
92    /**
93     * Gets the drawable's color value.
94     *
95     * @return int The color to draw.
96     */
97    public int getColor() {
98        return mState.mUseColor;
99    }
100
101    /**
102     * Sets the drawable's color value. This action will clobber the results of prior calls to
103     * {@link #setAlpha(int)} on this object, which side-affected the underlying color.
104     *
105     * @param color The color to draw.
106     */
107    public void setColor(int color) {
108        if (mState.mBaseColor != color || mState.mUseColor != color) {
109            invalidateSelf();
110            mState.mBaseColor = mState.mUseColor = color;
111        }
112    }
113
114    /**
115     * Returns the alpha value of this drawable's color.
116     *
117     * @return A value between 0 and 255.
118     */
119    public int getAlpha() {
120        return mState.mUseColor >>> 24;
121    }
122
123    /**
124     * Sets the color's alpha value.
125     *
126     * @param alpha The alpha value to set, between 0 and 255.
127     */
128    public void setAlpha(int alpha) {
129        alpha += alpha >> 7;   // make it 0..256
130        int baseAlpha = mState.mBaseColor >>> 24;
131        int useAlpha = baseAlpha * alpha >> 8;
132        int oldUseColor = mState.mUseColor;
133        mState.mUseColor = (mState.mBaseColor << 8 >>> 8) | (useAlpha << 24);
134        if (oldUseColor != mState.mUseColor) {
135            invalidateSelf();
136        }
137    }
138
139    /**
140     * Setting a color filter on a ColorDrawable has no effect.
141     *
142     * @param colorFilter Ignore.
143     */
144    public void setColorFilter(ColorFilter colorFilter) {
145    }
146
147    public int getOpacity() {
148        switch (mState.mUseColor >>> 24) {
149            case 255:
150                return PixelFormat.OPAQUE;
151            case 0:
152                return PixelFormat.TRANSPARENT;
153        }
154        return PixelFormat.TRANSLUCENT;
155    }
156
157    @Override
158    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
159            throws XmlPullParserException, IOException {
160        super.inflate(r, parser, attrs);
161
162        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ColorDrawable);
163
164        int color = mState.mBaseColor;
165        color = a.getColor(com.android.internal.R.styleable.ColorDrawable_color, color);
166        mState.mBaseColor = mState.mUseColor = color;
167
168        a.recycle();
169    }
170
171    @Override
172    public ConstantState getConstantState() {
173        mState.mChangingConfigurations = getChangingConfigurations();
174        return mState;
175    }
176
177    final static class ColorState extends ConstantState {
178        int mBaseColor; // base color, independent of setAlpha()
179        int mUseColor;  // basecolor modulated by setAlpha()
180        int mChangingConfigurations;
181
182        ColorState(ColorState state) {
183            if (state != null) {
184                mBaseColor = state.mBaseColor;
185                mUseColor = state.mUseColor;
186                mChangingConfigurations = state.mChangingConfigurations;
187            }
188        }
189
190        @Override
191        public Drawable newDrawable() {
192            return new ColorDrawable(this);
193        }
194
195        @Override
196        public Drawable newDrawable(Resources res) {
197            return new ColorDrawable(this);
198        }
199
200        @Override
201        public int getChangingConfigurations() {
202            return mChangingConfigurations;
203        }
204    }
205}
206