ScaleDrawable.java revision b1af7f3d4be6cd6bc6e1e805f22c209e19bf928f
1/* 2 * Copyright (C) 2006 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 org.xmlpull.v1.XmlPullParser; 20import org.xmlpull.v1.XmlPullParserException; 21 22import android.content.res.Resources; 23import android.content.res.TypedArray; 24import android.graphics.*; 25import android.view.Gravity; 26import android.util.AttributeSet; 27 28import java.io.IOException; 29 30/** 31 * A Drawable that changes the size of another Drawable based on its current 32 * level value. You can control how much the child Drawable changes in width 33 * and height based on the level, as well as a gravity to control where it is 34 * placed in its overall container. Most often used to implement things like 35 * progress bars. 36 * 37 * <p>It can be defined in an XML file with the <code><scale></code> element. For more 38 * information, see the guide to <a 39 * href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.</p> 40 * 41 * @attr ref android.R.styleable#ScaleDrawable_scaleWidth 42 * @attr ref android.R.styleable#ScaleDrawable_scaleHeight 43 * @attr ref android.R.styleable#ScaleDrawable_scaleGravity 44 * @attr ref android.R.styleable#ScaleDrawable_drawable 45 */ 46public class ScaleDrawable extends Drawable implements Drawable.Callback { 47 private ScaleState mScaleState; 48 private boolean mMutated; 49 private final Rect mTmpRect = new Rect(); 50 51 ScaleDrawable() { 52 this(null, null); 53 } 54 55 public ScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight) { 56 this(null, null); 57 58 mScaleState.mDrawable = drawable; 59 mScaleState.mGravity = gravity; 60 mScaleState.mScaleWidth = scaleWidth; 61 mScaleState.mScaleHeight = scaleHeight; 62 63 if (drawable != null) { 64 drawable.setCallback(this); 65 } 66 } 67 68 /** 69 * Returns the drawable scaled by this ScaleDrawable. 70 */ 71 public Drawable getDrawable() { 72 return mScaleState.mDrawable; 73 } 74 75 private static float getPercent(TypedArray a, int name) { 76 String s = a.getString(name); 77 if (s != null) { 78 if (s.endsWith("%")) { 79 String f = s.substring(0, s.length() - 1); 80 return Float.parseFloat(f) / 100.0f; 81 } 82 } 83 return -1; 84 } 85 86 @Override 87 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) 88 throws XmlPullParserException, IOException { 89 super.inflate(r, parser, attrs); 90 91 int type; 92 93 TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ScaleDrawable); 94 95 float sw = getPercent(a, com.android.internal.R.styleable.ScaleDrawable_scaleWidth); 96 float sh = getPercent(a, com.android.internal.R.styleable.ScaleDrawable_scaleHeight); 97 int g = a.getInt(com.android.internal.R.styleable.ScaleDrawable_scaleGravity, Gravity.LEFT); 98 boolean min = a.getBoolean( 99 com.android.internal.R.styleable.ScaleDrawable_useIntrinsicSizeAsMinimum, false); 100 Drawable dr = a.getDrawable(com.android.internal.R.styleable.ScaleDrawable_drawable); 101 102 a.recycle(); 103 104 final int outerDepth = parser.getDepth(); 105 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 106 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 107 if (type != XmlPullParser.START_TAG) { 108 continue; 109 } 110 dr = Drawable.createFromXmlInner(r, parser, attrs); 111 } 112 113 if (dr == null) { 114 throw new IllegalArgumentException("No drawable specified for <scale>"); 115 } 116 117 mScaleState.mDrawable = dr; 118 mScaleState.mScaleWidth = sw; 119 mScaleState.mScaleHeight = sh; 120 mScaleState.mGravity = g; 121 mScaleState.mUseIntrinsicSizeAsMin = min; 122 if (dr != null) { 123 dr.setCallback(this); 124 } 125 } 126 127 // overrides from Drawable.Callback 128 129 public void invalidateDrawable(Drawable who) { 130 if (getCallback() != null) { 131 getCallback().invalidateDrawable(this); 132 } 133 } 134 135 public void scheduleDrawable(Drawable who, Runnable what, long when) { 136 if (getCallback() != null) { 137 getCallback().scheduleDrawable(this, what, when); 138 } 139 } 140 141 public void unscheduleDrawable(Drawable who, Runnable what) { 142 if (getCallback() != null) { 143 getCallback().unscheduleDrawable(this, what); 144 } 145 } 146 147 // overrides from Drawable 148 149 @Override 150 public void draw(Canvas canvas) { 151 if (mScaleState.mDrawable.getLevel() != 0) 152 mScaleState.mDrawable.draw(canvas); 153 } 154 155 @Override 156 public int getChangingConfigurations() { 157 return super.getChangingConfigurations() 158 | mScaleState.mChangingConfigurations 159 | mScaleState.mDrawable.getChangingConfigurations(); 160 } 161 162 @Override 163 public boolean getPadding(Rect padding) { 164 // XXX need to adjust padding! 165 return mScaleState.mDrawable.getPadding(padding); 166 } 167 168 @Override 169 public boolean setVisible(boolean visible, boolean restart) { 170 mScaleState.mDrawable.setVisible(visible, restart); 171 return super.setVisible(visible, restart); 172 } 173 174 @Override 175 public void setAlpha(int alpha) { 176 mScaleState.mDrawable.setAlpha(alpha); 177 } 178 179 @Override 180 public int getAlpha() { 181 return mScaleState.mDrawable.getAlpha(); 182 } 183 184 @Override 185 public void setColorFilter(ColorFilter cf) { 186 mScaleState.mDrawable.setColorFilter(cf); 187 } 188 189 @Override 190 public int getOpacity() { 191 return mScaleState.mDrawable.getOpacity(); 192 } 193 194 @Override 195 public boolean isStateful() { 196 return mScaleState.mDrawable.isStateful(); 197 } 198 199 @Override 200 protected boolean onStateChange(int[] state) { 201 boolean changed = mScaleState.mDrawable.setState(state); 202 onBoundsChange(getBounds()); 203 return changed; 204 } 205 206 @Override 207 protected boolean onLevelChange(int level) { 208 mScaleState.mDrawable.setLevel(level); 209 onBoundsChange(getBounds()); 210 invalidateSelf(); 211 return true; 212 } 213 214 @Override 215 protected void onBoundsChange(Rect bounds) { 216 final Rect r = mTmpRect; 217 final boolean min = mScaleState.mUseIntrinsicSizeAsMin; 218 int level = getLevel(); 219 int w = bounds.width(); 220 if (mScaleState.mScaleWidth > 0) { 221 final int iw = min ? mScaleState.mDrawable.getIntrinsicWidth() : 0; 222 w -= (int) ((w - iw) * (10000 - level) * mScaleState.mScaleWidth / 10000); 223 } 224 int h = bounds.height(); 225 if (mScaleState.mScaleHeight > 0) { 226 final int ih = min ? mScaleState.mDrawable.getIntrinsicHeight() : 0; 227 h -= (int) ((h - ih) * (10000 - level) * mScaleState.mScaleHeight / 10000); 228 } 229 final int layoutDirection = getLayoutDirection(); 230 Gravity.apply(mScaleState.mGravity, w, h, bounds, r, layoutDirection); 231 232 if (w > 0 && h > 0) { 233 mScaleState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom); 234 } 235 } 236 237 @Override 238 public int getIntrinsicWidth() { 239 return mScaleState.mDrawable.getIntrinsicWidth(); 240 } 241 242 @Override 243 public int getIntrinsicHeight() { 244 return mScaleState.mDrawable.getIntrinsicHeight(); 245 } 246 247 @Override 248 public ConstantState getConstantState() { 249 if (mScaleState.canConstantState()) { 250 mScaleState.mChangingConfigurations = getChangingConfigurations(); 251 return mScaleState; 252 } 253 return null; 254 } 255 256 @Override 257 public Drawable mutate() { 258 if (!mMutated && super.mutate() == this) { 259 mScaleState.mDrawable.mutate(); 260 mMutated = true; 261 } 262 return this; 263 } 264 265 final static class ScaleState extends ConstantState { 266 Drawable mDrawable; 267 int mChangingConfigurations; 268 float mScaleWidth; 269 float mScaleHeight; 270 int mGravity; 271 boolean mUseIntrinsicSizeAsMin; 272 273 private boolean mCheckedConstantState; 274 private boolean mCanConstantState; 275 276 ScaleState(ScaleState orig, ScaleDrawable owner, Resources res) { 277 if (orig != null) { 278 if (res != null) { 279 mDrawable = orig.mDrawable.getConstantState().newDrawable(res); 280 } else { 281 mDrawable = orig.mDrawable.getConstantState().newDrawable(); 282 } 283 mDrawable.setCallback(owner); 284 mScaleWidth = orig.mScaleWidth; 285 mScaleHeight = orig.mScaleHeight; 286 mGravity = orig.mGravity; 287 mUseIntrinsicSizeAsMin = orig.mUseIntrinsicSizeAsMin; 288 mCheckedConstantState = mCanConstantState = true; 289 } 290 } 291 292 @Override 293 public Drawable newDrawable() { 294 return new ScaleDrawable(this, null); 295 } 296 297 @Override 298 public Drawable newDrawable(Resources res) { 299 return new ScaleDrawable(this, res); 300 } 301 302 @Override 303 public int getChangingConfigurations() { 304 return mChangingConfigurations; 305 } 306 307 boolean canConstantState() { 308 if (!mCheckedConstantState) { 309 mCanConstantState = mDrawable.getConstantState() != null; 310 mCheckedConstantState = true; 311 } 312 313 return mCanConstantState; 314 } 315 } 316 317 private ScaleDrawable(ScaleState state, Resources res) { 318 mScaleState = new ScaleState(state, this, res); 319 } 320} 321 322