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