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