ColorDrawable.java revision 70d4ba15b1f0c1133c5aabc86de828b41e482fff
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        mState.mBaseColor = mState.mUseColor = color;
91    }
92
93    /**
94     * Returns the alpha value of this drawable's color.
95     *
96     * @return A value between 0 and 255.
97     */
98    public int getAlpha() {
99        return mState.mUseColor >>> 24;
100    }
101
102    /**
103     * Sets the color's alpha value.
104     *
105     * @param alpha The alpha value to set, between 0 and 255.
106     */
107    public void setAlpha(int alpha) {
108        alpha += alpha >> 7;   // make it 0..256
109        int baseAlpha = mState.mBaseColor >>> 24;
110        int useAlpha = baseAlpha * alpha >> 8;
111        mState.mUseColor = (mState.mBaseColor << 8 >>> 8) | (useAlpha << 24);
112    }
113
114    /**
115     * Setting a color filter on a ColorDrawable has no effect.
116     *
117     * @param colorFilter Ignore.
118     */
119    public void setColorFilter(ColorFilter colorFilter) {
120    }
121
122    public int getOpacity() {
123        switch (mState.mUseColor >>> 24) {
124            case 255:
125                return PixelFormat.OPAQUE;
126            case 0:
127                return PixelFormat.TRANSPARENT;
128        }
129        return PixelFormat.TRANSLUCENT;
130    }
131
132    @Override
133    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
134            throws XmlPullParserException, IOException {
135        super.inflate(r, parser, attrs);
136
137        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ColorDrawable);
138
139        int color = mState.mBaseColor;
140        color = a.getColor(com.android.internal.R.styleable.ColorDrawable_color, color);
141        mState.mBaseColor = mState.mUseColor = color;
142
143        a.recycle();
144    }
145
146    @Override
147    public ConstantState getConstantState() {
148        mState.mChangingConfigurations = super.getChangingConfigurations();
149        return mState;
150    }
151
152    final static class ColorState extends ConstantState {
153        int mBaseColor; // base color, independent of setAlpha()
154        int mUseColor;  // basecolor modulated by setAlpha()
155        int mChangingConfigurations;
156
157        ColorState(ColorState state) {
158            if (state != null) {
159                mBaseColor = state.mBaseColor;
160                mUseColor = state.mUseColor;
161            }
162        }
163
164        @Override
165        public Drawable newDrawable() {
166            return new ColorDrawable(this);
167        }
168
169        @Override
170        public Drawable newDrawable(Resources res) {
171            return new ColorDrawable(this);
172        }
173
174        @Override
175        public int getChangingConfigurations() {
176            return mChangingConfigurations;
177        }
178    }
179}
180