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 void setColorFilter(ColorFilter cf) { 181 mScaleState.mDrawable.setColorFilter(cf); 182 } 183 184 @Override 185 public int getOpacity() { 186 return mScaleState.mDrawable.getOpacity(); 187 } 188 189 @Override 190 public boolean isStateful() { 191 return mScaleState.mDrawable.isStateful(); 192 } 193 194 @Override 195 protected boolean onStateChange(int[] state) { 196 boolean changed = mScaleState.mDrawable.setState(state); 197 onBoundsChange(getBounds()); 198 return changed; 199 } 200 201 @Override 202 protected boolean onLevelChange(int level) { 203 mScaleState.mDrawable.setLevel(level); 204 onBoundsChange(getBounds()); 205 invalidateSelf(); 206 return true; 207 } 208 209 @Override 210 protected void onBoundsChange(Rect bounds) { 211 final Rect r = mTmpRect; 212 final boolean min = mScaleState.mUseIntrinsicSizeAsMin; 213 int level = getLevel(); 214 int w = bounds.width(); 215 if (mScaleState.mScaleWidth > 0) { 216 final int iw = min ? mScaleState.mDrawable.getIntrinsicWidth() : 0; 217 w -= (int) ((w - iw) * (10000 - level) * mScaleState.mScaleWidth / 10000); 218 } 219 int h = bounds.height(); 220 if (mScaleState.mScaleHeight > 0) { 221 final int ih = min ? mScaleState.mDrawable.getIntrinsicHeight() : 0; 222 h -= (int) ((h - ih) * (10000 - level) * mScaleState.mScaleHeight / 10000); 223 } 224 final int layoutDirection = getLayoutDirection(); 225 Gravity.apply(mScaleState.mGravity, w, h, bounds, r, layoutDirection); 226 227 if (w > 0 && h > 0) { 228 mScaleState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom); 229 } 230 } 231 232 @Override 233 public int getIntrinsicWidth() { 234 return mScaleState.mDrawable.getIntrinsicWidth(); 235 } 236 237 @Override 238 public int getIntrinsicHeight() { 239 return mScaleState.mDrawable.getIntrinsicHeight(); 240 } 241 242 @Override 243 public ConstantState getConstantState() { 244 if (mScaleState.canConstantState()) { 245 mScaleState.mChangingConfigurations = getChangingConfigurations(); 246 return mScaleState; 247 } 248 return null; 249 } 250 251 @Override 252 public Drawable mutate() { 253 if (!mMutated && super.mutate() == this) { 254 mScaleState.mDrawable.mutate(); 255 mMutated = true; 256 } 257 return this; 258 } 259 260 final static class ScaleState extends ConstantState { 261 Drawable mDrawable; 262 int mChangingConfigurations; 263 float mScaleWidth; 264 float mScaleHeight; 265 int mGravity; 266 boolean mUseIntrinsicSizeAsMin; 267 268 private boolean mCheckedConstantState; 269 private boolean mCanConstantState; 270 271 ScaleState(ScaleState orig, ScaleDrawable owner, Resources res) { 272 if (orig != null) { 273 if (res != null) { 274 mDrawable = orig.mDrawable.getConstantState().newDrawable(res); 275 } else { 276 mDrawable = orig.mDrawable.getConstantState().newDrawable(); 277 } 278 mDrawable.setCallback(owner); 279 mScaleWidth = orig.mScaleWidth; 280 mScaleHeight = orig.mScaleHeight; 281 mGravity = orig.mGravity; 282 mUseIntrinsicSizeAsMin = orig.mUseIntrinsicSizeAsMin; 283 mCheckedConstantState = mCanConstantState = true; 284 } 285 } 286 287 @Override 288 public Drawable newDrawable() { 289 return new ScaleDrawable(this, null); 290 } 291 292 @Override 293 public Drawable newDrawable(Resources res) { 294 return new ScaleDrawable(this, res); 295 } 296 297 @Override 298 public int getChangingConfigurations() { 299 return mChangingConfigurations; 300 } 301 302 boolean canConstantState() { 303 if (!mCheckedConstantState) { 304 mCanConstantState = mDrawable.getConstantState() != null; 305 mCheckedConstantState = true; 306 } 307 308 return mCanConstantState; 309 } 310 } 311 312 private ScaleDrawable(ScaleState state, Resources res) { 313 mScaleState = new ScaleState(state, this, res); 314 } 315} 316 317