Home | History | Annotate | Download | only in drawable
      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 
     17 package android.graphics.drawable;
     18 
     19 import android.graphics.*;
     20 import android.content.res.Resources;
     21 import android.content.res.TypedArray;
     22 import android.util.AttributeSet;
     23 import org.xmlpull.v1.XmlPullParser;
     24 import org.xmlpull.v1.XmlPullParserException;
     25 
     26 import java.io.IOException;
     27 
     28 /**
     29  * A specialized Drawable that fills the Canvas with a specified color,
     30  * 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  *
     34  * <p>It can be defined in an XML file with the <code>&lt;color></code> element.</p>
     35  *
     36  * @attr ref android.R.styleable#ColorDrawable_color
     37  */
     38 public class ColorDrawable extends Drawable {
     39     private ColorState mState;
     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         mState.mBaseColor = mState.mUseColor = 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     @Override
     68     public void draw(Canvas canvas) {
     69         canvas.drawColor(mState.mUseColor);
     70     }
     71 
     72     /**
     73      * Returns the alpha value of this drawable's color.
     74      *
     75      * @return A value between 0 and 255.
     76      */
     77     public int getAlpha() {
     78         return mState.mUseColor >>> 24;
     79     }
     80 
     81     /**
     82      * Sets the color's alpha value.
     83      *
     84      * @param alpha The alpha value to set, between 0 and 255.
     85      */
     86     public void setAlpha(int alpha) {
     87         alpha += alpha >> 7;   // make it 0..256
     88         int baseAlpha = mState.mBaseColor >>> 24;
     89         int useAlpha = baseAlpha * alpha >> 8;
     90         mState.mUseColor = (mState.mBaseColor << 8 >>> 8) | (useAlpha << 24);
     91     }
     92 
     93     /**
     94      * Setting a color filter on a ColorDrawable has no effect.
     95      *
     96      * @param colorFilter Ignore.
     97      */
     98     public void setColorFilter(ColorFilter colorFilter) {
     99     }
    100 
    101     public int getOpacity() {
    102         switch (mState.mUseColor >>> 24) {
    103             case 255:
    104                 return PixelFormat.OPAQUE;
    105             case 0:
    106                 return PixelFormat.TRANSPARENT;
    107         }
    108         return PixelFormat.TRANSLUCENT;
    109     }
    110 
    111     @Override
    112     public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
    113             throws XmlPullParserException, IOException {
    114         super.inflate(r, parser, attrs);
    115 
    116         TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ColorDrawable);
    117 
    118         int color = mState.mBaseColor;
    119         color = a.getColor(com.android.internal.R.styleable.ColorDrawable_color, color);
    120         mState.mBaseColor = mState.mUseColor = color;
    121 
    122         a.recycle();
    123     }
    124 
    125     @Override
    126     public ConstantState getConstantState() {
    127         mState.mChangingConfigurations = super.getChangingConfigurations();
    128         return mState;
    129     }
    130 
    131     final static class ColorState extends ConstantState {
    132         int mBaseColor; // initial color. never changes
    133         int mUseColor;  // basecolor modulated by setAlpha()
    134         int mChangingConfigurations;
    135 
    136         ColorState(ColorState state) {
    137             if (state != null) {
    138                 mBaseColor = state.mBaseColor;
    139                 mUseColor = state.mUseColor;
    140             }
    141         }
    142 
    143         @Override
    144         public Drawable newDrawable() {
    145             return new ColorDrawable(this);
    146         }
    147 
    148         @Override
    149         public Drawable newDrawable(Resources res) {
    150             return new ColorDrawable(this);
    151         }
    152 
    153         @Override
    154         public int getChangingConfigurations() {
    155             return mChangingConfigurations;
    156         }
    157     }
    158 }
    159