ColorDrawable.java revision 41bff38d3060dbcb55133cedaf5d962c3082efc2
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 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 int oldUseColor = mState.mUseColor; 115 mState.mUseColor = (mState.mBaseColor << 8 >>> 8) | (useAlpha << 24); 116 if (oldUseColor != mState.mUseColor) { 117 invalidateSelf(); 118 } 119 } 120 121 /** 122 * Setting a color filter on a ColorDrawable has no effect. 123 * 124 * @param colorFilter Ignore. 125 */ 126 public void setColorFilter(ColorFilter colorFilter) { 127 } 128 129 public int getOpacity() { 130 switch (mState.mUseColor >>> 24) { 131 case 255: 132 return PixelFormat.OPAQUE; 133 case 0: 134 return PixelFormat.TRANSPARENT; 135 } 136 return PixelFormat.TRANSLUCENT; 137 } 138 139 @Override 140 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) 141 throws XmlPullParserException, IOException { 142 super.inflate(r, parser, attrs); 143 144 TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ColorDrawable); 145 146 int color = mState.mBaseColor; 147 color = a.getColor(com.android.internal.R.styleable.ColorDrawable_color, color); 148 mState.mBaseColor = mState.mUseColor = color; 149 150 a.recycle(); 151 } 152 153 @Override 154 public ConstantState getConstantState() { 155 mState.mChangingConfigurations = getChangingConfigurations(); 156 return mState; 157 } 158 159 final static class ColorState extends ConstantState { 160 int mBaseColor; // base color, independent of setAlpha() 161 int mUseColor; // basecolor modulated by setAlpha() 162 int mChangingConfigurations; 163 164 ColorState(ColorState state) { 165 if (state != null) { 166 mBaseColor = state.mBaseColor; 167 mUseColor = state.mUseColor; 168 } 169 } 170 171 @Override 172 public Drawable newDrawable() { 173 return new ColorDrawable(this); 174 } 175 176 @Override 177 public Drawable newDrawable(Resources res) { 178 return new ColorDrawable(this); 179 } 180 181 @Override 182 public int getChangingConfigurations() { 183 return mChangingConfigurations; 184 } 185 } 186} 187