blob: 222a040d2b3caf2cddbc888aa584370af70004ba [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 mDrawable.setCallback(null);
915 unscheduleDrawable(mDrawable);
Adam Powell4b2e12c2016-03-31 15:35:35 -0700916 if (isAttachedToWindow()) {
917 mDrawable.setVisible(false, false);
918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
Alan Viverette91174362014-06-17 14:51:45 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 mDrawable = d;
Alan Viverette91174362014-06-17 14:51:45 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 if (d != null) {
924 d.setCallback(this);
Alan Viverette91174362014-06-17 14:51:45 -0700925 d.setLayoutDirection(getLayoutDirection());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 if (d.isStateful()) {
927 d.setState(getDrawableState());
928 }
Adam Powellc5874092016-03-17 16:27:24 -0700929 if (isAttachedToWindow()) {
930 d.setVisible(getWindowVisibility() == VISIBLE && isShown(), true);
931 }
Alan Viverette91174362014-06-17 14:51:45 -0700932 d.setLevel(mLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 mDrawableWidth = d.getIntrinsicWidth();
934 mDrawableHeight = d.getIntrinsicHeight();
Alan Viverettea4264452014-07-28 16:02:55 -0700935 applyImageTint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 applyColorMod();
Alan Viveretted5133792014-10-28 14:41:36 -0700937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 configureBounds();
Romain Guy9e648c42011-08-17 20:04:27 -0700939 } else {
940 mDrawableWidth = mDrawableHeight = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942 }
943
944 private void resizeFromDrawable() {
Alan Viverette6aa92d12015-08-25 13:19:25 -0400945 final Drawable d = mDrawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 if (d != null) {
947 int w = d.getIntrinsicWidth();
948 if (w < 0) w = mDrawableWidth;
949 int h = d.getIntrinsicHeight();
950 if (h < 0) h = mDrawableHeight;
951 if (w != mDrawableWidth || h != mDrawableHeight) {
952 mDrawableWidth = w;
953 mDrawableHeight = h;
954 requestLayout();
955 }
956 }
957 }
958
Fabrice Di Meglio3f5a90b2013-06-24 19:22:25 -0700959 @Override
960 public void onRtlPropertiesChanged(int layoutDirection) {
961 super.onRtlPropertiesChanged(layoutDirection);
962
963 if (mDrawable != null) {
964 mDrawable.setLayoutDirection(layoutDirection);
965 }
966 }
967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 private static final Matrix.ScaleToFit[] sS2FArray = {
969 Matrix.ScaleToFit.FILL,
970 Matrix.ScaleToFit.START,
971 Matrix.ScaleToFit.CENTER,
972 Matrix.ScaleToFit.END
973 };
974
975 private static Matrix.ScaleToFit scaleTypeToScaleToFit(ScaleType st) {
976 // ScaleToFit enum to their corresponding Matrix.ScaleToFit values
977 return sS2FArray[st.nativeInt - 1];
Alan Viverette6aa92d12015-08-25 13:19:25 -0400978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979
980 @Override
981 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
982 resolveUri();
983 int w;
984 int h;
Alan Viverette6aa92d12015-08-25 13:19:25 -0400985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 // Desired aspect ratio of the view's contents (not including padding)
987 float desiredAspect = 0.0f;
Alan Viverette6aa92d12015-08-25 13:19:25 -0400988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 // We are allowed to change the view's width
990 boolean resizeWidth = false;
Alan Viverette6aa92d12015-08-25 13:19:25 -0400991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 // We are allowed to change the view's height
993 boolean resizeHeight = false;
Alan Viverette6aa92d12015-08-25 13:19:25 -0400994
Adam Powell2a0e99d2011-08-04 10:55:03 -0700995 final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
996 final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (mDrawable == null) {
999 // If no drawable, its intrinsic size is 0.
1000 mDrawableWidth = -1;
1001 mDrawableHeight = -1;
1002 w = h = 0;
1003 } else {
1004 w = mDrawableWidth;
1005 h = mDrawableHeight;
1006 if (w <= 0) w = 1;
1007 if (h <= 0) h = 1;
Romain Guy9e648c42011-08-17 20:04:27 -07001008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 // We are supposed to adjust view bounds to match the aspect
1010 // ratio of our drawable. See if that is possible.
1011 if (mAdjustViewBounds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
1013 resizeHeight = heightSpecMode != MeasureSpec.EXACTLY;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001014
Romain Guy9e648c42011-08-17 20:04:27 -07001015 desiredAspect = (float) w / (float) h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
1017 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001018
1019 final int pleft = mPaddingLeft;
1020 final int pright = mPaddingRight;
1021 final int ptop = mPaddingTop;
1022 final int pbottom = mPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023
1024 int widthSize;
1025 int heightSize;
1026
1027 if (resizeWidth || resizeHeight) {
1028 /* If we get here, it means we want to resize to match the
1029 drawables aspect ratio, and we have the freedom to change at
Alan Viverette6aa92d12015-08-25 13:19:25 -04001030 least one dimension.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032
1033 // Get the max possible width given our constraints
Romain Guy9e648c42011-08-17 20:04:27 -07001034 widthSize = resolveAdjustedSize(w + pleft + pright, mMaxWidth, widthMeasureSpec);
1035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 // Get the max possible height given our constraints
Romain Guy9e648c42011-08-17 20:04:27 -07001037 heightSize = resolveAdjustedSize(h + ptop + pbottom, mMaxHeight, heightMeasureSpec);
1038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 if (desiredAspect != 0.0f) {
1040 // See what our actual aspect ratio is
Alan Viverette6aa92d12015-08-25 13:19:25 -04001041 final float actualAspect = (float)(widthSize - pleft - pright) /
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 (heightSize - ptop - pbottom);
Alan Viverette6aa92d12015-08-25 13:19:25 -04001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 if (Math.abs(actualAspect - desiredAspect) > 0.0000001) {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 boolean done = false;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 // Try adjusting width to be proportional to height
1049 if (resizeWidth) {
Romain Guy9e648c42011-08-17 20:04:27 -07001050 int newWidth = (int)(desiredAspect * (heightSize - ptop - pbottom)) +
1051 pleft + pright;
Adam Powelld5edc772012-09-26 15:21:39 -07001052
1053 // Allow the width to outgrow its original estimate if height is fixed.
Adam Powell7da4b732012-12-07 15:28:33 -08001054 if (!resizeHeight && !mAdjustViewBoundsCompat) {
Adam Powelld5edc772012-09-26 15:21:39 -07001055 widthSize = resolveAdjustedSize(newWidth, mMaxWidth, widthMeasureSpec);
1056 }
1057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 if (newWidth <= widthSize) {
1059 widthSize = newWidth;
1060 done = true;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 // Try adjusting height to be proportional to width
1065 if (!done && resizeHeight) {
Romain Guy9e648c42011-08-17 20:04:27 -07001066 int newHeight = (int)((widthSize - pleft - pright) / desiredAspect) +
1067 ptop + pbottom;
Adam Powelld5edc772012-09-26 15:21:39 -07001068
1069 // Allow the height to outgrow its original estimate if width is fixed.
Adam Powell7da4b732012-12-07 15:28:33 -08001070 if (!resizeWidth && !mAdjustViewBoundsCompat) {
Adam Powelld5edc772012-09-26 15:21:39 -07001071 heightSize = resolveAdjustedSize(newHeight, mMaxHeight,
1072 heightMeasureSpec);
1073 }
1074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 if (newHeight <= heightSize) {
1076 heightSize = newHeight;
Dianne Hackborn189ee182010-12-02 21:48:53 -08001077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079 }
1080 }
1081 } else {
1082 /* We are either don't want to preserve the drawables aspect ratio,
1083 or we are not allowed to change view dimensions. Just measure in
1084 the normal way.
1085 */
1086 w += pleft + pright;
1087 h += ptop + pbottom;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 w = Math.max(w, getSuggestedMinimumWidth());
1090 h = Math.max(h, getSuggestedMinimumHeight());
1091
Dianne Hackborn189ee182010-12-02 21:48:53 -08001092 widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);
1093 heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
1096 setMeasuredDimension(widthSize, heightSize);
1097 }
1098
1099 private int resolveAdjustedSize(int desiredSize, int maxSize,
1100 int measureSpec) {
1101 int result = desiredSize;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001102 final int specMode = MeasureSpec.getMode(measureSpec);
1103 final int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 switch (specMode) {
1105 case MeasureSpec.UNSPECIFIED:
1106 /* Parent says we can be as big as we want. Just don't be larger
1107 than max size imposed on ourselves.
1108 */
1109 result = Math.min(desiredSize, maxSize);
1110 break;
1111 case MeasureSpec.AT_MOST:
Alan Viverette6aa92d12015-08-25 13:19:25 -04001112 // Parent says we can be as big as we want, up to specSize.
1113 // Don't be larger than specSize, and don't be larger than
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 // the max size imposed on ourselves.
1115 result = Math.min(Math.min(desiredSize, specSize), maxSize);
1116 break;
1117 case MeasureSpec.EXACTLY:
1118 // No choice. Do what we are told.
1119 result = specSize;
1120 break;
1121 }
1122 return result;
1123 }
1124
1125 @Override
1126 protected boolean setFrame(int l, int t, int r, int b) {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001127 final boolean changed = super.setFrame(l, t, r, b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 mHaveFrame = true;
1129 configureBounds();
1130 return changed;
1131 }
1132
1133 private void configureBounds() {
1134 if (mDrawable == null || !mHaveFrame) {
1135 return;
1136 }
1137
Alan Viverette6aa92d12015-08-25 13:19:25 -04001138 final int dwidth = mDrawableWidth;
1139 final int dheight = mDrawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140
Alan Viverette6aa92d12015-08-25 13:19:25 -04001141 final int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
1142 final int vheight = getHeight() - mPaddingTop - mPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143
Alan Viverette6aa92d12015-08-25 13:19:25 -04001144 final boolean fits = (dwidth < 0 || vwidth == dwidth)
1145 && (dheight < 0 || vheight == dheight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146
1147 if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) {
1148 /* If the drawable has no intrinsic size, or we're told to
1149 scaletofit, then we just fill our entire view.
1150 */
1151 mDrawable.setBounds(0, 0, vwidth, vheight);
1152 mDrawMatrix = null;
1153 } else {
1154 // We need to do the scaling ourself, so have the drawable
1155 // use its native size.
1156 mDrawable.setBounds(0, 0, dwidth, dheight);
1157
1158 if (ScaleType.MATRIX == mScaleType) {
1159 // Use the specified matrix as-is.
1160 if (mMatrix.isIdentity()) {
1161 mDrawMatrix = null;
1162 } else {
1163 mDrawMatrix = mMatrix;
1164 }
1165 } else if (fits) {
1166 // The bitmap fits exactly, no transform needed.
1167 mDrawMatrix = null;
1168 } else if (ScaleType.CENTER == mScaleType) {
1169 // Center bitmap in view, no scaling.
1170 mDrawMatrix = mMatrix;
Adam Powell14d1f1382015-06-04 12:56:00 -07001171 mDrawMatrix.setTranslate(Math.round((vwidth - dwidth) * 0.5f),
1172 Math.round((vheight - dheight) * 0.5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 } else if (ScaleType.CENTER_CROP == mScaleType) {
1174 mDrawMatrix = mMatrix;
1175
1176 float scale;
1177 float dx = 0, dy = 0;
1178
1179 if (dwidth * vheight > vwidth * dheight) {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001180 scale = (float) vheight / (float) dheight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 dx = (vwidth - dwidth * scale) * 0.5f;
1182 } else {
1183 scale = (float) vwidth / (float) dwidth;
1184 dy = (vheight - dheight * scale) * 0.5f;
1185 }
1186
1187 mDrawMatrix.setScale(scale, scale);
Adam Powell14d1f1382015-06-04 12:56:00 -07001188 mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 } else if (ScaleType.CENTER_INSIDE == mScaleType) {
1190 mDrawMatrix = mMatrix;
1191 float scale;
1192 float dx;
1193 float dy;
Alan Viverette6aa92d12015-08-25 13:19:25 -04001194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 if (dwidth <= vwidth && dheight <= vheight) {
1196 scale = 1.0f;
1197 } else {
Romain Guy9e648c42011-08-17 20:04:27 -07001198 scale = Math.min((float) vwidth / (float) dwidth,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 (float) vheight / (float) dheight);
1200 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001201
Adam Powell14d1f1382015-06-04 12:56:00 -07001202 dx = Math.round((vwidth - dwidth * scale) * 0.5f);
1203 dy = Math.round((vheight - dheight * scale) * 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204
1205 mDrawMatrix.setScale(scale, scale);
1206 mDrawMatrix.postTranslate(dx, dy);
1207 } else {
1208 // Generate the required transform.
1209 mTempSrc.set(0, 0, dwidth, dheight);
1210 mTempDst.set(0, 0, vwidth, vheight);
Alan Viverette6aa92d12015-08-25 13:19:25 -04001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 mDrawMatrix = mMatrix;
Romain Guy9e648c42011-08-17 20:04:27 -07001213 mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215 }
1216 }
1217
1218 @Override
1219 protected void drawableStateChanged() {
1220 super.drawableStateChanged();
Alan Viverette6aa92d12015-08-25 13:19:25 -04001221
Alan Viverettead0020f2015-09-04 10:10:42 -04001222 final Drawable drawable = mDrawable;
1223 if (drawable != null && drawable.isStateful()
1224 && drawable.setState(getDrawableState())) {
1225 invalidateDrawable(drawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
1228
Alan Viverettecebc6ba2014-06-13 15:52:13 -07001229 @Override
Alan Viverette8de14942014-06-18 18:05:15 -07001230 public void drawableHotspotChanged(float x, float y) {
1231 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -07001232
1233 if (mDrawable != null) {
1234 mDrawable.setHotspot(x, y);
1235 }
1236 }
1237
George Mount990205e2014-06-24 09:36:18 -07001238 /** @hide */
1239 public void animateTransform(Matrix matrix) {
George Mountf6c763d2014-09-25 15:13:15 -07001240 if (mDrawable == null) {
1241 return;
1242 }
George Mount990205e2014-06-24 09:36:18 -07001243 if (matrix == null) {
1244 mDrawable.setBounds(0, 0, getWidth(), getHeight());
1245 } else {
1246 mDrawable.setBounds(0, 0, mDrawableWidth, mDrawableHeight);
1247 if (mDrawMatrix == null) {
1248 mDrawMatrix = new Matrix();
1249 }
1250 mDrawMatrix.set(matrix);
1251 }
1252 invalidate();
1253 }
1254
Alan Viverettecebc6ba2014-06-13 15:52:13 -07001255 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 protected void onDraw(Canvas canvas) {
1257 super.onDraw(canvas);
1258
1259 if (mDrawable == null) {
1260 return; // couldn't resolve the URI
1261 }
1262
1263 if (mDrawableWidth == 0 || mDrawableHeight == 0) {
1264 return; // nothing to draw (empty bounds)
1265 }
1266
1267 if (mDrawMatrix == null && mPaddingTop == 0 && mPaddingLeft == 0) {
1268 mDrawable.draw(canvas);
1269 } else {
Alan Viverette6aa92d12015-08-25 13:19:25 -04001270 final int saveCount = canvas.getSaveCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 canvas.save();
Alan Viverette6aa92d12015-08-25 13:19:25 -04001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 if (mCropToPadding) {
1274 final int scrollX = mScrollX;
1275 final int scrollY = mScrollY;
1276 canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop,
1277 scrollX + mRight - mLeft - mPaddingRight,
1278 scrollY + mBottom - mTop - mPaddingBottom);
1279 }
Alan Viverette6aa92d12015-08-25 13:19:25 -04001280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 canvas.translate(mPaddingLeft, mPaddingTop);
1282
1283 if (mDrawMatrix != null) {
1284 canvas.concat(mDrawMatrix);
1285 }
1286 mDrawable.draw(canvas);
1287 canvas.restoreToCount(saveCount);
1288 }
1289 }
1290
Joe Onoratofd52b182010-11-10 18:00:52 -08001291 /**
1292 * <p>Return the offset of the widget's text baseline from the widget's top
1293 * boundary. </p>
1294 *
1295 * @return the offset of the baseline within the widget's bounds or -1
1296 * if baseline alignment is not supported.
1297 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 @Override
Joe Onoratofd52b182010-11-10 18:00:52 -08001299 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 public int getBaseline() {
Joe Onoratofd52b182010-11-10 18:00:52 -08001301 if (mBaselineAlignBottom) {
1302 return getMeasuredHeight();
1303 } else {
1304 return mBaseline;
1305 }
1306 }
1307
1308 /**
1309 * <p>Set the offset of the widget's text baseline from the widget's top
Romain Guy9fc27812011-04-27 14:21:41 -07001310 * boundary. This value is overridden by the {@link #setBaselineAlignBottom(boolean)}
Joe Onoratofd52b182010-11-10 18:00:52 -08001311 * property.</p>
1312 *
1313 * @param baseline The baseline to use, or -1 if none is to be provided.
1314 *
Alan Viverette6aa92d12015-08-25 13:19:25 -04001315 * @see #setBaseline(int)
Joe Onoratofd52b182010-11-10 18:00:52 -08001316 * @attr ref android.R.styleable#ImageView_baseline
1317 */
1318 public void setBaseline(int baseline) {
1319 if (mBaseline != baseline) {
1320 mBaseline = baseline;
1321 requestLayout();
1322 }
1323 }
1324
1325 /**
1326 * Set whether to set the baseline of this view to the bottom of the view.
1327 * Setting this value overrides any calls to setBaseline.
1328 *
1329 * @param aligned If true, the image view will be baseline aligned with
1330 * based on its bottom edge.
1331 *
1332 * @attr ref android.R.styleable#ImageView_baselineAlignBottom
1333 */
1334 public void setBaselineAlignBottom(boolean aligned) {
1335 if (mBaselineAlignBottom != aligned) {
1336 mBaselineAlignBottom = aligned;
1337 requestLayout();
1338 }
1339 }
1340
1341 /**
1342 * Return whether this view's baseline will be considered the bottom of the view.
1343 *
1344 * @see #setBaselineAlignBottom(boolean)
1345 */
1346 public boolean getBaselineAlignBottom() {
1347 return mBaselineAlignBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Set a tinting option for the image.
Alan Viverette6aa92d12015-08-25 13:19:25 -04001352 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 * @param color Color tint to apply.
1354 * @param mode How to apply the color. The standard mode is
1355 * {@link PorterDuff.Mode#SRC_ATOP}
Alan Viverette6aa92d12015-08-25 13:19:25 -04001356 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 * @attr ref android.R.styleable#ImageView_tint
1358 */
1359 public final void setColorFilter(int color, PorterDuff.Mode mode) {
1360 setColorFilter(new PorterDuffColorFilter(color, mode));
1361 }
1362
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001363 /**
1364 * Set a tinting option for the image. Assumes
1365 * {@link PorterDuff.Mode#SRC_ATOP} blending mode.
1366 *
1367 * @param color Color tint to apply.
1368 * @attr ref android.R.styleable#ImageView_tint
1369 */
1370 @RemotableViewMethod
1371 public final void setColorFilter(int color) {
1372 setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
1373 }
1374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 public final void clearColorFilter() {
1376 setColorFilter(null);
1377 }
Philip Milneaac722a2012-03-26 13:30:26 -07001378
1379 /**
Adam Powell31049d72013-10-07 12:58:42 -07001380 * @hide Candidate for future API inclusion
1381 */
1382 public final void setXfermode(Xfermode mode) {
1383 if (mXfermode != mode) {
1384 mXfermode = mode;
1385 mColorMod = true;
1386 applyColorMod();
1387 invalidate();
1388 }
1389 }
1390
1391 /**
Philip Milneaac722a2012-03-26 13:30:26 -07001392 * Returns the active color filter for this ImageView.
1393 *
1394 * @return the active color filter for this ImageView
1395 *
1396 * @see #setColorFilter(android.graphics.ColorFilter)
1397 */
1398 public ColorFilter getColorFilter() {
1399 return mColorFilter;
1400 }
1401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 /**
1403 * Apply an arbitrary colorfilter to the image.
1404 *
1405 * @param cf the colorfilter to apply (may be null)
Philip Milneaac722a2012-03-26 13:30:26 -07001406 *
1407 * @see #getColorFilter()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 */
1409 public void setColorFilter(ColorFilter cf) {
1410 if (mColorFilter != cf) {
1411 mColorFilter = cf;
Alan Viverette91174362014-06-17 14:51:45 -07001412 mHasColorFilter = true;
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001413 mColorMod = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 applyColorMod();
1415 invalidate();
1416 }
1417 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001418
Philip Milneaac722a2012-03-26 13:30:26 -07001419 /**
1420 * Returns the alpha that will be applied to the drawable of this ImageView.
1421 *
1422 * @return the alpha that will be applied to the drawable of this ImageView
1423 *
1424 * @see #setImageAlpha(int)
1425 */
1426 public int getImageAlpha() {
1427 return mAlpha;
1428 }
1429
1430 /**
1431 * Sets the alpha value that should be applied to the image.
1432 *
1433 * @param alpha the alpha value that should be applied to the image
1434 *
1435 * @see #getImageAlpha()
1436 */
1437 @RemotableViewMethod
1438 public void setImageAlpha(int alpha) {
1439 setAlpha(alpha);
1440 }
1441
1442 /**
1443 * Sets the alpha value that should be applied to the image.
1444 *
1445 * @param alpha the alpha value that should be applied to the image
1446 *
1447 * @deprecated use #setImageAlpha(int) instead
1448 */
1449 @Deprecated
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001450 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 public void setAlpha(int alpha) {
1452 alpha &= 0xFF; // keep it legal
1453 if (mAlpha != alpha) {
1454 mAlpha = alpha;
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001455 mColorMod = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 applyColorMod();
1457 invalidate();
1458 }
1459 }
1460
1461 private void applyColorMod() {
Jeff Sharkey2b95c242010-02-08 17:40:30 -08001462 // Only mutate and apply when modifications have occurred. This should
1463 // not reset the mColorMod flag, since these filters need to be
1464 // re-applied if the Drawable is changed.
1465 if (mDrawable != null && mColorMod) {
1466 mDrawable = mDrawable.mutate();
Alan Viverette91174362014-06-17 14:51:45 -07001467 if (mHasColorFilter) {
1468 mDrawable.setColorFilter(mColorFilter);
1469 }
Adam Powell31049d72013-10-07 12:58:42 -07001470 mDrawable.setXfermode(mXfermode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8);
1472 }
1473 }
Adam Powell37419d72011-11-10 11:32:09 -08001474
Alan Viverettee10dd532013-12-12 19:19:51 -08001475 @Override
1476 public boolean isOpaque() {
1477 return super.isOpaque() || mDrawable != null && mXfermode == null
1478 && mDrawable.getOpacity() == PixelFormat.OPAQUE
1479 && mAlpha * mViewAlphaScale >> 8 == 255
1480 && isFilledByImage();
1481 }
1482
1483 private boolean isFilledByImage() {
1484 if (mDrawable == null) {
1485 return false;
1486 }
1487
1488 final Rect bounds = mDrawable.getBounds();
1489 final Matrix matrix = mDrawMatrix;
1490 if (matrix == null) {
1491 return bounds.left <= 0 && bounds.top <= 0 && bounds.right >= getWidth()
1492 && bounds.bottom >= getHeight();
1493 } else if (matrix.rectStaysRect()) {
1494 final RectF boundsSrc = mTempSrc;
1495 final RectF boundsDst = mTempDst;
1496 boundsSrc.set(bounds);
1497 matrix.mapRect(boundsDst, boundsSrc);
1498 return boundsDst.left <= 0 && boundsDst.top <= 0 && boundsDst.right >= getWidth()
1499 && boundsDst.bottom >= getHeight();
1500 } else {
1501 // If the matrix doesn't map to a rectangle, assume the worst.
1502 return false;
1503 }
1504 }
1505
Adam Powell37419d72011-11-10 11:32:09 -08001506 @Override
Adam Powell9c146bf2016-03-15 17:35:00 -07001507 public void onVisibilityAggregated(boolean isVisible) {
1508 super.onVisibilityAggregated(isVisible);
Adam Powell37419d72011-11-10 11:32:09 -08001509 if (mDrawable != null) {
Adam Powell9c146bf2016-03-15 17:35:00 -07001510 mDrawable.setVisible(isVisible, false);
Adam Powell37419d72011-11-10 11:32:09 -08001511 }
1512 }
1513
1514 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -08001515 public CharSequence getAccessibilityClassName() {
1516 return ImageView.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08001517 }
Siva Velusamy94a6d152015-05-05 15:07:00 -07001518
1519 /** @hide */
1520 @Override
1521 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
1522 super.encodeProperties(stream);
1523 stream.addProperty("layout:baseline", getBaseline());
1524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525}