ColorDrawable.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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 ColorDrawable is a specialized drawable that fills the Canvas with a specified color, 30 * and with respect to the clip region. Note that a ColorDrawable ignores the ColorFilter. 31 * It also ignores the Bounds, meaning it will draw everywhere in the current clip, 32 * even if setBounds(...) was called with a smaller area. 33 */ 34public class ColorDrawable extends Drawable { 35 private ColorState mState; 36 37 /** 38 * Creates a new black ColorDrawable. 39 */ 40 public ColorDrawable() { 41 this(null); 42 } 43 44 /** 45 * Creates a new ColorDrawable with the specified color. 46 * 47 * @param color The color to draw. 48 */ 49 public ColorDrawable(int color) { 50 this(null); 51 mState.mBaseColor = mState.mUseColor = color; 52 } 53 54 private ColorDrawable(ColorState state) { 55 mState = new ColorState(state); 56 } 57 58 @Override 59 public int getChangingConfigurations() { 60 return super.getChangingConfigurations() | mState.mChangingConfigurations; 61 } 62 63 @Override 64 public void draw(Canvas canvas) { 65 canvas.drawColor(mState.mUseColor); 66 } 67 68 /** 69 * Returns the alpha value of this drawable's color. 70 * 71 * @return A value between 0 and 255. 72 */ 73 public int getAlpha() { 74 return mState.mUseColor >>> 24; 75 } 76 77 /** 78 * Sets the color's alpha value. 79 * 80 * @param alpha The alpha value to set, between 0 and 255. 81 */ 82 public void setAlpha(int alpha) { 83 alpha += alpha >> 7; // make it 0..256 84 int baseAlpha = mState.mBaseColor >>> 24; 85 int useAlpha = baseAlpha * alpha >> 8; 86 mState.mUseColor = (mState.mBaseColor << 8 >>> 8) | (useAlpha << 24); 87 } 88 89 /** 90 * Setting a color filter on a ColorDrawable has no effect. 91 * 92 * @param colorFilter Ignore. 93 */ 94 public void setColorFilter(ColorFilter colorFilter) { 95 } 96 97 public int getOpacity() { 98 switch (mState.mUseColor >>> 24) { 99 case 255: 100 return PixelFormat.OPAQUE; 101 case 0: 102 return PixelFormat.TRANSPARENT; 103 } 104 return PixelFormat.TRANSLUCENT; 105 } 106 107 @Override 108 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) 109 throws XmlPullParserException, IOException { 110 super.inflate(r, parser, attrs); 111 112 TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ColorDrawable); 113 114 int color = mState.mBaseColor; 115 color = a.getColor(com.android.internal.R.styleable.ColorDrawable_color, color); 116 mState.mBaseColor = mState.mUseColor = color; 117 118 a.recycle(); 119 } 120 121 @Override 122 public ConstantState getConstantState() { 123 mState.mChangingConfigurations = super.getChangingConfigurations(); 124 return mState; 125 } 126 127 final static class ColorState extends ConstantState { 128 int mBaseColor; // initial color. never changes 129 int mUseColor; // basecolor modulated by setAlpha() 130 int mChangingConfigurations; 131 132 ColorState(ColorState state) { 133 if (state != null) { 134 mBaseColor = state.mBaseColor; 135 mUseColor = state.mUseColor; 136 } 137 } 138 139 @Override 140 public Drawable newDrawable() { 141 return new ColorDrawable(this); 142 } 143 144 @Override 145 public int getChangingConfigurations() { 146 return mChangingConfigurations; 147 } 148 } 149} 150