InsetDrawable.java revision cda212d79d449468384cc7744878b8c99984059c
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 org.xmlpull.v1.XmlPullParser; 20import org.xmlpull.v1.XmlPullParserException; 21 22import android.content.res.Resources; 23import android.content.res.TypedArray; 24import android.content.res.Resources.Theme; 25import android.graphics.*; 26import android.util.AttributeSet; 27import android.util.Log; 28 29import java.io.IOException; 30 31/** 32 * A Drawable that insets another Drawable by a specified distance. 33 * This is used when a View needs a background that is smaller than 34 * the View's actual bounds. 35 * 36 * <p>It can be defined in an XML file with the <code><inset></code> element. For more 37 * information, see the guide to <a 38 * href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.</p> 39 * 40 * @attr ref android.R.styleable#InsetDrawable_visible 41 * @attr ref android.R.styleable#InsetDrawable_drawable 42 * @attr ref android.R.styleable#InsetDrawable_insetLeft 43 * @attr ref android.R.styleable#InsetDrawable_insetRight 44 * @attr ref android.R.styleable#InsetDrawable_insetTop 45 * @attr ref android.R.styleable#InsetDrawable_insetBottom 46 */ 47public class InsetDrawable extends Drawable implements Drawable.Callback 48{ 49 // Most of this is copied from ScaleDrawable. 50 private InsetState mInsetState; 51 private final Rect mTmpRect = new Rect(); 52 private boolean mMutated; 53 54 /*package*/ InsetDrawable() { 55 this(null, null); 56 } 57 58 public InsetDrawable(Drawable drawable, int inset) { 59 this(drawable, inset, inset, inset, inset); 60 } 61 62 public InsetDrawable(Drawable drawable, int insetLeft, int insetTop, 63 int insetRight, int insetBottom) { 64 this(null, null); 65 66 mInsetState.mDrawable = drawable; 67 mInsetState.mInsetLeft = insetLeft; 68 mInsetState.mInsetTop = insetTop; 69 mInsetState.mInsetRight = insetRight; 70 mInsetState.mInsetBottom = insetBottom; 71 72 if (drawable != null) { 73 drawable.setCallback(this); 74 } 75 } 76 77 @Override 78 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) 79 throws XmlPullParserException, IOException { 80 int type; 81 82 TypedArray a = r.obtainAttributes(attrs, 83 com.android.internal.R.styleable.InsetDrawable); 84 85 super.inflateWithAttributes(r, parser, a, 86 com.android.internal.R.styleable.InsetDrawable_visible); 87 88 int drawableRes = a.getResourceId(com.android.internal.R.styleable. 89 InsetDrawable_drawable, 0); 90 91 int inLeft = a.getDimensionPixelOffset(com.android.internal.R.styleable. 92 InsetDrawable_insetLeft, 0); 93 int inTop = a.getDimensionPixelOffset(com.android.internal.R.styleable. 94 InsetDrawable_insetTop, 0); 95 int inRight = a.getDimensionPixelOffset(com.android.internal.R.styleable. 96 InsetDrawable_insetRight, 0); 97 int inBottom = a.getDimensionPixelOffset(com.android.internal.R.styleable. 98 InsetDrawable_insetBottom, 0); 99 100 a.recycle(); 101 102 Drawable dr; 103 if (drawableRes != 0) { 104 dr = r.getDrawable(drawableRes); 105 } else { 106 while ((type=parser.next()) == XmlPullParser.TEXT) { 107 } 108 if (type != XmlPullParser.START_TAG) { 109 throw new XmlPullParserException( 110 parser.getPositionDescription() 111 + ": <inset> tag requires a 'drawable' attribute or " 112 + "child tag defining a drawable"); 113 } 114 dr = Drawable.createFromXmlInner(r, parser, attrs, theme); 115 } 116 117 if (dr == null) { 118 Log.w("drawable", "No drawable specified for <inset>"); 119 } 120 121 mInsetState.mDrawable = dr; 122 mInsetState.mInsetLeft = inLeft; 123 mInsetState.mInsetRight = inRight; 124 mInsetState.mInsetTop = inTop; 125 mInsetState.mInsetBottom = inBottom; 126 127 if (dr != null) { 128 dr.setCallback(this); 129 } 130 } 131 132 // overrides from Drawable.Callback 133 134 @Override 135 public void invalidateDrawable(Drawable who) { 136 final Callback callback = getCallback(); 137 if (callback != null) { 138 callback.invalidateDrawable(this); 139 } 140 } 141 142 @Override 143 public void scheduleDrawable(Drawable who, Runnable what, long when) { 144 final Callback callback = getCallback(); 145 if (callback != null) { 146 callback.scheduleDrawable(this, what, when); 147 } 148 } 149 150 @Override 151 public void unscheduleDrawable(Drawable who, Runnable what) { 152 final Callback callback = getCallback(); 153 if (callback != null) { 154 callback.unscheduleDrawable(this, what); 155 } 156 } 157 158 // overrides from Drawable 159 160 @Override 161 public void draw(Canvas canvas) { 162 mInsetState.mDrawable.draw(canvas); 163 } 164 165 @Override 166 public int getChangingConfigurations() { 167 return super.getChangingConfigurations() 168 | mInsetState.mChangingConfigurations 169 | mInsetState.mDrawable.getChangingConfigurations(); 170 } 171 172 @Override 173 public boolean getPadding(Rect padding) { 174 boolean pad = mInsetState.mDrawable.getPadding(padding); 175 176 padding.left += mInsetState.mInsetLeft; 177 padding.right += mInsetState.mInsetRight; 178 padding.top += mInsetState.mInsetTop; 179 padding.bottom += mInsetState.mInsetBottom; 180 181 if (pad || (mInsetState.mInsetLeft | mInsetState.mInsetRight | 182 mInsetState.mInsetTop | mInsetState.mInsetBottom) != 0) { 183 return true; 184 } else { 185 return false; 186 } 187 } 188 189 @Override 190 public void setHotspot(float x, float y) { 191 mInsetState.mDrawable.setHotspot(x, y); 192 } 193 194 @Override 195 public void setHotspotBounds(int left, int top, int right, int bottom) { 196 mInsetState.mDrawable.setHotspotBounds(left, top, right, bottom); 197 } 198 199 @Override 200 public boolean setVisible(boolean visible, boolean restart) { 201 mInsetState.mDrawable.setVisible(visible, restart); 202 return super.setVisible(visible, restart); 203 } 204 205 @Override 206 public void setAlpha(int alpha) { 207 mInsetState.mDrawable.setAlpha(alpha); 208 } 209 210 @Override 211 public int getAlpha() { 212 return mInsetState.mDrawable.getAlpha(); 213 } 214 215 @Override 216 public void setColorFilter(ColorFilter cf) { 217 mInsetState.mDrawable.setColorFilter(cf); 218 } 219 220 /** {@hide} */ 221 @Override 222 public void setLayoutDirection(int layoutDirection) { 223 mInsetState.mDrawable.setLayoutDirection(layoutDirection); 224 } 225 226 @Override 227 public int getOpacity() { 228 return mInsetState.mDrawable.getOpacity(); 229 } 230 231 @Override 232 public boolean isStateful() { 233 return mInsetState.mDrawable.isStateful(); 234 } 235 236 @Override 237 protected boolean onStateChange(int[] state) { 238 boolean changed = mInsetState.mDrawable.setState(state); 239 onBoundsChange(getBounds()); 240 return changed; 241 } 242 243 @Override 244 protected void onBoundsChange(Rect bounds) { 245 final Rect r = mTmpRect; 246 r.set(bounds); 247 248 r.left += mInsetState.mInsetLeft; 249 r.top += mInsetState.mInsetTop; 250 r.right -= mInsetState.mInsetRight; 251 r.bottom -= mInsetState.mInsetBottom; 252 253 mInsetState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom); 254 } 255 256 @Override 257 public int getIntrinsicWidth() { 258 return mInsetState.mDrawable.getIntrinsicWidth(); 259 } 260 261 @Override 262 public int getIntrinsicHeight() { 263 return mInsetState.mDrawable.getIntrinsicHeight(); 264 } 265 266 @Override 267 public ConstantState getConstantState() { 268 if (mInsetState.canConstantState()) { 269 mInsetState.mChangingConfigurations = getChangingConfigurations(); 270 return mInsetState; 271 } 272 return null; 273 } 274 275 @Override 276 public Drawable mutate() { 277 if (!mMutated && super.mutate() == this) { 278 mInsetState.mDrawable.mutate(); 279 mMutated = true; 280 } 281 return this; 282 } 283 284 /** 285 * Returns the drawable wrapped by this InsetDrawable. May be null. 286 */ 287 public Drawable getDrawable() { 288 return mInsetState.mDrawable; 289 } 290 291 final static class InsetState extends ConstantState { 292 Drawable mDrawable; 293 int mChangingConfigurations; 294 295 int mInsetLeft; 296 int mInsetTop; 297 int mInsetRight; 298 int mInsetBottom; 299 300 boolean mCheckedConstantState; 301 boolean mCanConstantState; 302 303 InsetState(InsetState orig, InsetDrawable owner, Resources res) { 304 if (orig != null) { 305 if (res != null) { 306 mDrawable = orig.mDrawable.getConstantState().newDrawable(res); 307 } else { 308 mDrawable = orig.mDrawable.getConstantState().newDrawable(); 309 } 310 mDrawable.setCallback(owner); 311 mDrawable.setLayoutDirection(orig.mDrawable.getLayoutDirection()); 312 mInsetLeft = orig.mInsetLeft; 313 mInsetTop = orig.mInsetTop; 314 mInsetRight = orig.mInsetRight; 315 mInsetBottom = orig.mInsetBottom; 316 mCheckedConstantState = mCanConstantState = true; 317 } 318 } 319 320 @Override 321 public Drawable newDrawable() { 322 return new InsetDrawable(this, null); 323 } 324 325 @Override 326 public Drawable newDrawable(Resources res) { 327 return new InsetDrawable(this, res); 328 } 329 330 @Override 331 public int getChangingConfigurations() { 332 return mChangingConfigurations; 333 } 334 335 boolean canConstantState() { 336 if (!mCheckedConstantState) { 337 mCanConstantState = mDrawable.getConstantState() != null; 338 mCheckedConstantState = true; 339 } 340 341 return mCanConstantState; 342 } 343 } 344 345 private InsetDrawable(InsetState state, Resources res) { 346 mInsetState = new InsetState(state, this, res); 347 } 348} 349 350