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