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