InsetDrawable.java revision 84956d53f1e86b8c000f8994ea9040b71a30e321
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.graphics.*; 25import android.util.AttributeSet; 26import android.util.Log; 27 28import 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 */ 46public 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 @Override 207 public int getOpacity() { 208 return mInsetState.mDrawable.getOpacity(); 209 } 210 211 @Override 212 public boolean isStateful() { 213 return mInsetState.mDrawable.isStateful(); 214 } 215 216 @Override 217 protected boolean onStateChange(int[] state) { 218 boolean changed = mInsetState.mDrawable.setState(state); 219 onBoundsChange(getBounds()); 220 return changed; 221 } 222 223 @Override 224 protected void onBoundsChange(Rect bounds) { 225 final Rect r = mTmpRect; 226 r.set(bounds); 227 228 r.left += mInsetState.mInsetLeft; 229 r.top += mInsetState.mInsetTop; 230 r.right -= mInsetState.mInsetRight; 231 r.bottom -= mInsetState.mInsetBottom; 232 233 mInsetState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom); 234 } 235 236 @Override 237 public int getIntrinsicWidth() { 238 return mInsetState.mDrawable.getIntrinsicWidth(); 239 } 240 241 @Override 242 public int getIntrinsicHeight() { 243 return mInsetState.mDrawable.getIntrinsicHeight(); 244 } 245 246 @Override 247 public ConstantState getConstantState() { 248 if (mInsetState.canConstantState()) { 249 mInsetState.mChangingConfigurations = getChangingConfigurations(); 250 return mInsetState; 251 } 252 return null; 253 } 254 255 @Override 256 public Drawable mutate() { 257 if (!mMutated && super.mutate() == this) { 258 mInsetState.mDrawable.mutate(); 259 mMutated = true; 260 } 261 return this; 262 } 263 264 /** 265 * Returns the drawable wrapped by this InsetDrawable. May be null. 266 */ 267 public Drawable getDrawable() { 268 return mInsetState.mDrawable; 269 } 270 271 final static class InsetState extends ConstantState { 272 Drawable mDrawable; 273 int mChangingConfigurations; 274 275 int mInsetLeft; 276 int mInsetTop; 277 int mInsetRight; 278 int mInsetBottom; 279 280 boolean mCheckedConstantState; 281 boolean mCanConstantState; 282 283 InsetState(InsetState orig, InsetDrawable owner, Resources res) { 284 if (orig != null) { 285 if (res != null) { 286 mDrawable = orig.mDrawable.getConstantState().newDrawable(res); 287 } else { 288 mDrawable = orig.mDrawable.getConstantState().newDrawable(); 289 } 290 mDrawable.setCallback(owner); 291 mDrawable.setLayoutDirection(orig.mDrawable.getLayoutDirection()); 292 mInsetLeft = orig.mInsetLeft; 293 mInsetTop = orig.mInsetTop; 294 mInsetRight = orig.mInsetRight; 295 mInsetBottom = orig.mInsetBottom; 296 mCheckedConstantState = mCanConstantState = true; 297 } 298 } 299 300 @Override 301 public Drawable newDrawable() { 302 return new InsetDrawable(this, null); 303 } 304 305 @Override 306 public Drawable newDrawable(Resources res) { 307 return new InsetDrawable(this, res); 308 } 309 310 @Override 311 public int getChangingConfigurations() { 312 return mChangingConfigurations; 313 } 314 315 boolean canConstantState() { 316 if (!mCheckedConstantState) { 317 mCanConstantState = mDrawable.getConstantState() != null; 318 mCheckedConstantState = true; 319 } 320 321 return mCanConstantState; 322 } 323 } 324 325 private InsetDrawable(InsetState state, Resources res) { 326 mInsetState = new InsetState(state, this, res); 327 } 328} 329 330