ColorDrawable.java revision 9891e1fce5f29d0421d34aa481037417bd70853d
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
40    /**
41     * Creates a new black ColorDrawable.
42     */
43    public ColorDrawable() {
44        this(null);
45    }
46
47    /**
48     * Creates a new ColorDrawable with the specified color.
49     *
50     * @param color The color to draw.
51     */
52    public ColorDrawable(int color) {
53        this(null);
54        setColor(color);
55    }
56
57    private ColorDrawable(ColorState state) {
58        mState = new ColorState(state);
59    }
60
61    @Override
62    public int getChangingConfigurations() {
63        return super.getChangingConfigurations() | mState.mChangingConfigurations;
64    }
65
66    @Override
67    public void draw(Canvas canvas) {
68        if ((mState.mUseColor >>> 24) != 0) {
69            mPaint.setColor(mState.mUseColor);
70            canvas.drawRect(getBounds(), mPaint);
71        }
72    }
73
74    /**
75     * Gets the drawable's color value.
76     *
77     * @return int The color to draw.
78     */
79    public int getColor() {
80        return mState.mUseColor;
81    }
82
83    /**
84     * Sets the drawable's color value. This action will clobber the results of prior calls to
85     * {@link #setAlpha(int)} on this object, which side-affected the underlying color.
86     *
87     * @param color The color to draw.
88     */
89    public void setColor(int color) {
90        if (mState.mBaseColor != color || mState.mUseColor != color) {
91            invalidateSelf();
92            mState.mBaseColor = mState.mUseColor = color;
93        }
94    }
95
96    /**
97     * Returns the alpha value of this drawable's color.
98     *
99     * @return A value between 0 and 255.
100     */
101    public int getAlpha() {
102        return mState.mUseColor >>> 24;
103    }
104
105    /**
106     * Sets the color's alpha value.
107     *
108     * @param alpha The alpha value to set, between 0 and 255.
109     */
110    public void setAlpha(int alpha) {
111        alpha += alpha >> 7;   // make it 0..256
112        int baseAlpha = mState.mBaseColor >>> 24;
113        int useAlpha = baseAlpha * alpha >> 8;
114        mState.mUseColor = (mState.mBaseColor << 8 >>> 8) | (useAlpha << 24);
115        invalidateSelf();
116    }
117
118    /**
119     * Setting a color filter on a ColorDrawable has no effect.
120     *
121     * @param colorFilter Ignore.
122     */
123    public void setColorFilter(ColorFilter colorFilter) {
124    }
125
126    public int getOpacity() {
127        switch (mState.mUseColor >>> 24) {
128            case 255:
129                return PixelFormat.OPAQUE;
130            case 0:
131                return PixelFormat.TRANSPARENT;
132        }
133        return PixelFormat.TRANSLUCENT;
134    }
135
136    @Override
137    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
138            throws XmlPullParserException, IOException {
139        super.inflate(r, parser, attrs);
140
141        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ColorDrawable);
142
143        int color = mState.mBaseColor;
144        color = a.getColor(com.android.internal.R.styleable.ColorDrawable_color, color);
145        mState.mBaseColor = mState.mUseColor = color;
146
147        a.recycle();
148    }
149
150    @Override
151    public ConstantState getConstantState() {
152        mState.mChangingConfigurations = super.getChangingConfigurations();
153        return mState;
154    }
155
156    final static class ColorState extends ConstantState {
157        int mBaseColor; // base color, independent of setAlpha()
158        int mUseColor;  // basecolor modulated by setAlpha()
159        int mChangingConfigurations;
160
161        ColorState(ColorState state) {
162            if (state != null) {
163                mBaseColor = state.mBaseColor;
164                mUseColor = state.mUseColor;
165            }
166        }
167
168        @Override
169        public Drawable newDrawable() {
170            return new ColorDrawable(this);
171        }
172
173        @Override
174        public Drawable newDrawable(Resources res) {
175            return new ColorDrawable(this);
176        }
177
178        @Override
179        public int getChangingConfigurations() {
180            return mChangingConfigurations;
181        }
182    }
183}
184