blob: 02065779ef91ea3d5803495d10452c829dc62d88 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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.widget;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Siva Velusamy94a6d152015-05-05 15:07:00 -070020import android.annotation.NonNull;
Alan Viverette91174362014-06-17 14:51:45 -070021import android.annotation.Nullable;
Bjorn Bringert0f8555b2009-11-27 12:42:28 +000022import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Alan Viverette91174362014-06-17 14:51:45 -070024import android.content.res.ColorStateList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.res.Resources;
26import android.content.res.TypedArray;
27import android.graphics.Bitmap;
28import android.graphics.Canvas;
29import android.graphics.ColorFilter;
30import android.graphics.Matrix;
Alan Viverettee10dd532013-12-12 19:19:51 -080031import android.graphics.PixelFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.PorterDuff;
33import android.graphics.PorterDuffColorFilter;
Alan Viverettee10dd532013-12-12 19:19:51 -080034import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.RectF;
Adam Powell31049d72013-10-07 12:58:42 -070036import android.graphics.Xfermode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.drawable.BitmapDrawable;
38import android.graphics.drawable.Drawable;
Dan Sandlera22a3802015-05-13 00:12:47 -040039import android.graphics.drawable.Icon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.Uri;
Adam Powell7da4b732012-12-07 15:28:33 -080041import android.os.Build;
Dan Sandlera22a3802015-05-13 00:12:47 -040042import android.os.Handler;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070043import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.util.AttributeSet;
45import android.util.Log;
Jeff Sharkey2b95c242010-02-08 17:40:30 -080046import android.view.RemotableViewMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.view.View;
Joe Onoratofd52b182010-11-10 18:00:52 -080048import android.view.ViewDebug;
Siva Velusamy94a6d152015-05-05 15:07:00 -070049import android.view.ViewHierarchyEncoder;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070050import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.widget.RemoteViews.RemoteView;
52
Alan Viverette91174362014-06-17 14:51:45 -070053import com.android.internal.R;
54
Romain Guy36143942012-12-03 10:10:22 -080055import java.io.IOException;
56import java.io.InputStream;
57
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058/**
59 * Displays an arbitrary image, such as an icon. The ImageView class
60 * can load images from various sources (such as resources or content
61 * providers), takes care of computing its measurement from the image so that
62 * it can be used in any layout manager, and provides various display options
63 * such as scaling and tinting.
64 *
65 * @attr ref android.R.styleable#ImageView_adjustViewBounds
66 * @attr ref android.R.styleable#ImageView_src
67 * @attr ref android.R.styleable#ImageView_maxWidth
68 * @attr ref android.R.styleable#ImageView_maxHeight
69 * @attr ref android.R.styleable#ImageView_tint
70 * @attr ref android.R.styleable#ImageView_scaleType
71 * @attr ref android.R.styleable#ImageView_cropToPadding
72 */
73@RemoteView
74public class ImageView extends View {
Alan Viverette6aa92d12015-08-25 13:19:25 -040075 private static final String LOG_TAG = "ImageView";
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 // settable by the client
78 private Uri mUri;
79 private int mResource = 0;
80 private Matrix mMatrix;
81 private ScaleType mScaleType;
82 private boolean mHaveFrame = false;
83 private boolean mAdjustViewBounds = false;
84 private int mMaxWidth = Integer.MAX_VALUE;
85 private int mMaxHeight = Integer.MAX_VALUE;
86
87 // these are applied to the drawable
Alan Viverette91174362014-06-17 14:51:45 -070088 private ColorFilter mColorFilter = null;
89 private boolean mHasColorFilter = false;
Adam Powell31049d72013-10-07 12:58:42 -070090 private Xfermode mXfermode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 private int mAlpha = 255;
Alan Viverette6aa92d12015-08-25 13:19:25 -040092 private final int mViewAlphaScale = 256;
Jeff Sharkey2b95c242010-02-08 17:40:30 -080093 private boolean mColorMod = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 private Drawable mDrawable = null;
Chris Craik82af13e2015-07-14 13:36:27 -070096 private BitmapDrawable mRecycleableBitmapDrawable = null;
Alan Viveretteb56f5d22014-09-14 15:48:50 -070097 private ColorStateList mDrawableTintList = null;
98 private PorterDuff.Mode mDrawableTintMode = null;
Alan Viverette91174362014-06-17 14:51:45 -070099 private boolean mHasDrawableTint = false;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700100 private boolean mHasDrawableTintMode = false;
Alan Viverette91174362014-06-17 14:51:45 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private int[] mState = null;
103 private boolean mMergeState = false;
104 private int mLevel = 0;
105 private int mDrawableWidth;
106 private int mDrawableHeight;
107 private Matrix mDrawMatrix = null;
108
109 // Avoid allocations...
Alan Viverette6aa92d12015-08-25 13:19:25 -0400110 private final RectF mTempSrc = new RectF();
111 private final RectF mTempDst = new RectF();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 private boolean mCropToPadding;
114
Joe Onoratofd52b182010-11-10 18:00:52 -0800115 private int mBaseline = -1;
116 private boolean mBaselineAlignBottom = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Adam Powell7da4b732012-12-07 15:28:33 -0800118 // AdjustViewBounds behavior will be in compatibility mode for older apps.
119 private boolean mAdjustViewBoundsCompat = false;
120
Alan Viverette270a3422015-08-25 13:25:16 -0400121 /** Whether to pass Resources when creating the source from a stream. */
122 private boolean mUseCorrectStreamDensity;
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 private static final ScaleType[] sScaleTypeArray = {
125 ScaleType.MATRIX,
126 ScaleType.FIT_XY,
127 ScaleType.FIT_START,
128 ScaleType.FIT_CENTER,
129 ScaleType.FIT_END,
130 ScaleType.CENTER,
131 ScaleType.CENTER_CROP,
132 ScaleType.CENTER_INSIDE
133 };
134
135 public ImageView(Context context) {
136 super(context);
137 initImageView();
138 }
Svetoslav Ganov42138042012-03-20 11:51:39 -0700139
Scott Kennedy76c382e2015-02-10 23:15:39 -0800140 public ImageView(Context context, @Nullable AttributeSet attrs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 this(context, attrs, 0);
142 }
Svetoslav Ganov42138042012-03-20 11:51:39 -0700143
Scott Kennedy76c382e2015-02-10 23:15:39 -0800144 public ImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
Alan Viverette617feb92013-09-09 18:09:13 -0700145 this(context, attrs, defStyleAttr, 0);
146 }
147
Scott Kennedy76c382e2015-02-10 23:15:39 -0800148 public ImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
149 int defStyleRes) {
Alan Viverette617feb92013-09-09 18:09:13 -0700150 super(context, attrs, defStyleAttr, defStyleRes);
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 initImageView();
153
Alan Viverette617feb92013-09-09 18:09:13 -0700154 final TypedArray a = context.obtainStyledAttributes(
Alan Viverette6aa92d12015-08-25 13:19:25 -0400155 attrs, R.styleable.ImageView, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Alan Viverette6aa92d12015-08-25 13:19:25 -0400157 final Drawable d = a.getDrawable(R.styleable.ImageView_src);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 if (d != null) {
159 setImageDrawable(d);
160 }
161
Alan Viverette6aa92d12015-08-25 13:19:25 -0400162 mBaselineAlignBottom = a.getBoolean(R.styleable.ImageView_baselineAlignBottom, false);
163 mBaseline = a.getDimensionPixelSize(R.styleable.ImageView_baseline, -1);
Joe Onoratofd52b182010-11-10 18:00:52 -0800164
Alan Viverette6aa92d12015-08-25 13:19:25 -0400165 setAdjustViewBounds(a.getBoolean(R.styleable.ImageView_adjustViewBounds, false));
166 setMaxWidth(a.getDimensionPixelSize(R.styleable.ImageView_maxWidth, Integer.MAX_VALUE));
167 setMaxHeight(a.getDimensionPixelSize(R.styleable.ImageView_maxHeight, Integer.MAX_VALUE));
Joe Onoratofd52b182010-11-10 18:00:52 -0800168
Alan Viverette6aa92d12015-08-25 13:19:25 -0400169 final int index = a.getInt(R.styleable.ImageView_scaleType, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 if (index >= 0) {
171 setScaleType(sScaleTypeArray[index]);
172 }
173
Alan Viverette91174362014-06-17 14:51:45 -0700174 if (a.hasValue(R.styleable.ImageView_tint)) {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700175 mDrawableTintList = a.getColorStateList(R.styleable.ImageView_tint);
Alan Viverette91174362014-06-17 14:51:45 -0700176 mHasDrawableTint = true;
177
Alan Viverette38f93bc2014-09-15 16:25:09 -0700178 // Prior to L, this attribute would always set a color filter with
179 // blending mode SRC_ATOP. Preserve that default behavior.
180 mDrawableTintMode = PorterDuff.Mode.SRC_ATOP;
181 mHasDrawableTintMode = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
Alan Viverette91174362014-06-17 14:51:45 -0700183
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700184 if (a.hasValue(R.styleable.ImageView_tintMode)) {
185 mDrawableTintMode = Drawable.parseTintMode(a.getInt(
186 R.styleable.ImageView_tintMode, -1), mDrawableTintMode);
187 mHasDrawableTintMode = true;
188 }
189
190 applyImageTint();
191
Alan Viverette6aa92d12015-08-25 13:19:25 -0400192 final int alpha = a.getInt(R.styleable.ImageView_drawableAlpha, 255);
Chet Haaseba1fe8e2011-10-15 07:35:51 -0700193 if (alpha != 255) {
Alan Viverette6aa92d12015-08-25 13:19:25 -0400194 setImageAlpha(alpha);
Chet Haaseba1fe8e2011-10-15 07:35:51 -0700195 }
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 mCropToPadding = a.getBoolean(
Alan Viverette6aa92d12015-08-25 13:19:25 -0400198 R.styleable.ImageView_cropToPadding, false);
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 a.recycle();
201
202 //need inflate syntax/reader for matrix
203 }
204
205 private void initImageView() {
Alan Viverette6aa92d12015-08-25 13:19:25 -0400206 mMatrix = new Matrix();
207 mScaleType = ScaleType.FIT_CENTER;
Alan Viverette270a3422015-08-25 13:25:16 -0400208
209 final int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
210 mAdjustViewBoundsCompat = targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR1;
211 mUseCorrectStreamDensity = targetSdkVersion > Build.VERSION_CODES.M;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
213
214 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500215 protected boolean verifyDrawable(@NonNull Drawable dr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 return mDrawable == dr || super.verifyDrawable(dr);
217 }
Chet Haase8473f5a2015-06-17 14:43:30 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -0700220 public void jumpDrawablesToCurrentState() {
221 super.jumpDrawablesToCurrentState();
222 if (mDrawable != null) mDrawable.jumpToCurrentState();
223 }
224
225 @Override
Alan Viverettef6d87ec2016-03-11 10:09:14 -0500226 public void invalidateDrawable(@NonNull Drawable dr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 if (dr == mDrawable) {
Chet Haase8473f5a2015-06-17 14:43:30 -0700228 if (dr != null) {
229 // update cached drawable dimensions if they've changed
230 final int w = dr.getIntrinsicWidth();
231 final int h = dr.getIntrinsicHeight();
232 if (w != mDrawableWidth || h != mDrawableHeight) {
233 mDrawableWidth = w;
234 mDrawableHeight = h;
Alan Viverette01320de2015-10-20 09:48:58 -0400235 // updates the matrix, which is dependent on the bounds
236 configureBounds();
Chet Haase8473f5a2015-06-17 14:43:30 -0700237 }
238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 /* we invalidate the whole view in this case because it's very
240 * hard to know where the drawable actually is. This is made
241 * complicated because of the offsets and transformations that
242 * can be applied. In theory we could get the drawable's bounds
243 * and run them through the transformation and offsets, but this
244 * is probably not worth the effort.
245 */
246 invalidate();
247 } else {
248 super.invalidateDrawable(dr);
249 }
250 }
Joe Onoratofd52b182010-11-10 18:00:52 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 @Override
Chet Haasedb8c9a62012-03-21 18:54:18 -0700253 public boolean hasOverlappingRendering() {
Michael Jurka0931a852013-03-21 16:07:45 +0100254 return (getBackground() != null && getBackground().getCurrent() != null);
Chet Haasedb8c9a62012-03-21 18:54:18 -0700255 }
256
Alan Viverettea54956a2015-01-07 16:05:02 -0800257 /** @hide */
Chet Haasedb8c9a62012-03-21 18:54:18 -0700258 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800259 public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
260 super.onPopulateAccessibilityEventInternal(event);
Alan Viverette6aa92d12015-08-25 13:19:25 -0400261
262 final CharSequence contentDescription = getContentDescription();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700263 if (!TextUtils.isEmpty(contentDescription)) {
264 event.getText().add(contentDescription);
265 }
266 }
267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
Philip Milneaac722a2012-03-26 13:30:26 -0700269 * True when ImageView is adjusting its bounds
270 * to preserve the aspect ratio of its drawable
271 *
272 * @return whether to adjust the bounds of this view
Alan Viverette6aa92d12015-08-25 13:19:25 -0400273 * to preserve the original aspect ratio of the drawable
Philip Milneaac722a2012-03-26 13:30:26 -0700274 *
275 * @see #setAdjustViewBounds(boolean)
276 *
277 * @attr ref android.R.styleable#ImageView_adjustViewBounds
278 */
279 public boolean getAdjustViewBounds() {
280 return mAdjustViewBounds;
281 }
282
283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Set this to true if you want the ImageView to adjust its bounds
285 * to preserve the aspect ratio of its drawable.
Adam Powell2c8cc972012-12-07 18:04:51 -0800286 *
287 * <p><strong>Note:</strong> If the application targets API level 17 or lower,
288 * adjustViewBounds will allow the drawable to shrink the view bounds, but not grow
289 * to fill available measured space in all cases. This is for compatibility with
290 * legacy {@link android.view.View.MeasureSpec MeasureSpec} and
291 * {@link android.widget.RelativeLayout RelativeLayout} behavior.</p>
292 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 * @param adjustViewBounds Whether to adjust the bounds of this view
Adam Powell2c8cc972012-12-07 18:04:51 -0800294 * to preserve the original aspect ratio of the drawable.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400295 *
Philip Milneaac722a2012-03-26 13:30:26 -0700296 * @see #getAdjustViewBounds()
297 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 * @attr ref android.R.styleable#ImageView_adjustViewBounds
299 */
300 @android.view.RemotableViewMethod
301 public void setAdjustViewBounds(boolean adjustViewBounds) {
302 mAdjustViewBounds = adjustViewBounds;
303 if (adjustViewBounds) {
304 setScaleType(ScaleType.FIT_CENTER);
305 }
306 }
Philip Milneaac722a2012-03-26 13:30:26 -0700307
308 /**
309 * The maximum width of this view.
310 *
311 * @return The maximum width of this view
312 *
313 * @see #setMaxWidth(int)
314 *
315 * @attr ref android.R.styleable#ImageView_maxWidth
316 */
317 public int getMaxWidth() {
318 return mMaxWidth;
319 }
320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * An optional argument to supply a maximum width for this view. Only valid if
Romain Guy9fc27812011-04-27 14:21:41 -0700323 * {@link #setAdjustViewBounds(boolean)} has been set to true. To set an image to be a maximum
324 * of 100 x 100 while preserving the original aspect ratio, do the following: 1) set
325 * adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width
326 * layout params to WRAP_CONTENT.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400327 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 * <p>
329 * Note that this view could be still smaller than 100 x 100 using this approach if the original
330 * image is small. To set an image to a fixed size, specify that size in the layout params and
Romain Guy9fc27812011-04-27 14:21:41 -0700331 * then use {@link #setScaleType(android.widget.ImageView.ScaleType)} to determine how to fit
332 * the image within the bounds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * </p>
Alan Viverette6aa92d12015-08-25 13:19:25 -0400334 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 * @param maxWidth maximum width for this view
Philip Milneaac722a2012-03-26 13:30:26 -0700336 *
337 * @see #getMaxWidth()
338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * @attr ref android.R.styleable#ImageView_maxWidth
340 */
341 @android.view.RemotableViewMethod
342 public void setMaxWidth(int maxWidth) {
343 mMaxWidth = maxWidth;
344 }
Philip Milneaac722a2012-03-26 13:30:26 -0700345
346 /**
347 * The maximum height of this view.
348 *
349 * @return The maximum height of this view
350 *
351 * @see #setMaxHeight(int)
352 *
353 * @attr ref android.R.styleable#ImageView_maxHeight
354 */
355 public int getMaxHeight() {
356 return mMaxHeight;
357 }
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * An optional argument to supply a maximum height for this view. Only valid if
Romain Guy9fc27812011-04-27 14:21:41 -0700361 * {@link #setAdjustViewBounds(boolean)} has been set to true. To set an image to be a
362 * maximum of 100 x 100 while preserving the original aspect ratio, do the following: 1) set
363 * adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width
364 * layout params to WRAP_CONTENT.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * <p>
367 * Note that this view could be still smaller than 100 x 100 using this approach if the original
368 * image is small. To set an image to a fixed size, specify that size in the layout params and
Romain Guy9fc27812011-04-27 14:21:41 -0700369 * then use {@link #setScaleType(android.widget.ImageView.ScaleType)} to determine how to fit
370 * the image within the bounds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 * </p>
Alan Viverette6aa92d12015-08-25 13:19:25 -0400372 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 * @param maxHeight maximum height for this view
Philip Milneaac722a2012-03-26 13:30:26 -0700374 *
375 * @see #getMaxHeight()
376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * @attr ref android.R.styleable#ImageView_maxHeight
378 */
379 @android.view.RemotableViewMethod
380 public void setMaxHeight(int maxHeight) {
381 mMaxHeight = maxHeight;
382 }
383
384 /** Return the view's drawable, or null if no drawable has been
385 assigned.
386 */
387 public Drawable getDrawable() {
Chet Haasebfa11e42015-08-05 21:44:42 -0700388 if (mDrawable == mRecycleableBitmapDrawable) {
389 // Consider our cached version dirty since app code now has a reference to it
390 mRecycleableBitmapDrawable = null;
391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 return mDrawable;
393 }
394
Sunny Goyaldd292f42015-12-02 14:29:27 -0800395 private class ImageDrawableCallback implements Runnable {
396
397 private final Drawable drawable;
398 private final Uri uri;
399 private final int resource;
400
401 ImageDrawableCallback(Drawable drawable, Uri uri, int resource) {
402 this.drawable = drawable;
403 this.uri = uri;
404 this.resource = resource;
405 }
406
407 @Override
408 public void run() {
409 setImageDrawable(drawable);
410 mUri = uri;
411 mResource = resource;
412 }
413 }
414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
416 * Sets a drawable as the content of this ImageView.
Brad Fitzpatrickc6b0b772010-08-27 11:43:56 -0700417 *
418 * <p class="note">This does Bitmap reading and decoding on the UI
419 * thread, which can cause a latency hiccup. If that's a concern,
Romain Guy9fc27812011-04-27 14:21:41 -0700420 * consider using {@link #setImageDrawable(android.graphics.drawable.Drawable)} or
421 * {@link #setImageBitmap(android.graphics.Bitmap)} and
Brad Fitzpatrickc6b0b772010-08-27 11:43:56 -0700422 * {@link android.graphics.BitmapFactory} instead.</p>
423 *
Chet Haase430742f2013-04-12 11:18:36 -0700424 * @param resId the resource identifier of the drawable
Brad Fitzpatrickc6b0b772010-08-27 11:43:56 -0700425 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * @attr ref android.R.styleable#ImageView_src
427 */
Sunny Goyaldd292f42015-12-02 14:29:27 -0800428 @android.view.RemotableViewMethod(asyncImpl="setImageResourceAsync")
Tor Norbye7b9c9122013-05-30 16:48:33 -0700429 public void setImageResource(@DrawableRes int resId) {
Alan Viveretted739d7b2014-10-29 17:26:37 -0700430 // The resource configuration may have changed, so we should always
431 // try to load the resource even if the resId hasn't changed.
432 final int oldWidth = mDrawableWidth;
433 final int oldHeight = mDrawableHeight;
Alan Viverette4803bc12014-02-03 14:32:07 -0800434
Alan Viveretted739d7b2014-10-29 17:26:37 -0700435 updateDrawable(null);
436 mResource = resId;
437 mUri = null;
Adam Powellf96ce022012-08-09 15:08:33 -0700438
Alan Viveretted739d7b2014-10-29 17:26:37 -0700439 resolveUri();
Adam Powellf96ce022012-08-09 15:08:33 -0700440
Alan Viveretted739d7b2014-10-29 17:26:37 -0700441 if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
442 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
Alan Viveretted739d7b2014-10-29 17:26:37 -0700444 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446
Sunny Goyaldd292f42015-12-02 14:29:27 -0800447 /** @hide **/
448 public Runnable setImageResourceAsync(@DrawableRes int resId) {
449 return new ImageDrawableCallback(getContext().getDrawable(resId), null, resId);
450 }
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 /**
453 * Sets the content of this ImageView to the specified Uri.
Brad Fitzpatrickc6b0b772010-08-27 11:43:56 -0700454 *
455 * <p class="note">This does Bitmap reading and decoding on the UI
456 * thread, which can cause a latency hiccup. If that's a concern,
Alan Viverette6ef129e2015-06-30 13:32:27 -0700457 * consider using {@link #setImageDrawable(Drawable)} or
Romain Guy9fc27812011-04-27 14:21:41 -0700458 * {@link #setImageBitmap(android.graphics.Bitmap)} and
Brad Fitzpatrickc6b0b772010-08-27 11:43:56 -0700459 * {@link android.graphics.BitmapFactory} instead.</p>
460 *
Alan Viverette6ef129e2015-06-30 13:32:27 -0700461 * @param uri the Uri of an image, or {@code null} to clear the content
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 */
Sunny Goyaldd292f42015-12-02 14:29:27 -0800463 @android.view.RemotableViewMethod(asyncImpl="setImageURIAsync")
Alan Viverette6ef129e2015-06-30 13:32:27 -0700464 public void setImageURI(@Nullable Uri uri) {
Alan Viverette6aa92d12015-08-25 13:19:25 -0400465 if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 updateDrawable(null);
467 mResource = 0;
468 mUri = uri;
Adam Powellf96ce022012-08-09 15:08:33 -0700469
470 final int oldWidth = mDrawableWidth;
471 final int oldHeight = mDrawableHeight;
472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 resolveUri();
Adam Powellf96ce022012-08-09 15:08:33 -0700474
475 if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
476 requestLayout();
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 invalidate();
479 }
480 }
481
Sunny Goyaldd292f42015-12-02 14:29:27 -0800482 /** @hide **/
483 public Runnable setImageURIAsync(@Nullable Uri uri) {
484 if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) {
485 Drawable d = uri == null ? null : getDrawableFromUri(uri);
486 if (d == null) {
487 // Do not set the URI if the drawable couldn't be loaded.
488 uri = null;
489 }
490 return new ImageDrawableCallback(d, uri, 0);
491 }
492 return null;
493 }
494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 /**
496 * Sets a drawable as the content of this ImageView.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400497 *
Alan Viverette6ef129e2015-06-30 13:32:27 -0700498 * @param drawable the Drawable to set, or {@code null} to clear the
499 * content
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 */
Alan Viverette6ef129e2015-06-30 13:32:27 -0700501 public void setImageDrawable(@Nullable Drawable drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 if (mDrawable != drawable) {
503 mResource = 0;
504 mUri = null;
Romain Guy9e648c42011-08-17 20:04:27 -0700505
Adam Powellf96ce022012-08-09 15:08:33 -0700506 final int oldWidth = mDrawableWidth;
507 final int oldHeight = mDrawableHeight;
Romain Guy9e648c42011-08-17 20:04:27 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 updateDrawable(drawable);
Adam Powell2a0e99d2011-08-04 10:55:03 -0700510
Romain Guy9e648c42011-08-17 20:04:27 -0700511 if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
Adam Powell2a0e99d2011-08-04 10:55:03 -0700512 requestLayout();
513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 invalidate();
515 }
516 }
517
518 /**
Dan Sandlera22a3802015-05-13 00:12:47 -0400519 * Sets the content of this ImageView to the specified Icon.
520 *
Alan Viverette6ef129e2015-06-30 13:32:27 -0700521 * <p class="note">Depending on the Icon type, this may do Bitmap reading
522 * and decoding on the UI thread, which can cause UI jank. If that's a
523 * concern, consider using
Chris Banese695bb82015-05-14 10:12:20 +0100524 * {@link Icon#loadDrawableAsync(Context, Icon.OnDrawableLoadedListener, Handler)}
Alan Viverette6ef129e2015-06-30 13:32:27 -0700525 * and then {@link #setImageDrawable(android.graphics.drawable.Drawable)}
526 * instead.</p>
Dan Sandlera22a3802015-05-13 00:12:47 -0400527 *
Alan Viverette6ef129e2015-06-30 13:32:27 -0700528 * @param icon an Icon holding the desired image, or {@code null} to clear
529 * the content
Dan Sandlera22a3802015-05-13 00:12:47 -0400530 */
Sunny Goyaldd292f42015-12-02 14:29:27 -0800531 @android.view.RemotableViewMethod(asyncImpl="setImageIconAsync")
Alan Viverette6ef129e2015-06-30 13:32:27 -0700532 public void setImageIcon(@Nullable Icon icon) {
533 setImageDrawable(icon == null ? null : icon.loadDrawable(mContext));
Dan Sandlera22a3802015-05-13 00:12:47 -0400534 }
535
Sunny Goyaldd292f42015-12-02 14:29:27 -0800536 /** @hide **/
537 public Runnable setImageIconAsync(@Nullable Icon icon) {
538 return new ImageDrawableCallback(icon == null ? null : icon.loadDrawable(mContext), null, 0);
539 }
540
Dan Sandlera22a3802015-05-13 00:12:47 -0400541 /**
Alan Viverette91174362014-06-17 14:51:45 -0700542 * Applies a tint to the image drawable. Does not modify the current tint
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700543 * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
Alan Viverette91174362014-06-17 14:51:45 -0700544 * <p>
545 * Subsequent calls to {@link #setImageDrawable(Drawable)} will automatically
546 * mutate the drawable and apply the specified tint and tint mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700547 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700548 *
549 * @param tint the tint to apply, may be {@code null} to clear tint
550 *
551 * @attr ref android.R.styleable#ImageView_tint
Alan Viverettea4264452014-07-28 16:02:55 -0700552 * @see #getImageTintList()
553 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700554 */
Alan Viverettea4264452014-07-28 16:02:55 -0700555 public void setImageTintList(@Nullable ColorStateList tint) {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700556 mDrawableTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700557 mHasDrawableTint = true;
558
Alan Viverettea4264452014-07-28 16:02:55 -0700559 applyImageTint();
Alan Viverette91174362014-06-17 14:51:45 -0700560 }
561
562 /**
563 * @return the tint applied to the image drawable
564 * @attr ref android.R.styleable#ImageView_tint
Alan Viverettea4264452014-07-28 16:02:55 -0700565 * @see #setImageTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700566 */
567 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700568 public ColorStateList getImageTintList() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700569 return mDrawableTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700570 }
571
572 /**
573 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700574 * {@link #setImageTintList(ColorStateList)}} to the image drawable. The default
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700575 * mode is {@link PorterDuff.Mode#SRC_IN}.
Alan Viverette91174362014-06-17 14:51:45 -0700576 *
577 * @param tintMode the blending mode used to apply the tint, may be
578 * {@code null} to clear tint
579 * @attr ref android.R.styleable#ImageView_tintMode
Alan Viverettea4264452014-07-28 16:02:55 -0700580 * @see #getImageTintMode()
581 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700582 */
Alan Viverettea4264452014-07-28 16:02:55 -0700583 public void setImageTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700584 mDrawableTintMode = tintMode;
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700585 mHasDrawableTintMode = true;
Alan Viverette4f64c042014-07-21 17:49:13 -0700586
Alan Viverettea4264452014-07-28 16:02:55 -0700587 applyImageTint();
Alan Viverette91174362014-06-17 14:51:45 -0700588 }
589
590 /**
591 * @return the blending mode used to apply the tint to the image drawable
592 * @attr ref android.R.styleable#ImageView_tintMode
Alan Viverettea4264452014-07-28 16:02:55 -0700593 * @see #setImageTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700594 */
595 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700596 public PorterDuff.Mode getImageTintMode() {
Alan Viverette91174362014-06-17 14:51:45 -0700597 return mDrawableTintMode;
598 }
599
Alan Viverettea4264452014-07-28 16:02:55 -0700600 private void applyImageTint() {
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700601 if (mDrawable != null && (mHasDrawableTint || mHasDrawableTintMode)) {
Alan Viverette91174362014-06-17 14:51:45 -0700602 mDrawable = mDrawable.mutate();
Alan Viveretteb56f5d22014-09-14 15:48:50 -0700603
604 if (mHasDrawableTint) {
605 mDrawable.setTintList(mDrawableTintList);
606 }
607
608 if (mHasDrawableTintMode) {
609 mDrawable.setTintMode(mDrawableTintMode);
610 }
Alan Viveretted5133792014-10-28 14:41:36 -0700611
612 // The drawable (or one of its children) may not have been
613 // stateful before applying the tint, so let's try again.
614 if (mDrawable.isStateful()) {
615 mDrawable.setState(getDrawableState());
616 }
Alan Viverette91174362014-06-17 14:51:45 -0700617 }
618 }
619
620 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * Sets a Bitmap as the content of this ImageView.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400622 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @param bm The bitmap to set
624 */
625 @android.view.RemotableViewMethod
626 public void setImageBitmap(Bitmap bm) {
John Reck5a135692015-07-10 10:02:58 -0700627 // Hacky fix to force setImageDrawable to do a full setImageDrawable
628 // instead of doing an object reference comparison
629 mDrawable = null;
630 if (mRecycleableBitmapDrawable == null) {
Chris Craik82af13e2015-07-14 13:36:27 -0700631 mRecycleableBitmapDrawable = new BitmapDrawable(mContext.getResources(), bm);
John Reckb7ba1222015-07-09 17:37:34 -0700632 } else {
John Reck5a135692015-07-10 10:02:58 -0700633 mRecycleableBitmapDrawable.setBitmap(bm);
John Reckb7ba1222015-07-09 17:37:34 -0700634 }
John Reck5a135692015-07-10 10:02:58 -0700635 setImageDrawable(mRecycleableBitmapDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637
638 public void setImageState(int[] state, boolean merge) {
639 mState = state;
640 mMergeState = merge;
641 if (mDrawable != null) {
642 refreshDrawableState();
643 resizeFromDrawable();
644 }
645 }
646
647 @Override
648 public void setSelected(boolean selected) {
649 super.setSelected(selected);
650 resizeFromDrawable();
651 }
652
The Android Open Source Project4df24232009-03-05 14:34:35 -0800653 /**
Alan Viverette6aa92d12015-08-25 13:19:25 -0400654 * Sets the image level, when it is constructed from a
The Android Open Source Project4df24232009-03-05 14:34:35 -0800655 * {@link android.graphics.drawable.LevelListDrawable}.
656 *
657 * @param level The new level for the image.
658 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 @android.view.RemotableViewMethod
660 public void setImageLevel(int level) {
661 mLevel = level;
662 if (mDrawable != null) {
663 mDrawable.setLevel(level);
664 resizeFromDrawable();
665 }
666 }
667
668 /**
669 * Options for scaling the bounds of an image to the bounds of this view.
670 */
671 public enum ScaleType {
672 /**
673 * Scale using the image matrix when drawing. The image matrix can be set using
674 * {@link ImageView#setImageMatrix(Matrix)}. From XML, use this syntax:
675 * <code>android:scaleType="matrix"</code>.
676 */
677 MATRIX (0),
678 /**
679 * Scale the image using {@link Matrix.ScaleToFit#FILL}.
680 * From XML, use this syntax: <code>android:scaleType="fitXY"</code>.
681 */
682 FIT_XY (1),
683 /**
684 * Scale the image using {@link Matrix.ScaleToFit#START}.
685 * From XML, use this syntax: <code>android:scaleType="fitStart"</code>.
686 */
687 FIT_START (2),
688 /**
689 * Scale the image using {@link Matrix.ScaleToFit#CENTER}.
690 * From XML, use this syntax:
691 * <code>android:scaleType="fitCenter"</code>.
692 */
693 FIT_CENTER (3),
694 /**
695 * Scale the image using {@link Matrix.ScaleToFit#END}.
696 * From XML, use this syntax: <code>android:scaleType="fitEnd"</code>.
697 */
698 FIT_END (4),
699 /**
700 * Center the image in the view, but perform no scaling.
701 * From XML, use this syntax: <code>android:scaleType="center"</code>.
702 */
703 CENTER (5),
704 /**
705 * Scale the image uniformly (maintain the image's aspect ratio) so
706 * that both dimensions (width and height) of the image will be equal
707 * to or larger than the corresponding dimension of the view
708 * (minus padding). The image is then centered in the view.
709 * From XML, use this syntax: <code>android:scaleType="centerCrop"</code>.
710 */
711 CENTER_CROP (6),
712 /**
713 * Scale the image uniformly (maintain the image's aspect ratio) so
714 * that both dimensions (width and height) of the image will be equal
715 * to or less than the corresponding dimension of the view
716 * (minus padding). The image is then centered in the view.
717 * From XML, use this syntax: <code>android:scaleType="centerInside"</code>.
718 */
719 CENTER_INSIDE (7);
Alan Viverette6aa92d12015-08-25 13:19:25 -0400720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 ScaleType(int ni) {
722 nativeInt = ni;
723 }
724 final int nativeInt;
725 }
726
727 /**
728 * Controls how the image should be resized or moved to match the size
729 * of this ImageView.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400730 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 * @param scaleType The desired scaling mode.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * @attr ref android.R.styleable#ImageView_scaleType
734 */
735 public void setScaleType(ScaleType scaleType) {
736 if (scaleType == null) {
737 throw new NullPointerException();
738 }
739
740 if (mScaleType != scaleType) {
741 mScaleType = scaleType;
742
Alan Viverette6aa92d12015-08-25 13:19:25 -0400743 setWillNotCacheDrawing(mScaleType == ScaleType.CENTER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744
745 requestLayout();
746 invalidate();
747 }
748 }
Alan Viverette6aa92d12015-08-25 13:19:25 -0400749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 /**
751 * Return the current scale type in use by this ImageView.
752 *
753 * @see ImageView.ScaleType
754 *
755 * @attr ref android.R.styleable#ImageView_scaleType
756 */
757 public ScaleType getScaleType() {
758 return mScaleType;
759 }
760
761 /** Return the view's optional matrix. This is applied to the
John Spurlock0196e562014-02-03 09:00:35 -0500762 view's drawable when it is drawn. If there is no matrix,
Romain Guy53704052013-01-30 16:25:50 -0800763 this method will return an identity matrix.
764 Do not change this matrix in place but make a copy.
765 If you want a different matrix applied to the drawable,
766 be sure to call setImageMatrix().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 */
768 public Matrix getImageMatrix() {
Dake Gu1ee60172013-01-03 15:11:43 -0800769 if (mDrawMatrix == null) {
Romain Guy53704052013-01-30 16:25:50 -0800770 return new Matrix(Matrix.IDENTITY_MATRIX);
Dake Gu1ee60172013-01-03 15:11:43 -0800771 }
772 return mDrawMatrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774
Joseph Cooperd96fdbd2015-04-03 14:00:31 -0700775 /**
776 * Adds a transformation {@link Matrix} that is applied
777 * to the view's drawable when it is drawn. Allows custom scaling,
778 * translation, and perspective distortion.
Alan Viverette6aa92d12015-08-25 13:19:25 -0400779 *
Joseph Cooperd96fdbd2015-04-03 14:00:31 -0700780 * @param matrix the transformation parameters in matrix form
781 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 public void setImageMatrix(Matrix matrix) {
Joseph Cooperd96fdbd2015-04-03 14:00:31 -0700783 // collapse null and identity to just null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 if (matrix != null && matrix.isIdentity()) {
785 matrix = null;
786 }
Joseph Cooperd96fdbd2015-04-03 14:00:31 -0700787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 // don't invalidate unless we're actually changing our matrix
789 if (matrix == null && !mMatrix.isIdentity() ||
790 matrix != null && !mMatrix.equals(matrix)) {
791 mMatrix.set(matrix);
Chih-Chung Changf8887262009-07-30 15:36:13 +0800792 configureBounds();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 invalidate();
794 }
795 }
Philip Milneaac722a2012-03-26 13:30:26 -0700796
797 /**
798 * Return whether this ImageView crops to padding.
799 *
800 * @return whether this ImageView crops to padding
801 *
802 * @see #setCropToPadding(boolean)
803 *
804 * @attr ref android.R.styleable#ImageView_cropToPadding
805 */
806 public boolean getCropToPadding() {
807 return mCropToPadding;
808 }
809
810 /**
811 * Sets whether this ImageView will crop to padding.
812 *
813 * @param cropToPadding whether this ImageView will crop to padding
814 *
815 * @see #getCropToPadding()
816 *
817 * @attr ref android.R.styleable#ImageView_cropToPadding
818 */
819 public void setCropToPadding(boolean cropToPadding) {
820 if (mCropToPadding != cropToPadding) {
821 mCropToPadding = cropToPadding;
822 requestLayout();
823 invalidate();
824 }
825 }
826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 private void resolveUri() {
828 if (mDrawable != null) {
829 return;
830 }
831
Sunny Goyaldd292f42015-12-02 14:29:27 -0800832 if (getResources() == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 return;
834 }
835
836 Drawable d = null;
837
838 if (mResource != 0) {
839 try {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800840 d = mContext.getDrawable(mResource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 } catch (Exception e) {
Alan Viverette6aa92d12015-08-25 13:19:25 -0400842 Log.w(LOG_TAG, "Unable to find resource: " + mResource, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 // Don't try again.
844 mUri = null;
845 }
846 } else if (mUri != null) {
Sunny Goyaldd292f42015-12-02 14:29:27 -0800847 d = getDrawableFromUri(mUri);
Alan Viverette6aa92d12015-08-25 13:19:25 -0400848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 if (d == null) {
Alan Viverette6aa92d12015-08-25 13:19:25 -0400850 Log.w(LOG_TAG, "resolveUri failed on bad bitmap uri: " + mUri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 // Don't try again.
852 mUri = null;
853 }
854 } else {
855 return;
856 }
857
858 updateDrawable(d);
859 }
860
Sunny Goyaldd292f42015-12-02 14:29:27 -0800861 private Drawable getDrawableFromUri(Uri uri) {
862 final String scheme = uri.getScheme();
863 if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
864 try {
865 // Load drawable through Resources, to get the source density information
866 ContentResolver.OpenResourceIdResult r =
867 mContext.getContentResolver().getResourceId(uri);
868 return r.r.getDrawable(r.id, mContext.getTheme());
869 } catch (Exception e) {
870 Log.w(LOG_TAG, "Unable to open content: " + uri, e);
871 }
872 } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)
873 || ContentResolver.SCHEME_FILE.equals(scheme)) {
874 InputStream stream = null;
875 try {
876 stream = mContext.getContentResolver().openInputStream(uri);
877 return Drawable.createFromResourceStream(
878 mUseCorrectStreamDensity ? getResources() : null, null, stream, null);
879 } catch (Exception e) {
880 Log.w(LOG_TAG, "Unable to open content: " + uri, e);
881 } finally {
882 if (stream != null) {
883 try {
884 stream.close();
885 } catch (IOException e) {
886 Log.w(LOG_TAG, "Unable to close content: " + uri, e);
887 }
888 }
889 }
890 } else {
891 return Drawable.createFromPath(uri.toString());
892 }
893 return null;
894 }
895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 @Override
897 public int[] onCreateDrawableState(int extraSpace) {
898 if (mState == null) {
899 return super.onCreateDrawableState(extraSpace);
900 } else if (!mMergeState) {
901 return mState;
902 } else {
903 return mergeDrawableStates(
904 super.onCreateDrawableState(extraSpace + mState.length), mState);
905 }
906 }
907
908 private void updateDrawable(Drawable d) {
John Reck5a135692015-07-10 10:02:58 -0700909 if (d != mRecycleableBitmapDrawable && mRecycleableBitmapDrawable != null) {
910 mRecycleableBitmapDrawable.setBitmap(null);
911 }
912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 if (mDrawable != null) {
Adam Powell35e2ea02016-03-22 10:40:29 -0700914 // It's possible for this method to be invoked from the constructor before
915 // subclass constructors have run. Drawables can and should trigger invalidations
916 // and other activity with their callback on visibility changes, which shouldn't
917 // happen before subclass constructors finish. However, we won't have set the
918 // drawable as visible until the view becomes attached. This guard below keeps
919 // multiple calls to this method from constructors from causing issues.
920 if (mDrawable.isVisible()) {
Adam Powellc5874092016-03-17 16:27:24 -0700921 mDrawable.setVisible(false, false);
922 }
Adam Powell35e2ea02016-03-22 10:40:29 -0700923 mDrawable.setCallback(null);
924 unscheduleDrawable(mDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
Alan Viverette91174362014-06-17 14:51:45 -0700926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 mDrawable = d;
Alan Viverette91174362014-06-17 14:51:45 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 if (d != null) {
930 d.setCallback(this);
Alan Viverette91174362014-06-17 14:51:45 -0700931 d.setLayoutDirection(getLayoutDirection());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 if (d.isStateful()) {
933 d.setState(getDrawableState());
934 }
Adam Powellc5874092016-03-17 16:27:24 -0700935 if (isAttachedToWindow()) {
936 d.setVisible(getWindowVisibility() == VISIBLE && isShown(), true);
937 }
Alan Viverette91174362014-06-17 14:51:45 -0700938 d.setLevel(mLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 mDrawableWidth = d.getIntrinsicWidth();
940 mDrawableHeight = d.getIntrinsicHeight();
Alan Viverettea4264452014-07-28 16:02:55 -0700941 applyImageTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 applyColorMod();
Alan Viveretted5133792014-10-28 14:41:36 -0700943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 configureBounds();
Romain Guy9e648c42011-08-17 20:04:27 -0700945 } else {
946 mDrawableWidth = mDrawableHeight = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 }
948 }
949
950 private void resizeFromDrawable() {
Alan Viverette6aa92d12015-08-25 13:19:25 -0400951 final Drawable d = mDrawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 if (d != null) {
953 int w = d.getIntrinsicWidth();
954 if (w < 0) w = mDrawableWidth;
955 int h = d.getIntrinsicHeight();
956 if (h < 0) h = mDrawableHeight;
957 if (w != mDrawableWidth || h != mDrawableHeight) {
958 mDrawableWidth = w;
959 mDrawableHeight = h;
960 requestLayout();
961 }
962 }
963 }
964
Fabrice Di Meglio3f5a90b2013-06-24 19:22:25 -0700965 @Override
966 public void onRtlPropertiesChanged(int layoutDirection) {
967 super.onRtlPropertiesChanged(layoutDirection);
968
969 if (mDrawable != null) {
970 mDrawable.setLayoutDirection(layoutDirection);
971 }
972 }
973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 private static final Matrix.ScaleToFit[] sS2FArray = {
975 Matrix.ScaleToFit.FILL,
976 Matrix.ScaleToFit.START,
977 Matrix.ScaleToFit.CENTER,
978 Matrix.ScaleToFit.END
979 };
980
981 private static Matrix.ScaleToFit scaleTypeToScaleToFit(ScaleType st) {
982 // ScaleToFit enum to their corresponding Matrix.ScaleToFit values
983 return sS2FArray[st.nativeInt - 1];
Alan Viverette6aa92d12015-08-25 13:19:25 -0400984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985
986 @Override
987 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
988 resolveUri();
989 int w;
990 int h;
Alan Viverette6aa92d12015-08-25 13:19:25 -0400991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 // Desired aspect ratio of the view's contents (not including padding)
993 float desiredAspect = 0.0f;
Alan Viverette6aa92d12015-08-25 13:19:25 -0400994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 // We are allowed to change the view's width
996 boolean resizeWidth = false;
Alan Viverette6aa92d12015-08-25 13:19:25 -0400997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 // We are allowed to change the view's height
999 boolean resizeHeight = false;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001000
Adam Powell2a0e99d2011-08-04 10:55:03 -07001001 final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
1002 final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
1003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 if (mDrawable == null) {
1005 // If no drawable, its intrinsic size is 0.
1006 mDrawableWidth = -1;
1007 mDrawableHeight = -1;
1008 w = h = 0;
1009 } else {
1010 w = mDrawableWidth;
1011 h = mDrawableHeight;
1012 if (w <= 0) w = 1;
1013 if (h <= 0) h = 1;
Romain Guy9e648c42011-08-17 20:04:27 -07001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 // We are supposed to adjust view bounds to match the aspect
1016 // ratio of our drawable. See if that is possible.
1017 if (mAdjustViewBounds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
1019 resizeHeight = heightSpecMode != MeasureSpec.EXACTLY;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001020
Romain Guy9e648c42011-08-17 20:04:27 -07001021 desiredAspect = (float) w / (float) h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001024
1025 final int pleft = mPaddingLeft;
1026 final int pright = mPaddingRight;
1027 final int ptop = mPaddingTop;
1028 final int pbottom = mPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
1030 int widthSize;
1031 int heightSize;
1032
1033 if (resizeWidth || resizeHeight) {
1034 /* If we get here, it means we want to resize to match the
1035 drawables aspect ratio, and we have the freedom to change at
Alan Viverette6aa92d12015-08-25 13:19:25 -04001036 least one dimension.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038
1039 // Get the max possible width given our constraints
Romain Guy9e648c42011-08-17 20:04:27 -07001040 widthSize = resolveAdjustedSize(w + pleft + pright, mMaxWidth, widthMeasureSpec);
1041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 // Get the max possible height given our constraints
Romain Guy9e648c42011-08-17 20:04:27 -07001043 heightSize = resolveAdjustedSize(h + ptop + pbottom, mMaxHeight, heightMeasureSpec);
1044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (desiredAspect != 0.0f) {
1046 // See what our actual aspect ratio is
Alan Viverette6aa92d12015-08-25 13:19:25 -04001047 final float actualAspect = (float)(widthSize - pleft - pright) /
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 (heightSize - ptop - pbottom);
Alan Viverette6aa92d12015-08-25 13:19:25 -04001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 if (Math.abs(actualAspect - desiredAspect) > 0.0000001) {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 boolean done = false;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 // Try adjusting width to be proportional to height
1055 if (resizeWidth) {
Romain Guy9e648c42011-08-17 20:04:27 -07001056 int newWidth = (int)(desiredAspect * (heightSize - ptop - pbottom)) +
1057 pleft + pright;
Adam Powelld5edc772012-09-26 15:21:39 -07001058
1059 // Allow the width to outgrow its original estimate if height is fixed.
Adam Powell7da4b732012-12-07 15:28:33 -08001060 if (!resizeHeight && !mAdjustViewBoundsCompat) {
Adam Powelld5edc772012-09-26 15:21:39 -07001061 widthSize = resolveAdjustedSize(newWidth, mMaxWidth, widthMeasureSpec);
1062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 if (newWidth <= widthSize) {
1065 widthSize = newWidth;
1066 done = true;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 // Try adjusting height to be proportional to width
1071 if (!done && resizeHeight) {
Romain Guy9e648c42011-08-17 20:04:27 -07001072 int newHeight = (int)((widthSize - pleft - pright) / desiredAspect) +
1073 ptop + pbottom;
Adam Powelld5edc772012-09-26 15:21:39 -07001074
1075 // Allow the height to outgrow its original estimate if width is fixed.
Adam Powell7da4b732012-12-07 15:28:33 -08001076 if (!resizeWidth && !mAdjustViewBoundsCompat) {
Adam Powelld5edc772012-09-26 15:21:39 -07001077 heightSize = resolveAdjustedSize(newHeight, mMaxHeight,
1078 heightMeasureSpec);
1079 }
1080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 if (newHeight <= heightSize) {
1082 heightSize = newHeight;
Dianne Hackborn189ee182010-12-02 21:48:53 -08001083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
1085 }
1086 }
1087 } else {
1088 /* We are either don't want to preserve the drawables aspect ratio,
1089 or we are not allowed to change view dimensions. Just measure in
1090 the normal way.
1091 */
1092 w += pleft + pright;
1093 h += ptop + pbottom;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 w = Math.max(w, getSuggestedMinimumWidth());
1096 h = Math.max(h, getSuggestedMinimumHeight());
1097
Dianne Hackborn189ee182010-12-02 21:48:53 -08001098 widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);
1099 heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
1101
1102 setMeasuredDimension(widthSize, heightSize);
1103 }
1104
1105 private int resolveAdjustedSize(int desiredSize, int maxSize,
1106 int measureSpec) {
1107 int result = desiredSize;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001108 final int specMode = MeasureSpec.getMode(measureSpec);
1109 final int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 switch (specMode) {
1111 case MeasureSpec.UNSPECIFIED:
1112 /* Parent says we can be as big as we want. Just don't be larger
1113 than max size imposed on ourselves.
1114 */
1115 result = Math.min(desiredSize, maxSize);
1116 break;
1117 case MeasureSpec.AT_MOST:
Alan Viverette6aa92d12015-08-25 13:19:25 -04001118 // Parent says we can be as big as we want, up to specSize.
1119 // Don't be larger than specSize, and don't be larger than
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 // the max size imposed on ourselves.
1121 result = Math.min(Math.min(desiredSize, specSize), maxSize);
1122 break;
1123 case MeasureSpec.EXACTLY:
1124 // No choice. Do what we are told.
1125 result = specSize;
1126 break;
1127 }
1128 return result;
1129 }
1130
1131 @Override
1132 protected boolean setFrame(int l, int t, int r, int b) {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001133 final boolean changed = super.setFrame(l, t, r, b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 mHaveFrame = true;
1135 configureBounds();
1136 return changed;
1137 }
1138
1139 private void configureBounds() {
1140 if (mDrawable == null || !mHaveFrame) {
1141 return;
1142 }
1143
Alan Viverette6aa92d12015-08-25 13:19:25 -04001144 final int dwidth = mDrawableWidth;
1145 final int dheight = mDrawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146
Alan Viverette6aa92d12015-08-25 13:19:25 -04001147 final int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
1148 final int vheight = getHeight() - mPaddingTop - mPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149
Alan Viverette6aa92d12015-08-25 13:19:25 -04001150 final boolean fits = (dwidth < 0 || vwidth == dwidth)
1151 && (dheight < 0 || vheight == dheight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152
1153 if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) {
1154 /* If the drawable has no intrinsic size, or we're told to
1155 scaletofit, then we just fill our entire view.
1156 */
1157 mDrawable.setBounds(0, 0, vwidth, vheight);
1158 mDrawMatrix = null;
1159 } else {
1160 // We need to do the scaling ourself, so have the drawable
1161 // use its native size.
1162 mDrawable.setBounds(0, 0, dwidth, dheight);
1163
1164 if (ScaleType.MATRIX == mScaleType) {
1165 // Use the specified matrix as-is.
1166 if (mMatrix.isIdentity()) {
1167 mDrawMatrix = null;
1168 } else {
1169 mDrawMatrix = mMatrix;
1170 }
1171 } else if (fits) {
1172 // The bitmap fits exactly, no transform needed.
1173 mDrawMatrix = null;
1174 } else if (ScaleType.CENTER == mScaleType) {
1175 // Center bitmap in view, no scaling.
1176 mDrawMatrix = mMatrix;
Adam Powell14d1f1382015-06-04 12:56:00 -07001177 mDrawMatrix.setTranslate(Math.round((vwidth - dwidth) * 0.5f),
1178 Math.round((vheight - dheight) * 0.5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 } else if (ScaleType.CENTER_CROP == mScaleType) {
1180 mDrawMatrix = mMatrix;
1181
1182 float scale;
1183 float dx = 0, dy = 0;
1184
1185 if (dwidth * vheight > vwidth * dheight) {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001186 scale = (float) vheight / (float) dheight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 dx = (vwidth - dwidth * scale) * 0.5f;
1188 } else {
1189 scale = (float) vwidth / (float) dwidth;
1190 dy = (vheight - dheight * scale) * 0.5f;
1191 }
1192
1193 mDrawMatrix.setScale(scale, scale);
Adam Powell14d1f1382015-06-04 12:56:00 -07001194 mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 } else if (ScaleType.CENTER_INSIDE == mScaleType) {
1196 mDrawMatrix = mMatrix;
1197 float scale;
1198 float dx;
1199 float dy;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 if (dwidth <= vwidth && dheight <= vheight) {
1202 scale = 1.0f;
1203 } else {
Romain Guy9e648c42011-08-17 20:04:27 -07001204 scale = Math.min((float) vwidth / (float) dwidth,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 (float) vheight / (float) dheight);
1206 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001207
Adam Powell14d1f1382015-06-04 12:56:00 -07001208 dx = Math.round((vwidth - dwidth * scale) * 0.5f);
1209 dy = Math.round((vheight - dheight * scale) * 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210
1211 mDrawMatrix.setScale(scale, scale);
1212 mDrawMatrix.postTranslate(dx, dy);
1213 } else {
1214 // Generate the required transform.
1215 mTempSrc.set(0, 0, dwidth, dheight);
1216 mTempDst.set(0, 0, vwidth, vheight);
Alan Viverette6aa92d12015-08-25 13:19:25 -04001217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 mDrawMatrix = mMatrix;
Romain Guy9e648c42011-08-17 20:04:27 -07001219 mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221 }
1222 }
1223
1224 @Override
1225 protected void drawableStateChanged() {
1226 super.drawableStateChanged();
Alan Viverette6aa92d12015-08-25 13:19:25 -04001227
Alan Viverettead0020f2015-09-04 10:10:42 -04001228 final Drawable drawable = mDrawable;
1229 if (drawable != null && drawable.isStateful()
1230 && drawable.setState(getDrawableState())) {
1231 invalidateDrawable(drawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 }
1234
Alan Viverettecebc6ba2014-06-13 15:52:13 -07001235 @Override
Alan Viverette8de14942014-06-18 18:05:15 -07001236 public void drawableHotspotChanged(float x, float y) {
1237 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -07001238
1239 if (mDrawable != null) {
1240 mDrawable.setHotspot(x, y);
1241 }
1242 }
1243
George Mount990205e2014-06-24 09:36:18 -07001244 /** @hide */
1245 public void animateTransform(Matrix matrix) {
George Mountf6c763d2014-09-25 15:13:15 -07001246 if (mDrawable == null) {
1247 return;
1248 }
George Mount990205e2014-06-24 09:36:18 -07001249 if (matrix == null) {
1250 mDrawable.setBounds(0, 0, getWidth(), getHeight());
1251 } else {
1252 mDrawable.setBounds(0, 0, mDrawableWidth, mDrawableHeight);
1253 if (mDrawMatrix == null) {
1254 mDrawMatrix = new Matrix();
1255 }
1256 mDrawMatrix.set(matrix);
1257 }
1258 invalidate();
1259 }
1260
Alan Viverettecebc6ba2014-06-13 15:52:13 -07001261 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 protected void onDraw(Canvas canvas) {
1263 super.onDraw(canvas);
1264
1265 if (mDrawable == null) {
1266 return; // couldn't resolve the URI
1267 }
1268
1269 if (mDrawableWidth == 0 || mDrawableHeight == 0) {
1270 return; // nothing to draw (empty bounds)
1271 }
1272
1273 if (mDrawMatrix == null && mPaddingTop == 0 && mPaddingLeft == 0) {
1274 mDrawable.draw(canvas);
1275 } else {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001276 final int saveCount = canvas.getSaveCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 canvas.save();
Alan Viverette6aa92d12015-08-25 13:19:25 -04001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if (mCropToPadding) {
1280 final int scrollX = mScrollX;
1281 final int scrollY = mScrollY;
1282 canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop,
1283 scrollX + mRight - mLeft - mPaddingRight,
1284 scrollY + mBottom - mTop - mPaddingBottom);
1285 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 canvas.translate(mPaddingLeft, mPaddingTop);
1288
1289 if (mDrawMatrix != null) {
1290 canvas.concat(mDrawMatrix);
1291 }
1292 mDrawable.draw(canvas);
1293 canvas.restoreToCount(saveCount);
1294 }
1295 }
1296
Joe Onoratofd52b182010-11-10 18:00:52 -08001297 /**
1298 * <p>Return the offset of the widget's text baseline from the widget's top
1299 * boundary. </p>
1300 *
1301 * @return the offset of the baseline within the widget's bounds or -1
1302 * if baseline alignment is not supported.
1303 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 @Override
Joe Onoratofd52b182010-11-10 18:00:52 -08001305 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 public int getBaseline() {
Joe Onoratofd52b182010-11-10 18:00:52 -08001307 if (mBaselineAlignBottom) {
1308 return getMeasuredHeight();
1309 } else {
1310 return mBaseline;
1311 }
1312 }
1313
1314 /**
1315 * <p>Set the offset of the widget's text baseline from the widget's top
Romain Guy9fc27812011-04-27 14:21:41 -07001316 * boundary. This value is overridden by the {@link #setBaselineAlignBottom(boolean)}
Joe Onoratofd52b182010-11-10 18:00:52 -08001317 * property.</p>
1318 *
1319 * @param baseline The baseline to use, or -1 if none is to be provided.
1320 *
Alan Viverette6aa92d12015-08-25 13:19:25 -04001321 * @see #setBaseline(int)
Joe Onoratofd52b182010-11-10 18:00:52 -08001322 * @attr ref android.R.styleable#ImageView_baseline
1323 */
1324 public void setBaseline(int baseline) {
1325 if (mBaseline != baseline) {
1326 mBaseline = baseline;
1327 requestLayout();
1328 }
1329 }
1330
1331 /**
1332 * Set whether to set the baseline of this view to the bottom of the view.
1333 * Setting this value overrides any calls to setBaseline.
1334 *
1335 * @param aligned If true, the image view will be baseline aligned with
1336 * based on its bottom edge.
1337 *
1338 * @attr ref android.R.styleable#ImageView_baselineAlignBottom
1339 */
1340 public void setBaselineAlignBottom(boolean aligned) {
1341 if (mBaselineAlignBottom != aligned) {
1342 mBaselineAlignBottom = aligned;
1343 requestLayout();
1344 }
1345 }
1346
1347 /**
1348 * Return whether this view's baseline will be considered the bottom of the view.
1349 *
1350 * @see #setBaselineAlignBottom(boolean)
1351 */
1352 public boolean getBaselineAlignBottom() {
1353 return mBaselineAlignBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 /**
1357 * Set a tinting option for the image.
Alan Viverette6aa92d12015-08-25 13:19:25 -04001358 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 * @param color Color tint to apply.
1360 * @param mode How to apply the color. The standard mode is
1361 * {@link PorterDuff.Mode#SRC_ATOP}
Alan Viverette6aa92d12015-08-25 13:19:25 -04001362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 * @attr ref android.R.styleable#ImageView_tint
1364 */
1365 public final void setColorFilter(int color, PorterDuff.Mode mode) {
1366 setColorFilter(new PorterDuffColorFilter(color, mode));
1367 }
1368
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001369 /**
1370 * Set a tinting option for the image. Assumes
1371 * {@link PorterDuff.Mode#SRC_ATOP} blending mode.
1372 *
1373 * @param color Color tint to apply.
1374 * @attr ref android.R.styleable#ImageView_tint
1375 */
1376 @RemotableViewMethod
1377 public final void setColorFilter(int color) {
1378 setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
1379 }
1380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 public final void clearColorFilter() {
1382 setColorFilter(null);
1383 }
Philip Milneaac722a2012-03-26 13:30:26 -07001384
1385 /**
Adam Powell31049d72013-10-07 12:58:42 -07001386 * @hide Candidate for future API inclusion
1387 */
1388 public final void setXfermode(Xfermode mode) {
1389 if (mXfermode != mode) {
1390 mXfermode = mode;
1391 mColorMod = true;
1392 applyColorMod();
1393 invalidate();
1394 }
1395 }
1396
1397 /**
Philip Milneaac722a2012-03-26 13:30:26 -07001398 * Returns the active color filter for this ImageView.
1399 *
1400 * @return the active color filter for this ImageView
1401 *
1402 * @see #setColorFilter(android.graphics.ColorFilter)
1403 */
1404 public ColorFilter getColorFilter() {
1405 return mColorFilter;
1406 }
1407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 /**
1409 * Apply an arbitrary colorfilter to the image.
1410 *
1411 * @param cf the colorfilter to apply (may be null)
Philip Milneaac722a2012-03-26 13:30:26 -07001412 *
1413 * @see #getColorFilter()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 */
1415 public void setColorFilter(ColorFilter cf) {
1416 if (mColorFilter != cf) {
1417 mColorFilter = cf;
Alan Viverette91174362014-06-17 14:51:45 -07001418 mHasColorFilter = true;
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001419 mColorMod = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 applyColorMod();
1421 invalidate();
1422 }
1423 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001424
Philip Milneaac722a2012-03-26 13:30:26 -07001425 /**
1426 * Returns the alpha that will be applied to the drawable of this ImageView.
1427 *
1428 * @return the alpha that will be applied to the drawable of this ImageView
1429 *
1430 * @see #setImageAlpha(int)
1431 */
1432 public int getImageAlpha() {
1433 return mAlpha;
1434 }
1435
1436 /**
1437 * Sets the alpha value that should be applied to the image.
1438 *
1439 * @param alpha the alpha value that should be applied to the image
1440 *
1441 * @see #getImageAlpha()
1442 */
1443 @RemotableViewMethod
1444 public void setImageAlpha(int alpha) {
1445 setAlpha(alpha);
1446 }
1447
1448 /**
1449 * Sets the alpha value that should be applied to the image.
1450 *
1451 * @param alpha the alpha value that should be applied to the image
1452 *
1453 * @deprecated use #setImageAlpha(int) instead
1454 */
1455 @Deprecated
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001456 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 public void setAlpha(int alpha) {
1458 alpha &= 0xFF; // keep it legal
1459 if (mAlpha != alpha) {
1460 mAlpha = alpha;
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001461 mColorMod = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 applyColorMod();
1463 invalidate();
1464 }
1465 }
1466
1467 private void applyColorMod() {
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001468 // Only mutate and apply when modifications have occurred. This should
1469 // not reset the mColorMod flag, since these filters need to be
1470 // re-applied if the Drawable is changed.
1471 if (mDrawable != null && mColorMod) {
1472 mDrawable = mDrawable.mutate();
Alan Viverette91174362014-06-17 14:51:45 -07001473 if (mHasColorFilter) {
1474 mDrawable.setColorFilter(mColorFilter);
1475 }
Adam Powell31049d72013-10-07 12:58:42 -07001476 mDrawable.setXfermode(mXfermode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8);
1478 }
1479 }
Adam Powell37419d72011-11-10 11:32:09 -08001480
Alan Viverettee10dd532013-12-12 19:19:51 -08001481 @Override
1482 public boolean isOpaque() {
1483 return super.isOpaque() || mDrawable != null && mXfermode == null
1484 && mDrawable.getOpacity() == PixelFormat.OPAQUE
1485 && mAlpha * mViewAlphaScale >> 8 == 255
1486 && isFilledByImage();
1487 }
1488
1489 private boolean isFilledByImage() {
1490 if (mDrawable == null) {
1491 return false;
1492 }
1493
1494 final Rect bounds = mDrawable.getBounds();
1495 final Matrix matrix = mDrawMatrix;
1496 if (matrix == null) {
1497 return bounds.left <= 0 && bounds.top <= 0 && bounds.right >= getWidth()
1498 && bounds.bottom >= getHeight();
1499 } else if (matrix.rectStaysRect()) {
1500 final RectF boundsSrc = mTempSrc;
1501 final RectF boundsDst = mTempDst;
1502 boundsSrc.set(bounds);
1503 matrix.mapRect(boundsDst, boundsSrc);
1504 return boundsDst.left <= 0 && boundsDst.top <= 0 && boundsDst.right >= getWidth()
1505 && boundsDst.bottom >= getHeight();
1506 } else {
1507 // If the matrix doesn't map to a rectangle, assume the worst.
1508 return false;
1509 }
1510 }
1511
Adam Powell37419d72011-11-10 11:32:09 -08001512 @Override
Adam Powell9c146bf2016-03-15 17:35:00 -07001513 public void onVisibilityAggregated(boolean isVisible) {
1514 super.onVisibilityAggregated(isVisible);
Adam Powell37419d72011-11-10 11:32:09 -08001515 if (mDrawable != null) {
Adam Powell9c146bf2016-03-15 17:35:00 -07001516 mDrawable.setVisible(isVisible, false);
Adam Powell37419d72011-11-10 11:32:09 -08001517 }
1518 }
1519
1520 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -08001521 public CharSequence getAccessibilityClassName() {
1522 return ImageView.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08001523 }
Siva Velusamy94a6d152015-05-05 15:07:00 -07001524
1525 /** @hide */
1526 @Override
1527 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
1528 super.encodeProperties(stream);
1529 stream.addProperty("layout:baseline", getBaseline());
1530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531}