The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.DrawableRes; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jiaquan He | 1dd48d0 | 2017-05-01 14:18:39 -0700 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
Bjorn Bringert | 0f8555b | 2009-11-27 12:42:28 +0000 | [diff] [blame] | 24 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 26 | import android.content.res.ColorStateList; |
Leon Scroggins III | 046a99e | 2018-01-31 14:59:29 -0500 | [diff] [blame] | 27 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
| 29 | import android.graphics.Bitmap; |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 30 | import android.graphics.BlendMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.graphics.Canvas; |
| 32 | import android.graphics.ColorFilter; |
Leon Scroggins III | 046a99e | 2018-01-31 14:59:29 -0500 | [diff] [blame] | 33 | import android.graphics.ImageDecoder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.graphics.Matrix; |
Alan Viverette | e10dd53 | 2013-12-12 19:19:51 -0800 | [diff] [blame] | 35 | import android.graphics.PixelFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.graphics.PorterDuff; |
| 37 | import android.graphics.PorterDuffColorFilter; |
Alan Viverette | e10dd53 | 2013-12-12 19:19:51 -0800 | [diff] [blame] | 38 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.graphics.RectF; |
Adam Powell | 31049d7 | 2013-10-07 12:58:42 -0700 | [diff] [blame] | 40 | import android.graphics.Xfermode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.graphics.drawable.BitmapDrawable; |
| 42 | import android.graphics.drawable.Drawable; |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 43 | import android.graphics.drawable.Icon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.net.Uri; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 45 | import android.os.Build; |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 46 | import android.os.Handler; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 47 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.util.AttributeSet; |
| 49 | import android.util.Log; |
Jeff Sharkey | 2b95c24 | 2010-02-08 17:40:30 -0800 | [diff] [blame] | 50 | import android.view.RemotableViewMethod; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.view.View; |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 52 | import android.view.ViewDebug; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 53 | import android.view.ViewHierarchyEncoder; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 54 | import android.view.accessibility.AccessibilityEvent; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 55 | import android.view.inspector.InspectableProperty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.widget.RemoteViews.RemoteView; |
| 57 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 58 | import com.android.internal.R; |
| 59 | |
Romain Guy | 3614394 | 2012-12-03 10:10:22 -0800 | [diff] [blame] | 60 | import java.io.IOException; |
Romain Guy | 3614394 | 2012-12-03 10:10:22 -0800 | [diff] [blame] | 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | /** |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 63 | * Displays image resources, for example {@link android.graphics.Bitmap} |
| 64 | * or {@link android.graphics.drawable.Drawable} resources. |
Kevin Hufnagle | b358694 | 2019-08-14 17:50:17 -0700 | [diff] [blame] | 65 | * ImageView is also commonly used to |
| 66 | * <a href="#setImageTintMode(android.graphics.PorterDuff.Mode)">apply tints to an image</a> and |
| 67 | * handle <a href="#setScaleType(android.widget.ImageView.ScaleType)">image scaling</a>. |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 68 | * |
| 69 | * <p> |
| 70 | * The following XML snippet is a common example of using an ImageView to display an image resource: |
| 71 | * </p> |
| 72 | * <pre> |
| 73 | * <LinearLayout |
| 74 | * xmlns:android="http://schemas.android.com/apk/res/android" |
| 75 | * android:layout_width="match_parent" |
| 76 | * android:layout_height="match_parent"> |
| 77 | * <ImageView |
| 78 | * android:layout_width="wrap_content" |
| 79 | * android:layout_height="wrap_content" |
Kevin Hufnagle | b358694 | 2019-08-14 17:50:17 -0700 | [diff] [blame] | 80 | * android:src="@drawable/my_image" |
| 81 | * android:contentDescription="@string/my_image_description" |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 82 | * /> |
| 83 | * </LinearLayout> |
| 84 | * </pre> |
| 85 | * |
| 86 | * <p> |
| 87 | * To learn more about Drawables, see: <a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>. |
Laura Davis | 3175696 | 2018-06-18 16:09:32 -0700 | [diff] [blame] | 88 | * To learn more about working with Bitmaps, see: <a href="{@docRoot}topic/performance/graphics/index.html">Handling Bitmaps</a>. |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 89 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * |
| 91 | * @attr ref android.R.styleable#ImageView_adjustViewBounds |
| 92 | * @attr ref android.R.styleable#ImageView_src |
| 93 | * @attr ref android.R.styleable#ImageView_maxWidth |
| 94 | * @attr ref android.R.styleable#ImageView_maxHeight |
| 95 | * @attr ref android.R.styleable#ImageView_tint |
| 96 | * @attr ref android.R.styleable#ImageView_scaleType |
| 97 | * @attr ref android.R.styleable#ImageView_cropToPadding |
| 98 | */ |
| 99 | @RemoteView |
| 100 | public class ImageView extends View { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 101 | private static final String LOG_TAG = "ImageView"; |
| 102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | // settable by the client |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 104 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | private Uri mUri; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 106 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | private int mResource = 0; |
| 108 | private Matrix mMatrix; |
| 109 | private ScaleType mScaleType; |
| 110 | private boolean mHaveFrame = false; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 111 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | private boolean mAdjustViewBounds = false; |
Clara Bayarri | 9e6588d | 2018-11-27 11:39:58 +0000 | [diff] [blame] | 113 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | private int mMaxWidth = Integer.MAX_VALUE; |
Clara Bayarri | 9e6588d | 2018-11-27 11:39:58 +0000 | [diff] [blame] | 115 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | private int mMaxHeight = Integer.MAX_VALUE; |
| 117 | |
| 118 | // these are applied to the drawable |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 119 | private ColorFilter mColorFilter = null; |
| 120 | private boolean mHasColorFilter = false; |
Adam Powell | 31049d7 | 2013-10-07 12:58:42 -0700 | [diff] [blame] | 121 | private Xfermode mXfermode; |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 122 | private boolean mHasXfermode = false; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 123 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | private int mAlpha = 255; |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 125 | private boolean mHasAlpha = false; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 126 | private final int mViewAlphaScale = 256; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 128 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | private Drawable mDrawable = null; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 130 | @UnsupportedAppUsage |
Chris Craik | 82af13e | 2015-07-14 13:36:27 -0700 | [diff] [blame] | 131 | private BitmapDrawable mRecycleableBitmapDrawable = null; |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 132 | private ColorStateList mDrawableTintList = null; |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 133 | private BlendMode mDrawableBlendMode = null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 134 | private boolean mHasDrawableTint = false; |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 135 | private boolean mHasDrawableBlendMode = false; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | private int[] mState = null; |
| 138 | private boolean mMergeState = false; |
| 139 | private int mLevel = 0; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 140 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | private int mDrawableWidth; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 142 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | private int mDrawableHeight; |
Rahul Ravikumar | cd946ba | 2019-02-08 16:32:40 -0800 | [diff] [blame] | 144 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 124051687) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | private Matrix mDrawMatrix = null; |
| 146 | |
| 147 | // Avoid allocations... |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 148 | private final RectF mTempSrc = new RectF(); |
| 149 | private final RectF mTempDst = new RectF(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 151 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | private boolean mCropToPadding; |
| 153 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 154 | private int mBaseline = -1; |
| 155 | private boolean mBaselineAlignBottom = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 157 | /** Compatibility modes dependent on targetSdkVersion of the app. */ |
| 158 | private static boolean sCompatDone; |
| 159 | |
| 160 | /** AdjustViewBounds behavior will be in compatibility mode for older apps. */ |
| 161 | private static boolean sCompatAdjustViewBounds; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 162 | |
Alan Viverette | 270a342 | 2015-08-25 13:25:16 -0400 | [diff] [blame] | 163 | /** Whether to pass Resources when creating the source from a stream. */ |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 164 | private static boolean sCompatUseCorrectStreamDensity; |
| 165 | |
| 166 | /** Whether to use pre-Nougat drawable visibility dispatching conditions. */ |
| 167 | private static boolean sCompatDrawableVisibilityDispatch; |
Alan Viverette | 270a342 | 2015-08-25 13:25:16 -0400 | [diff] [blame] | 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | private static final ScaleType[] sScaleTypeArray = { |
| 170 | ScaleType.MATRIX, |
| 171 | ScaleType.FIT_XY, |
| 172 | ScaleType.FIT_START, |
| 173 | ScaleType.FIT_CENTER, |
| 174 | ScaleType.FIT_END, |
| 175 | ScaleType.CENTER, |
| 176 | ScaleType.CENTER_CROP, |
| 177 | ScaleType.CENTER_INSIDE |
| 178 | }; |
| 179 | |
| 180 | public ImageView(Context context) { |
| 181 | super(context); |
| 182 | initImageView(); |
| 183 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 184 | |
Scott Kennedy | 76c382e | 2015-02-10 23:15:39 -0800 | [diff] [blame] | 185 | public ImageView(Context context, @Nullable AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | this(context, attrs, 0); |
| 187 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 188 | |
Scott Kennedy | 76c382e | 2015-02-10 23:15:39 -0800 | [diff] [blame] | 189 | public ImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 190 | this(context, attrs, defStyleAttr, 0); |
| 191 | } |
| 192 | |
Scott Kennedy | 76c382e | 2015-02-10 23:15:39 -0800 | [diff] [blame] | 193 | public ImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, |
| 194 | int defStyleRes) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 195 | super(context, attrs, defStyleAttr, defStyleRes); |
| 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | initImageView(); |
| 198 | |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 199 | // ImageView is not important by default, unless app developer overrode attribute. |
| 200 | if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) { |
| 201 | setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_NO); |
| 202 | } |
| 203 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 204 | final TypedArray a = context.obtainStyledAttributes( |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 205 | attrs, R.styleable.ImageView, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 206 | saveAttributeDataForStyleable(context, R.styleable.ImageView, |
| 207 | attrs, a, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 209 | final Drawable d = a.getDrawable(R.styleable.ImageView_src); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | if (d != null) { |
| 211 | setImageDrawable(d); |
| 212 | } |
| 213 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 214 | mBaselineAlignBottom = a.getBoolean(R.styleable.ImageView_baselineAlignBottom, false); |
| 215 | mBaseline = a.getDimensionPixelSize(R.styleable.ImageView_baseline, -1); |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 216 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 217 | setAdjustViewBounds(a.getBoolean(R.styleable.ImageView_adjustViewBounds, false)); |
| 218 | setMaxWidth(a.getDimensionPixelSize(R.styleable.ImageView_maxWidth, Integer.MAX_VALUE)); |
| 219 | setMaxHeight(a.getDimensionPixelSize(R.styleable.ImageView_maxHeight, Integer.MAX_VALUE)); |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 220 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 221 | final int index = a.getInt(R.styleable.ImageView_scaleType, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | if (index >= 0) { |
| 223 | setScaleType(sScaleTypeArray[index]); |
| 224 | } |
| 225 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 226 | if (a.hasValue(R.styleable.ImageView_tint)) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 227 | mDrawableTintList = a.getColorStateList(R.styleable.ImageView_tint); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 228 | mHasDrawableTint = true; |
| 229 | |
Alan Viverette | 38f93bc | 2014-09-15 16:25:09 -0700 | [diff] [blame] | 230 | // Prior to L, this attribute would always set a color filter with |
| 231 | // blending mode SRC_ATOP. Preserve that default behavior. |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 232 | mDrawableBlendMode = BlendMode.SRC_ATOP; |
| 233 | mHasDrawableBlendMode = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 235 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 236 | if (a.hasValue(R.styleable.ImageView_tintMode)) { |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 237 | mDrawableBlendMode = Drawable.parseBlendMode(a.getInt( |
| 238 | R.styleable.ImageView_tintMode, -1), mDrawableBlendMode); |
| 239 | mHasDrawableBlendMode = true; |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | applyImageTint(); |
| 243 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 244 | final int alpha = a.getInt(R.styleable.ImageView_drawableAlpha, 255); |
Chet Haase | ba1fe8e | 2011-10-15 07:35:51 -0700 | [diff] [blame] | 245 | if (alpha != 255) { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 246 | setImageAlpha(alpha); |
Chet Haase | ba1fe8e | 2011-10-15 07:35:51 -0700 | [diff] [blame] | 247 | } |
| 248 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | mCropToPadding = a.getBoolean( |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 250 | R.styleable.ImageView_cropToPadding, false); |
| 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | a.recycle(); |
| 253 | |
| 254 | //need inflate syntax/reader for matrix |
| 255 | } |
| 256 | |
| 257 | private void initImageView() { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 258 | mMatrix = new Matrix(); |
| 259 | mScaleType = ScaleType.FIT_CENTER; |
Alan Viverette | 270a342 | 2015-08-25 13:25:16 -0400 | [diff] [blame] | 260 | |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 261 | if (!sCompatDone) { |
| 262 | final int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion; |
| 263 | sCompatAdjustViewBounds = targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 264 | sCompatUseCorrectStreamDensity = targetSdkVersion > Build.VERSION_CODES.M; |
| 265 | sCompatDrawableVisibilityDispatch = targetSdkVersion < Build.VERSION_CODES.N; |
| 266 | sCompatDone = true; |
| 267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
Alan Viverette | f6d87ec | 2016-03-11 10:09:14 -0500 | [diff] [blame] | 271 | protected boolean verifyDrawable(@NonNull Drawable dr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | return mDrawable == dr || super.verifyDrawable(dr); |
| 273 | } |
Chet Haase | 8473f5a | 2015-06-17 14:43:30 -0700 | [diff] [blame] | 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 276 | public void jumpDrawablesToCurrentState() { |
| 277 | super.jumpDrawablesToCurrentState(); |
| 278 | if (mDrawable != null) mDrawable.jumpToCurrentState(); |
| 279 | } |
| 280 | |
| 281 | @Override |
Alan Viverette | f6d87ec | 2016-03-11 10:09:14 -0500 | [diff] [blame] | 282 | public void invalidateDrawable(@NonNull Drawable dr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | if (dr == mDrawable) { |
Chet Haase | 8473f5a | 2015-06-17 14:43:30 -0700 | [diff] [blame] | 284 | if (dr != null) { |
| 285 | // update cached drawable dimensions if they've changed |
| 286 | final int w = dr.getIntrinsicWidth(); |
| 287 | final int h = dr.getIntrinsicHeight(); |
| 288 | if (w != mDrawableWidth || h != mDrawableHeight) { |
| 289 | mDrawableWidth = w; |
| 290 | mDrawableHeight = h; |
Alan Viverette | 01320de | 2015-10-20 09:48:58 -0400 | [diff] [blame] | 291 | // updates the matrix, which is dependent on the bounds |
| 292 | configureBounds(); |
Chet Haase | 8473f5a | 2015-06-17 14:43:30 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | /* we invalidate the whole view in this case because it's very |
| 296 | * hard to know where the drawable actually is. This is made |
| 297 | * complicated because of the offsets and transformations that |
| 298 | * can be applied. In theory we could get the drawable's bounds |
| 299 | * and run them through the transformation and offsets, but this |
| 300 | * is probably not worth the effort. |
| 301 | */ |
| 302 | invalidate(); |
| 303 | } else { |
| 304 | super.invalidateDrawable(dr); |
| 305 | } |
| 306 | } |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 307 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | @Override |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 309 | public boolean hasOverlappingRendering() { |
Michael Jurka | 0931a85 | 2013-03-21 16:07:45 +0100 | [diff] [blame] | 310 | return (getBackground() != null && getBackground().getCurrent() != null); |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 313 | /** @hide */ |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 314 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 315 | public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
| 316 | super.onPopulateAccessibilityEventInternal(event); |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 317 | |
| 318 | final CharSequence contentDescription = getContentDescription(); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 319 | if (!TextUtils.isEmpty(contentDescription)) { |
| 320 | event.getText().add(contentDescription); |
| 321 | } |
| 322 | } |
| 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | /** |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 325 | * True when ImageView is adjusting its bounds |
| 326 | * to preserve the aspect ratio of its drawable |
| 327 | * |
| 328 | * @return whether to adjust the bounds of this view |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 329 | * to preserve the original aspect ratio of the drawable |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 330 | * |
| 331 | * @see #setAdjustViewBounds(boolean) |
| 332 | * |
| 333 | * @attr ref android.R.styleable#ImageView_adjustViewBounds |
| 334 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 335 | @InspectableProperty |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 336 | public boolean getAdjustViewBounds() { |
| 337 | return mAdjustViewBounds; |
| 338 | } |
| 339 | |
| 340 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | * Set this to true if you want the ImageView to adjust its bounds |
| 342 | * to preserve the aspect ratio of its drawable. |
Adam Powell | 2c8cc97 | 2012-12-07 18:04:51 -0800 | [diff] [blame] | 343 | * |
| 344 | * <p><strong>Note:</strong> If the application targets API level 17 or lower, |
| 345 | * adjustViewBounds will allow the drawable to shrink the view bounds, but not grow |
| 346 | * to fill available measured space in all cases. This is for compatibility with |
| 347 | * legacy {@link android.view.View.MeasureSpec MeasureSpec} and |
| 348 | * {@link android.widget.RelativeLayout RelativeLayout} behavior.</p> |
| 349 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | * @param adjustViewBounds Whether to adjust the bounds of this view |
Adam Powell | 2c8cc97 | 2012-12-07 18:04:51 -0800 | [diff] [blame] | 351 | * to preserve the original aspect ratio of the drawable. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 352 | * |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 353 | * @see #getAdjustViewBounds() |
| 354 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | * @attr ref android.R.styleable#ImageView_adjustViewBounds |
| 356 | */ |
| 357 | @android.view.RemotableViewMethod |
| 358 | public void setAdjustViewBounds(boolean adjustViewBounds) { |
| 359 | mAdjustViewBounds = adjustViewBounds; |
| 360 | if (adjustViewBounds) { |
| 361 | setScaleType(ScaleType.FIT_CENTER); |
| 362 | } |
| 363 | } |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * The maximum width of this view. |
| 367 | * |
| 368 | * @return The maximum width of this view |
| 369 | * |
| 370 | * @see #setMaxWidth(int) |
| 371 | * |
| 372 | * @attr ref android.R.styleable#ImageView_maxWidth |
| 373 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 374 | @InspectableProperty |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 375 | public int getMaxWidth() { |
| 376 | return mMaxWidth; |
| 377 | } |
| 378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | /** |
| 380 | * An optional argument to supply a maximum width for this view. Only valid if |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 381 | * {@link #setAdjustViewBounds(boolean)} has been set to true. To set an image to be a maximum |
| 382 | * of 100 x 100 while preserving the original aspect ratio, do the following: 1) set |
| 383 | * adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width |
| 384 | * layout params to WRAP_CONTENT. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 385 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | * <p> |
| 387 | * Note that this view could be still smaller than 100 x 100 using this approach if the original |
| 388 | * image is small. To set an image to a fixed size, specify that size in the layout params and |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 389 | * then use {@link #setScaleType(android.widget.ImageView.ScaleType)} to determine how to fit |
| 390 | * the image within the bounds. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | * </p> |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 392 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | * @param maxWidth maximum width for this view |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 394 | * |
| 395 | * @see #getMaxWidth() |
| 396 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | * @attr ref android.R.styleable#ImageView_maxWidth |
| 398 | */ |
| 399 | @android.view.RemotableViewMethod |
| 400 | public void setMaxWidth(int maxWidth) { |
| 401 | mMaxWidth = maxWidth; |
| 402 | } |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * The maximum height of this view. |
| 406 | * |
| 407 | * @return The maximum height of this view |
| 408 | * |
| 409 | * @see #setMaxHeight(int) |
| 410 | * |
| 411 | * @attr ref android.R.styleable#ImageView_maxHeight |
| 412 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 413 | @InspectableProperty |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 414 | public int getMaxHeight() { |
| 415 | return mMaxHeight; |
| 416 | } |
| 417 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | /** |
| 419 | * An optional argument to supply a maximum height for this view. Only valid if |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 420 | * {@link #setAdjustViewBounds(boolean)} has been set to true. To set an image to be a |
| 421 | * maximum of 100 x 100 while preserving the original aspect ratio, do the following: 1) set |
| 422 | * adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width |
| 423 | * layout params to WRAP_CONTENT. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 424 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | * <p> |
| 426 | * Note that this view could be still smaller than 100 x 100 using this approach if the original |
| 427 | * image is small. To set an image to a fixed size, specify that size in the layout params and |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 428 | * then use {@link #setScaleType(android.widget.ImageView.ScaleType)} to determine how to fit |
| 429 | * the image within the bounds. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | * </p> |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 431 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | * @param maxHeight maximum height for this view |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 433 | * |
| 434 | * @see #getMaxHeight() |
| 435 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | * @attr ref android.R.styleable#ImageView_maxHeight |
| 437 | */ |
| 438 | @android.view.RemotableViewMethod |
| 439 | public void setMaxHeight(int maxHeight) { |
| 440 | mMaxHeight = maxHeight; |
| 441 | } |
| 442 | |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 443 | /** |
| 444 | * Gets the current Drawable, or null if no Drawable has been |
| 445 | * assigned. |
| 446 | * |
| 447 | * @return the view's drawable, or null if no drawable has been |
| 448 | * assigned. |
| 449 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 450 | @InspectableProperty(name = "src") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | public Drawable getDrawable() { |
Chet Haase | bfa11e4 | 2015-08-05 21:44:42 -0700 | [diff] [blame] | 452 | if (mDrawable == mRecycleableBitmapDrawable) { |
| 453 | // Consider our cached version dirty since app code now has a reference to it |
| 454 | mRecycleableBitmapDrawable = null; |
| 455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | return mDrawable; |
| 457 | } |
| 458 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 459 | private class ImageDrawableCallback implements Runnable { |
| 460 | |
| 461 | private final Drawable drawable; |
| 462 | private final Uri uri; |
| 463 | private final int resource; |
| 464 | |
| 465 | ImageDrawableCallback(Drawable drawable, Uri uri, int resource) { |
| 466 | this.drawable = drawable; |
| 467 | this.uri = uri; |
| 468 | this.resource = resource; |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void run() { |
| 473 | setImageDrawable(drawable); |
| 474 | mUri = uri; |
| 475 | mResource = resource; |
| 476 | } |
| 477 | } |
| 478 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | /** |
| 480 | * Sets a drawable as the content of this ImageView. |
Brad Fitzpatrick | c6b0b77 | 2010-08-27 11:43:56 -0700 | [diff] [blame] | 481 | * <p class="note">This does Bitmap reading and decoding on the UI |
| 482 | * thread, which can cause a latency hiccup. If that's a concern, |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 483 | * consider using {@link #setImageDrawable(android.graphics.drawable.Drawable)} or |
| 484 | * {@link #setImageBitmap(android.graphics.Bitmap)} and |
Brad Fitzpatrick | c6b0b77 | 2010-08-27 11:43:56 -0700 | [diff] [blame] | 485 | * {@link android.graphics.BitmapFactory} instead.</p> |
| 486 | * |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 487 | * @param resId the resource identifier of the drawable |
Brad Fitzpatrick | c6b0b77 | 2010-08-27 11:43:56 -0700 | [diff] [blame] | 488 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | * @attr ref android.R.styleable#ImageView_src |
| 490 | */ |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 491 | @android.view.RemotableViewMethod(asyncImpl="setImageResourceAsync") |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 492 | public void setImageResource(@DrawableRes int resId) { |
Alan Viverette | d739d7b | 2014-10-29 17:26:37 -0700 | [diff] [blame] | 493 | // The resource configuration may have changed, so we should always |
| 494 | // try to load the resource even if the resId hasn't changed. |
| 495 | final int oldWidth = mDrawableWidth; |
| 496 | final int oldHeight = mDrawableHeight; |
Alan Viverette | 4803bc1 | 2014-02-03 14:32:07 -0800 | [diff] [blame] | 497 | |
Alan Viverette | d739d7b | 2014-10-29 17:26:37 -0700 | [diff] [blame] | 498 | updateDrawable(null); |
| 499 | mResource = resId; |
| 500 | mUri = null; |
Adam Powell | f96ce02 | 2012-08-09 15:08:33 -0700 | [diff] [blame] | 501 | |
Alan Viverette | d739d7b | 2014-10-29 17:26:37 -0700 | [diff] [blame] | 502 | resolveUri(); |
Adam Powell | f96ce02 | 2012-08-09 15:08:33 -0700 | [diff] [blame] | 503 | |
Alan Viverette | d739d7b | 2014-10-29 17:26:37 -0700 | [diff] [blame] | 504 | if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) { |
| 505 | requestLayout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
Alan Viverette | d739d7b | 2014-10-29 17:26:37 -0700 | [diff] [blame] | 507 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 510 | /** @hide **/ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 511 | @UnsupportedAppUsage |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 512 | public Runnable setImageResourceAsync(@DrawableRes int resId) { |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 513 | Drawable d = null; |
| 514 | if (resId != 0) { |
| 515 | try { |
| 516 | d = getContext().getDrawable(resId); |
| 517 | } catch (Exception e) { |
| 518 | Log.w(LOG_TAG, "Unable to find resource: " + resId, e); |
| 519 | resId = 0; |
| 520 | } |
| 521 | } |
| 522 | return new ImageDrawableCallback(d, null, resId); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 523 | } |
| 524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | /** |
| 526 | * Sets the content of this ImageView to the specified Uri. |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 527 | * Note that you use this method to load images from a local Uri only. |
| 528 | * <p/> |
| 529 | * To learn how to display images from a remote Uri see: <a href="https://developer.android.com/topic/performance/graphics/index.html">Handling Bitmaps</a> |
| 530 | * <p/> |
Brad Fitzpatrick | c6b0b77 | 2010-08-27 11:43:56 -0700 | [diff] [blame] | 531 | * <p class="note">This does Bitmap reading and decoding on the UI |
| 532 | * thread, which can cause a latency hiccup. If that's a concern, |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 533 | * consider using {@link #setImageDrawable(Drawable)} or |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 534 | * {@link #setImageBitmap(android.graphics.Bitmap)} and |
Brad Fitzpatrick | c6b0b77 | 2010-08-27 11:43:56 -0700 | [diff] [blame] | 535 | * {@link android.graphics.BitmapFactory} instead.</p> |
| 536 | * |
Alan Viverette | ac21e56 | 2016-10-11 17:42:21 -0400 | [diff] [blame] | 537 | * <p class="note">On devices running SDK < 24, this method will fail to |
| 538 | * apply correct density scaling to images loaded from |
| 539 | * {@link ContentResolver#SCHEME_CONTENT content} and |
| 540 | * {@link ContentResolver#SCHEME_FILE file} schemes. Applications running |
| 541 | * on devices with SDK >= 24 <strong>MUST</strong> specify the |
| 542 | * {@code targetSdkVersion} in their manifest as 24 or above for density |
| 543 | * scaling to be applied to images loaded from these schemes.</p> |
| 544 | * |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 545 | * @param uri the Uri of an image, or {@code null} to clear the content |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | */ |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 547 | @android.view.RemotableViewMethod(asyncImpl="setImageURIAsync") |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 548 | public void setImageURI(@Nullable Uri uri) { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 549 | if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | updateDrawable(null); |
| 551 | mResource = 0; |
| 552 | mUri = uri; |
Adam Powell | f96ce02 | 2012-08-09 15:08:33 -0700 | [diff] [blame] | 553 | |
| 554 | final int oldWidth = mDrawableWidth; |
| 555 | final int oldHeight = mDrawableHeight; |
| 556 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | resolveUri(); |
Adam Powell | f96ce02 | 2012-08-09 15:08:33 -0700 | [diff] [blame] | 558 | |
| 559 | if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) { |
| 560 | requestLayout(); |
| 561 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | invalidate(); |
| 563 | } |
| 564 | } |
| 565 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 566 | /** @hide **/ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 567 | @UnsupportedAppUsage |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 568 | public Runnable setImageURIAsync(@Nullable Uri uri) { |
| 569 | if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) { |
| 570 | Drawable d = uri == null ? null : getDrawableFromUri(uri); |
| 571 | if (d == null) { |
| 572 | // Do not set the URI if the drawable couldn't be loaded. |
| 573 | uri = null; |
| 574 | } |
| 575 | return new ImageDrawableCallback(d, uri, 0); |
| 576 | } |
| 577 | return null; |
| 578 | } |
| 579 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | /** |
| 581 | * Sets a drawable as the content of this ImageView. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 582 | * |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 583 | * @param drawable the Drawable to set, or {@code null} to clear the |
| 584 | * content |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | */ |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 586 | public void setImageDrawable(@Nullable Drawable drawable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | if (mDrawable != drawable) { |
| 588 | mResource = 0; |
| 589 | mUri = null; |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 590 | |
Adam Powell | f96ce02 | 2012-08-09 15:08:33 -0700 | [diff] [blame] | 591 | final int oldWidth = mDrawableWidth; |
| 592 | final int oldHeight = mDrawableHeight; |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 593 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | updateDrawable(drawable); |
Adam Powell | 2a0e99d | 2011-08-04 10:55:03 -0700 | [diff] [blame] | 595 | |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 596 | if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) { |
Adam Powell | 2a0e99d | 2011-08-04 10:55:03 -0700 | [diff] [blame] | 597 | requestLayout(); |
| 598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | invalidate(); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /** |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 604 | * Sets the content of this ImageView to the specified Icon. |
| 605 | * |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 606 | * <p class="note">Depending on the Icon type, this may do Bitmap reading |
| 607 | * and decoding on the UI thread, which can cause UI jank. If that's a |
| 608 | * concern, consider using |
Chris Banes | e695bb8 | 2015-05-14 10:12:20 +0100 | [diff] [blame] | 609 | * {@link Icon#loadDrawableAsync(Context, Icon.OnDrawableLoadedListener, Handler)} |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 610 | * and then {@link #setImageDrawable(android.graphics.drawable.Drawable)} |
| 611 | * instead.</p> |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 612 | * |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 613 | * @param icon an Icon holding the desired image, or {@code null} to clear |
| 614 | * the content |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 615 | */ |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 616 | @android.view.RemotableViewMethod(asyncImpl="setImageIconAsync") |
Alan Viverette | 6ef129e | 2015-06-30 13:32:27 -0700 | [diff] [blame] | 617 | public void setImageIcon(@Nullable Icon icon) { |
| 618 | setImageDrawable(icon == null ? null : icon.loadDrawable(mContext)); |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 619 | } |
| 620 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 621 | /** @hide **/ |
| 622 | public Runnable setImageIconAsync(@Nullable Icon icon) { |
| 623 | return new ImageDrawableCallback(icon == null ? null : icon.loadDrawable(mContext), null, 0); |
| 624 | } |
| 625 | |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 626 | /** |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 627 | * Applies a tint to the image drawable. Does not modify the current tint |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 628 | * mode, which is {@link PorterDuff.Mode#SRC_IN} by default. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 629 | * <p> |
| 630 | * Subsequent calls to {@link #setImageDrawable(Drawable)} will automatically |
| 631 | * mutate the drawable and apply the specified tint and tint mode using |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 632 | * {@link Drawable#setTintList(ColorStateList)}. |
Alan Viverette | 2b4e14c | 2016-07-21 16:25:13 -0400 | [diff] [blame] | 633 | * <p> |
| 634 | * <em>Note:</em> The default tint mode used by this setter is NOT |
| 635 | * consistent with the default tint mode used by the |
| 636 | * {@link android.R.styleable#ImageView_tint android:tint} |
| 637 | * attribute. If the {@code android:tint} attribute is specified, the |
| 638 | * default tint mode will be set to {@link PorterDuff.Mode#SRC_ATOP} to |
| 639 | * ensure consistency with earlier versions of the platform. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 640 | * |
| 641 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 642 | * |
| 643 | * @attr ref android.R.styleable#ImageView_tint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 644 | * @see #getImageTintList() |
| 645 | * @see Drawable#setTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 646 | */ |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 647 | public void setImageTintList(@Nullable ColorStateList tint) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 648 | mDrawableTintList = tint; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 649 | mHasDrawableTint = true; |
| 650 | |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 651 | applyImageTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /** |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 655 | * Get the current {@link android.content.res.ColorStateList} used to tint the image Drawable, |
| 656 | * or null if no tint is applied. |
| 657 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 658 | * @return the tint applied to the image drawable |
| 659 | * @attr ref android.R.styleable#ImageView_tint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 660 | * @see #setImageTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 661 | */ |
| 662 | @Nullable |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 663 | @InspectableProperty(name = "tint") |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 664 | public ColorStateList getImageTintList() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 665 | return mDrawableTintList; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Specifies the blending mode used to apply the tint specified by |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 670 | * {@link #setImageTintList(ColorStateList)}} to the image drawable. The default |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 671 | * mode is {@link PorterDuff.Mode#SRC_IN}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 672 | * |
| 673 | * @param tintMode the blending mode used to apply the tint, may be |
| 674 | * {@code null} to clear tint |
| 675 | * @attr ref android.R.styleable#ImageView_tintMode |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 676 | * @see #getImageTintMode() |
| 677 | * @see Drawable#setTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 678 | */ |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 679 | public void setImageTintMode(@Nullable PorterDuff.Mode tintMode) { |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 680 | setImageTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Specifies the blending mode used to apply the tint specified by |
| 685 | * {@link #setImageTintList(ColorStateList)}} to the image drawable. The default |
| 686 | * mode is {@link BlendMode#SRC_IN}. |
| 687 | * |
| 688 | * @param blendMode the blending mode used to apply the tint, may be |
| 689 | * {@code null} to clear tint |
| 690 | * @attr ref android.R.styleable#ImageView_tintMode |
| 691 | * @see #getImageTintMode() |
| 692 | * @see Drawable#setTintBlendMode(BlendMode) |
| 693 | */ |
| 694 | public void setImageTintBlendMode(@Nullable BlendMode blendMode) { |
| 695 | mDrawableBlendMode = blendMode; |
| 696 | mHasDrawableBlendMode = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 697 | |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 698 | applyImageTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /** |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 702 | * Gets the blending mode used to apply the tint to the image Drawable |
| 703 | * @return the blending mode used to apply the tint to the image Drawable |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 704 | * @attr ref android.R.styleable#ImageView_tintMode |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 705 | * @see #setImageTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 706 | */ |
| 707 | @Nullable |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 708 | @InspectableProperty(name = "tintMode") |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 709 | public PorterDuff.Mode getImageTintMode() { |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 710 | return mDrawableBlendMode != null |
| 711 | ? BlendMode.blendModeToPorterDuffMode(mDrawableBlendMode) : null; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Gets the blending mode used to apply the tint to the image Drawable |
| 716 | * @return the blending mode used to apply the tint to the image Drawable |
| 717 | * @attr ref android.R.styleable#ImageView_tintMode |
| 718 | * @see #setImageTintBlendMode(BlendMode) |
| 719 | */ |
| 720 | @Nullable |
| 721 | @InspectableProperty(name = "blendMode", attributeId = android.R.styleable.ImageView_tintMode) |
| 722 | public BlendMode getImageTintBlendMode() { |
| 723 | return mDrawableBlendMode; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 726 | private void applyImageTint() { |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 727 | if (mDrawable != null && (mHasDrawableTint || mHasDrawableBlendMode)) { |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 728 | mDrawable = mDrawable.mutate(); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 729 | |
| 730 | if (mHasDrawableTint) { |
| 731 | mDrawable.setTintList(mDrawableTintList); |
| 732 | } |
| 733 | |
Nader Jawad | 531db61 | 2019-04-14 21:58:04 -0700 | [diff] [blame] | 734 | if (mHasDrawableBlendMode) { |
| 735 | mDrawable.setTintBlendMode(mDrawableBlendMode); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 736 | } |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 737 | |
| 738 | // The drawable (or one of its children) may not have been |
| 739 | // stateful before applying the tint, so let's try again. |
| 740 | if (mDrawable.isStateful()) { |
| 741 | mDrawable.setState(getDrawableState()); |
| 742 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | * Sets a Bitmap as the content of this ImageView. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 748 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | * @param bm The bitmap to set |
| 750 | */ |
| 751 | @android.view.RemotableViewMethod |
| 752 | public void setImageBitmap(Bitmap bm) { |
John Reck | 5a13569 | 2015-07-10 10:02:58 -0700 | [diff] [blame] | 753 | // Hacky fix to force setImageDrawable to do a full setImageDrawable |
| 754 | // instead of doing an object reference comparison |
| 755 | mDrawable = null; |
| 756 | if (mRecycleableBitmapDrawable == null) { |
Chris Craik | 82af13e | 2015-07-14 13:36:27 -0700 | [diff] [blame] | 757 | mRecycleableBitmapDrawable = new BitmapDrawable(mContext.getResources(), bm); |
John Reck | b7ba122 | 2015-07-09 17:37:34 -0700 | [diff] [blame] | 758 | } else { |
John Reck | 5a13569 | 2015-07-10 10:02:58 -0700 | [diff] [blame] | 759 | mRecycleableBitmapDrawable.setBitmap(bm); |
John Reck | b7ba122 | 2015-07-09 17:37:34 -0700 | [diff] [blame] | 760 | } |
John Reck | 5a13569 | 2015-07-10 10:02:58 -0700 | [diff] [blame] | 761 | setImageDrawable(mRecycleableBitmapDrawable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | } |
| 763 | |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 764 | /** |
| 765 | * Set the state of the current {@link android.graphics.drawable.StateListDrawable}. |
| 766 | * For more information about State List Drawables, see: <a href="https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList">the Drawable Resource Guide</a>. |
| 767 | * |
| 768 | * @param state the state to set for the StateListDrawable |
| 769 | * @param merge if true, merges the state values for the state you specify into the current state |
| 770 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 771 | public void setImageState(int[] state, boolean merge) { |
| 772 | mState = state; |
| 773 | mMergeState = merge; |
| 774 | if (mDrawable != null) { |
| 775 | refreshDrawableState(); |
| 776 | resizeFromDrawable(); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | @Override |
| 781 | public void setSelected(boolean selected) { |
| 782 | super.setSelected(selected); |
| 783 | resizeFromDrawable(); |
| 784 | } |
| 785 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 786 | /** |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 787 | * Sets the image level, when it is constructed from a |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 788 | * {@link android.graphics.drawable.LevelListDrawable}. |
| 789 | * |
| 790 | * @param level The new level for the image. |
| 791 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | @android.view.RemotableViewMethod |
| 793 | public void setImageLevel(int level) { |
| 794 | mLevel = level; |
| 795 | if (mDrawable != null) { |
| 796 | mDrawable.setLevel(level); |
| 797 | resizeFromDrawable(); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Options for scaling the bounds of an image to the bounds of this view. |
| 803 | */ |
| 804 | public enum ScaleType { |
| 805 | /** |
| 806 | * Scale using the image matrix when drawing. The image matrix can be set using |
| 807 | * {@link ImageView#setImageMatrix(Matrix)}. From XML, use this syntax: |
| 808 | * <code>android:scaleType="matrix"</code>. |
| 809 | */ |
| 810 | MATRIX (0), |
| 811 | /** |
| 812 | * Scale the image using {@link Matrix.ScaleToFit#FILL}. |
| 813 | * From XML, use this syntax: <code>android:scaleType="fitXY"</code>. |
| 814 | */ |
| 815 | FIT_XY (1), |
| 816 | /** |
| 817 | * Scale the image using {@link Matrix.ScaleToFit#START}. |
| 818 | * From XML, use this syntax: <code>android:scaleType="fitStart"</code>. |
| 819 | */ |
| 820 | FIT_START (2), |
| 821 | /** |
| 822 | * Scale the image using {@link Matrix.ScaleToFit#CENTER}. |
| 823 | * From XML, use this syntax: |
| 824 | * <code>android:scaleType="fitCenter"</code>. |
| 825 | */ |
| 826 | FIT_CENTER (3), |
| 827 | /** |
| 828 | * Scale the image using {@link Matrix.ScaleToFit#END}. |
| 829 | * From XML, use this syntax: <code>android:scaleType="fitEnd"</code>. |
| 830 | */ |
| 831 | FIT_END (4), |
| 832 | /** |
| 833 | * Center the image in the view, but perform no scaling. |
| 834 | * From XML, use this syntax: <code>android:scaleType="center"</code>. |
| 835 | */ |
| 836 | CENTER (5), |
| 837 | /** |
| 838 | * Scale the image uniformly (maintain the image's aspect ratio) so |
| 839 | * that both dimensions (width and height) of the image will be equal |
| 840 | * to or larger than the corresponding dimension of the view |
| 841 | * (minus padding). The image is then centered in the view. |
| 842 | * From XML, use this syntax: <code>android:scaleType="centerCrop"</code>. |
| 843 | */ |
| 844 | CENTER_CROP (6), |
| 845 | /** |
| 846 | * Scale the image uniformly (maintain the image's aspect ratio) so |
| 847 | * that both dimensions (width and height) of the image will be equal |
| 848 | * to or less than the corresponding dimension of the view |
| 849 | * (minus padding). The image is then centered in the view. |
| 850 | * From XML, use this syntax: <code>android:scaleType="centerInside"</code>. |
| 851 | */ |
| 852 | CENTER_INSIDE (7); |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 853 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | ScaleType(int ni) { |
| 855 | nativeInt = ni; |
| 856 | } |
| 857 | final int nativeInt; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Controls how the image should be resized or moved to match the size |
| 862 | * of this ImageView. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 863 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | * @param scaleType The desired scaling mode. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 865 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | * @attr ref android.R.styleable#ImageView_scaleType |
| 867 | */ |
| 868 | public void setScaleType(ScaleType scaleType) { |
| 869 | if (scaleType == null) { |
| 870 | throw new NullPointerException(); |
| 871 | } |
| 872 | |
| 873 | if (mScaleType != scaleType) { |
| 874 | mScaleType = scaleType; |
| 875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | requestLayout(); |
| 877 | invalidate(); |
| 878 | } |
| 879 | } |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 880 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | /** |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 882 | * Returns the current ScaleType that is used to scale the bounds of an image to the bounds of the ImageView. |
| 883 | * @return The ScaleType used to scale the image. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | * @see ImageView.ScaleType |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | * @attr ref android.R.styleable#ImageView_scaleType |
| 886 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 887 | @InspectableProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | public ScaleType getScaleType() { |
| 889 | return mScaleType; |
| 890 | } |
| 891 | |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 892 | /** Returns the view's optional matrix. This is applied to the |
John Spurlock | 0196e56 | 2014-02-03 09:00:35 -0500 | [diff] [blame] | 893 | view's drawable when it is drawn. If there is no matrix, |
Romain Guy | 5370405 | 2013-01-30 16:25:50 -0800 | [diff] [blame] | 894 | this method will return an identity matrix. |
| 895 | Do not change this matrix in place but make a copy. |
| 896 | If you want a different matrix applied to the drawable, |
| 897 | be sure to call setImageMatrix(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | */ |
| 899 | public Matrix getImageMatrix() { |
Dake Gu | 1ee6017 | 2013-01-03 15:11:43 -0800 | [diff] [blame] | 900 | if (mDrawMatrix == null) { |
Romain Guy | 5370405 | 2013-01-30 16:25:50 -0800 | [diff] [blame] | 901 | return new Matrix(Matrix.IDENTITY_MATRIX); |
Dake Gu | 1ee6017 | 2013-01-03 15:11:43 -0800 | [diff] [blame] | 902 | } |
| 903 | return mDrawMatrix; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | } |
| 905 | |
Joseph Cooper | d96fdbd | 2015-04-03 14:00:31 -0700 | [diff] [blame] | 906 | /** |
| 907 | * Adds a transformation {@link Matrix} that is applied |
| 908 | * to the view's drawable when it is drawn. Allows custom scaling, |
| 909 | * translation, and perspective distortion. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 910 | * |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 911 | * @param matrix The transformation parameters in matrix form. |
Joseph Cooper | d96fdbd | 2015-04-03 14:00:31 -0700 | [diff] [blame] | 912 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | public void setImageMatrix(Matrix matrix) { |
Joseph Cooper | d96fdbd | 2015-04-03 14:00:31 -0700 | [diff] [blame] | 914 | // collapse null and identity to just null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | if (matrix != null && matrix.isIdentity()) { |
| 916 | matrix = null; |
| 917 | } |
Joseph Cooper | d96fdbd | 2015-04-03 14:00:31 -0700 | [diff] [blame] | 918 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | // don't invalidate unless we're actually changing our matrix |
| 920 | if (matrix == null && !mMatrix.isIdentity() || |
| 921 | matrix != null && !mMatrix.equals(matrix)) { |
| 922 | mMatrix.set(matrix); |
Chih-Chung Chang | f888726 | 2009-07-30 15:36:13 +0800 | [diff] [blame] | 923 | configureBounds(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | invalidate(); |
| 925 | } |
| 926 | } |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 927 | |
| 928 | /** |
| 929 | * Return whether this ImageView crops to padding. |
| 930 | * |
| 931 | * @return whether this ImageView crops to padding |
| 932 | * |
| 933 | * @see #setCropToPadding(boolean) |
| 934 | * |
| 935 | * @attr ref android.R.styleable#ImageView_cropToPadding |
| 936 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 937 | @InspectableProperty |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 938 | public boolean getCropToPadding() { |
| 939 | return mCropToPadding; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * Sets whether this ImageView will crop to padding. |
| 944 | * |
| 945 | * @param cropToPadding whether this ImageView will crop to padding |
| 946 | * |
| 947 | * @see #getCropToPadding() |
| 948 | * |
| 949 | * @attr ref android.R.styleable#ImageView_cropToPadding |
| 950 | */ |
| 951 | public void setCropToPadding(boolean cropToPadding) { |
| 952 | if (mCropToPadding != cropToPadding) { |
| 953 | mCropToPadding = cropToPadding; |
| 954 | requestLayout(); |
| 955 | invalidate(); |
| 956 | } |
| 957 | } |
| 958 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 959 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | private void resolveUri() { |
| 961 | if (mDrawable != null) { |
| 962 | return; |
| 963 | } |
| 964 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 965 | if (getResources() == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | return; |
| 967 | } |
| 968 | |
| 969 | Drawable d = null; |
| 970 | |
| 971 | if (mResource != 0) { |
| 972 | try { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 973 | d = mContext.getDrawable(mResource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | } catch (Exception e) { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 975 | Log.w(LOG_TAG, "Unable to find resource: " + mResource, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | // Don't try again. |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 977 | mResource = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | } |
| 979 | } else if (mUri != null) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 980 | d = getDrawableFromUri(mUri); |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 981 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | if (d == null) { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 983 | Log.w(LOG_TAG, "resolveUri failed on bad bitmap uri: " + mUri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | // Don't try again. |
| 985 | mUri = null; |
| 986 | } |
| 987 | } else { |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | updateDrawable(d); |
| 992 | } |
| 993 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 994 | private Drawable getDrawableFromUri(Uri uri) { |
| 995 | final String scheme = uri.getScheme(); |
| 996 | if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { |
| 997 | try { |
| 998 | // Load drawable through Resources, to get the source density information |
| 999 | ContentResolver.OpenResourceIdResult r = |
| 1000 | mContext.getContentResolver().getResourceId(uri); |
| 1001 | return r.r.getDrawable(r.id, mContext.getTheme()); |
| 1002 | } catch (Exception e) { |
| 1003 | Log.w(LOG_TAG, "Unable to open content: " + uri, e); |
| 1004 | } |
| 1005 | } else if (ContentResolver.SCHEME_CONTENT.equals(scheme) |
| 1006 | || ContentResolver.SCHEME_FILE.equals(scheme)) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1007 | try { |
Leon Scroggins III | 046a99e | 2018-01-31 14:59:29 -0500 | [diff] [blame] | 1008 | Resources res = sCompatUseCorrectStreamDensity ? getResources() : null; |
| 1009 | ImageDecoder.Source src = ImageDecoder.createSource(mContext.getContentResolver(), |
| 1010 | uri, res); |
| 1011 | return ImageDecoder.decodeDrawable(src, (decoder, info, s) -> { |
| 1012 | decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE); |
| 1013 | }); |
| 1014 | } catch (IOException e) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1015 | Log.w(LOG_TAG, "Unable to open content: " + uri, e); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } else { |
| 1018 | return Drawable.createFromPath(uri.toString()); |
| 1019 | } |
| 1020 | return null; |
| 1021 | } |
| 1022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | @Override |
| 1024 | public int[] onCreateDrawableState(int extraSpace) { |
| 1025 | if (mState == null) { |
| 1026 | return super.onCreateDrawableState(extraSpace); |
| 1027 | } else if (!mMergeState) { |
| 1028 | return mState; |
| 1029 | } else { |
| 1030 | return mergeDrawableStates( |
| 1031 | super.onCreateDrawableState(extraSpace + mState.length), mState); |
| 1032 | } |
| 1033 | } |
| 1034 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1035 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | private void updateDrawable(Drawable d) { |
John Reck | 5a13569 | 2015-07-10 10:02:58 -0700 | [diff] [blame] | 1037 | if (d != mRecycleableBitmapDrawable && mRecycleableBitmapDrawable != null) { |
| 1038 | mRecycleableBitmapDrawable.setBitmap(null); |
| 1039 | } |
| 1040 | |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1041 | boolean sameDrawable = false; |
| 1042 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | if (mDrawable != null) { |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1044 | sameDrawable = mDrawable == d; |
Adam Powell | 35e2ea0 | 2016-03-22 10:40:29 -0700 | [diff] [blame] | 1045 | mDrawable.setCallback(null); |
| 1046 | unscheduleDrawable(mDrawable); |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1047 | if (!sCompatDrawableVisibilityDispatch && !sameDrawable && isAttachedToWindow()) { |
Adam Powell | 4b2e12c | 2016-03-31 15:35:35 -0700 | [diff] [blame] | 1048 | mDrawable.setVisible(false, false); |
| 1049 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1051 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | mDrawable = d; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1053 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | if (d != null) { |
| 1055 | d.setCallback(this); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1056 | d.setLayoutDirection(getLayoutDirection()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | if (d.isStateful()) { |
| 1058 | d.setState(getDrawableState()); |
| 1059 | } |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1060 | if (!sameDrawable || sCompatDrawableVisibilityDispatch) { |
| 1061 | final boolean visible = sCompatDrawableVisibilityDispatch |
| 1062 | ? getVisibility() == VISIBLE |
| 1063 | : isAttachedToWindow() && getWindowVisibility() == VISIBLE && isShown(); |
| 1064 | d.setVisible(visible, true); |
| 1065 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1066 | d.setLevel(mLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | mDrawableWidth = d.getIntrinsicWidth(); |
| 1068 | mDrawableHeight = d.getIntrinsicHeight(); |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1069 | applyImageTint(); |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 1070 | applyColorFilter(); |
| 1071 | applyAlpha(); |
| 1072 | applyXfermode(); |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 1073 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | configureBounds(); |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1075 | } else { |
| 1076 | mDrawableWidth = mDrawableHeight = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1080 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | private void resizeFromDrawable() { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1082 | final Drawable d = mDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | if (d != null) { |
| 1084 | int w = d.getIntrinsicWidth(); |
| 1085 | if (w < 0) w = mDrawableWidth; |
| 1086 | int h = d.getIntrinsicHeight(); |
| 1087 | if (h < 0) h = mDrawableHeight; |
| 1088 | if (w != mDrawableWidth || h != mDrawableHeight) { |
| 1089 | mDrawableWidth = w; |
| 1090 | mDrawableHeight = h; |
| 1091 | requestLayout(); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
Fabrice Di Meglio | 3f5a90b | 2013-06-24 19:22:25 -0700 | [diff] [blame] | 1096 | @Override |
| 1097 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 1098 | super.onRtlPropertiesChanged(layoutDirection); |
| 1099 | |
| 1100 | if (mDrawable != null) { |
| 1101 | mDrawable.setLayoutDirection(layoutDirection); |
| 1102 | } |
| 1103 | } |
| 1104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | private static final Matrix.ScaleToFit[] sS2FArray = { |
| 1106 | Matrix.ScaleToFit.FILL, |
| 1107 | Matrix.ScaleToFit.START, |
| 1108 | Matrix.ScaleToFit.CENTER, |
| 1109 | Matrix.ScaleToFit.END |
| 1110 | }; |
| 1111 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1112 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | private static Matrix.ScaleToFit scaleTypeToScaleToFit(ScaleType st) { |
| 1114 | // ScaleToFit enum to their corresponding Matrix.ScaleToFit values |
| 1115 | return sS2FArray[st.nativeInt - 1]; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | |
| 1118 | @Override |
| 1119 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 1120 | resolveUri(); |
| 1121 | int w; |
| 1122 | int h; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | // Desired aspect ratio of the view's contents (not including padding) |
| 1125 | float desiredAspect = 0.0f; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | // We are allowed to change the view's width |
| 1128 | boolean resizeWidth = false; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | // We are allowed to change the view's height |
| 1131 | boolean resizeHeight = false; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1132 | |
Adam Powell | 2a0e99d | 2011-08-04 10:55:03 -0700 | [diff] [blame] | 1133 | final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 1134 | final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 1135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | if (mDrawable == null) { |
| 1137 | // If no drawable, its intrinsic size is 0. |
| 1138 | mDrawableWidth = -1; |
| 1139 | mDrawableHeight = -1; |
| 1140 | w = h = 0; |
| 1141 | } else { |
| 1142 | w = mDrawableWidth; |
| 1143 | h = mDrawableHeight; |
| 1144 | if (w <= 0) w = 1; |
| 1145 | if (h <= 0) h = 1; |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1146 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | // We are supposed to adjust view bounds to match the aspect |
| 1148 | // ratio of our drawable. See if that is possible. |
| 1149 | if (mAdjustViewBounds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | resizeWidth = widthSpecMode != MeasureSpec.EXACTLY; |
| 1151 | resizeHeight = heightSpecMode != MeasureSpec.EXACTLY; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1152 | |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1153 | desiredAspect = (float) w / (float) h; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1154 | } |
| 1155 | } |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1156 | |
| 1157 | final int pleft = mPaddingLeft; |
| 1158 | final int pright = mPaddingRight; |
| 1159 | final int ptop = mPaddingTop; |
| 1160 | final int pbottom = mPaddingBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | |
| 1162 | int widthSize; |
| 1163 | int heightSize; |
| 1164 | |
| 1165 | if (resizeWidth || resizeHeight) { |
| 1166 | /* If we get here, it means we want to resize to match the |
| 1167 | drawables aspect ratio, and we have the freedom to change at |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1168 | least one dimension. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | */ |
| 1170 | |
| 1171 | // Get the max possible width given our constraints |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1172 | widthSize = resolveAdjustedSize(w + pleft + pright, mMaxWidth, widthMeasureSpec); |
| 1173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | // Get the max possible height given our constraints |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1175 | heightSize = resolveAdjustedSize(h + ptop + pbottom, mMaxHeight, heightMeasureSpec); |
| 1176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1177 | if (desiredAspect != 0.0f) { |
| 1178 | // See what our actual aspect ratio is |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1179 | final float actualAspect = (float)(widthSize - pleft - pright) / |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | (heightSize - ptop - pbottom); |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | if (Math.abs(actualAspect - desiredAspect) > 0.0000001) { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1184 | boolean done = false; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1185 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | // Try adjusting width to be proportional to height |
| 1187 | if (resizeWidth) { |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1188 | int newWidth = (int)(desiredAspect * (heightSize - ptop - pbottom)) + |
| 1189 | pleft + pright; |
Adam Powell | d5edc77 | 2012-09-26 15:21:39 -0700 | [diff] [blame] | 1190 | |
| 1191 | // Allow the width to outgrow its original estimate if height is fixed. |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1192 | if (!resizeHeight && !sCompatAdjustViewBounds) { |
Adam Powell | d5edc77 | 2012-09-26 15:21:39 -0700 | [diff] [blame] | 1193 | widthSize = resolveAdjustedSize(newWidth, mMaxWidth, widthMeasureSpec); |
| 1194 | } |
| 1195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | if (newWidth <= widthSize) { |
| 1197 | widthSize = newWidth; |
| 1198 | done = true; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1199 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | } |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | // Try adjusting height to be proportional to width |
| 1203 | if (!done && resizeHeight) { |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1204 | int newHeight = (int)((widthSize - pleft - pright) / desiredAspect) + |
| 1205 | ptop + pbottom; |
Adam Powell | d5edc77 | 2012-09-26 15:21:39 -0700 | [diff] [blame] | 1206 | |
| 1207 | // Allow the height to outgrow its original estimate if width is fixed. |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1208 | if (!resizeWidth && !sCompatAdjustViewBounds) { |
Adam Powell | d5edc77 | 2012-09-26 15:21:39 -0700 | [diff] [blame] | 1209 | heightSize = resolveAdjustedSize(newHeight, mMaxHeight, |
| 1210 | heightMeasureSpec); |
| 1211 | } |
| 1212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | if (newHeight <= heightSize) { |
| 1214 | heightSize = newHeight; |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1215 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | } else { |
| 1220 | /* We are either don't want to preserve the drawables aspect ratio, |
| 1221 | or we are not allowed to change view dimensions. Just measure in |
| 1222 | the normal way. |
| 1223 | */ |
| 1224 | w += pleft + pright; |
| 1225 | h += ptop + pbottom; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | w = Math.max(w, getSuggestedMinimumWidth()); |
| 1228 | h = Math.max(h, getSuggestedMinimumHeight()); |
| 1229 | |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1230 | widthSize = resolveSizeAndState(w, widthMeasureSpec, 0); |
| 1231 | heightSize = resolveSizeAndState(h, heightMeasureSpec, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | setMeasuredDimension(widthSize, heightSize); |
| 1235 | } |
| 1236 | |
| 1237 | private int resolveAdjustedSize(int desiredSize, int maxSize, |
| 1238 | int measureSpec) { |
| 1239 | int result = desiredSize; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1240 | final int specMode = MeasureSpec.getMode(measureSpec); |
| 1241 | final int specSize = MeasureSpec.getSize(measureSpec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | switch (specMode) { |
| 1243 | case MeasureSpec.UNSPECIFIED: |
| 1244 | /* Parent says we can be as big as we want. Just don't be larger |
| 1245 | than max size imposed on ourselves. |
| 1246 | */ |
| 1247 | result = Math.min(desiredSize, maxSize); |
| 1248 | break; |
| 1249 | case MeasureSpec.AT_MOST: |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1250 | // Parent says we can be as big as we want, up to specSize. |
| 1251 | // Don't be larger than specSize, and don't be larger than |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | // the max size imposed on ourselves. |
| 1253 | result = Math.min(Math.min(desiredSize, specSize), maxSize); |
| 1254 | break; |
| 1255 | case MeasureSpec.EXACTLY: |
| 1256 | // No choice. Do what we are told. |
| 1257 | result = specSize; |
| 1258 | break; |
| 1259 | } |
| 1260 | return result; |
| 1261 | } |
| 1262 | |
| 1263 | @Override |
| 1264 | protected boolean setFrame(int l, int t, int r, int b) { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1265 | final boolean changed = super.setFrame(l, t, r, b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1266 | mHaveFrame = true; |
| 1267 | configureBounds(); |
| 1268 | return changed; |
| 1269 | } |
| 1270 | |
| 1271 | private void configureBounds() { |
| 1272 | if (mDrawable == null || !mHaveFrame) { |
| 1273 | return; |
| 1274 | } |
| 1275 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1276 | final int dwidth = mDrawableWidth; |
| 1277 | final int dheight = mDrawableHeight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1279 | final int vwidth = getWidth() - mPaddingLeft - mPaddingRight; |
| 1280 | final int vheight = getHeight() - mPaddingTop - mPaddingBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 | |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1282 | final boolean fits = (dwidth < 0 || vwidth == dwidth) |
| 1283 | && (dheight < 0 || vheight == dheight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | |
| 1285 | if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) { |
| 1286 | /* If the drawable has no intrinsic size, or we're told to |
| 1287 | scaletofit, then we just fill our entire view. |
| 1288 | */ |
| 1289 | mDrawable.setBounds(0, 0, vwidth, vheight); |
| 1290 | mDrawMatrix = null; |
| 1291 | } else { |
| 1292 | // We need to do the scaling ourself, so have the drawable |
| 1293 | // use its native size. |
| 1294 | mDrawable.setBounds(0, 0, dwidth, dheight); |
| 1295 | |
| 1296 | if (ScaleType.MATRIX == mScaleType) { |
| 1297 | // Use the specified matrix as-is. |
| 1298 | if (mMatrix.isIdentity()) { |
| 1299 | mDrawMatrix = null; |
| 1300 | } else { |
| 1301 | mDrawMatrix = mMatrix; |
| 1302 | } |
| 1303 | } else if (fits) { |
| 1304 | // The bitmap fits exactly, no transform needed. |
| 1305 | mDrawMatrix = null; |
| 1306 | } else if (ScaleType.CENTER == mScaleType) { |
| 1307 | // Center bitmap in view, no scaling. |
| 1308 | mDrawMatrix = mMatrix; |
Adam Powell | 14d1f138 | 2015-06-04 12:56:00 -0700 | [diff] [blame] | 1309 | mDrawMatrix.setTranslate(Math.round((vwidth - dwidth) * 0.5f), |
| 1310 | Math.round((vheight - dheight) * 0.5f)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | } else if (ScaleType.CENTER_CROP == mScaleType) { |
| 1312 | mDrawMatrix = mMatrix; |
| 1313 | |
| 1314 | float scale; |
| 1315 | float dx = 0, dy = 0; |
| 1316 | |
| 1317 | if (dwidth * vheight > vwidth * dheight) { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1318 | scale = (float) vheight / (float) dheight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | dx = (vwidth - dwidth * scale) * 0.5f; |
| 1320 | } else { |
| 1321 | scale = (float) vwidth / (float) dwidth; |
| 1322 | dy = (vheight - dheight * scale) * 0.5f; |
| 1323 | } |
| 1324 | |
| 1325 | mDrawMatrix.setScale(scale, scale); |
Adam Powell | 14d1f138 | 2015-06-04 12:56:00 -0700 | [diff] [blame] | 1326 | mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1327 | } else if (ScaleType.CENTER_INSIDE == mScaleType) { |
| 1328 | mDrawMatrix = mMatrix; |
| 1329 | float scale; |
| 1330 | float dx; |
| 1331 | float dy; |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | if (dwidth <= vwidth && dheight <= vheight) { |
| 1334 | scale = 1.0f; |
| 1335 | } else { |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1336 | scale = Math.min((float) vwidth / (float) dwidth, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | (float) vheight / (float) dheight); |
| 1338 | } |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1339 | |
Adam Powell | 14d1f138 | 2015-06-04 12:56:00 -0700 | [diff] [blame] | 1340 | dx = Math.round((vwidth - dwidth * scale) * 0.5f); |
| 1341 | dy = Math.round((vheight - dheight * scale) * 0.5f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | |
| 1343 | mDrawMatrix.setScale(scale, scale); |
| 1344 | mDrawMatrix.postTranslate(dx, dy); |
| 1345 | } else { |
| 1346 | // Generate the required transform. |
| 1347 | mTempSrc.set(0, 0, dwidth, dheight); |
| 1348 | mTempDst.set(0, 0, vwidth, vheight); |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | mDrawMatrix = mMatrix; |
Romain Guy | 9e648c4 | 2011-08-17 20:04:27 -0700 | [diff] [blame] | 1351 | mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | @Override |
| 1357 | protected void drawableStateChanged() { |
| 1358 | super.drawableStateChanged(); |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1359 | |
Alan Viverette | ad0020f | 2015-09-04 10:10:42 -0400 | [diff] [blame] | 1360 | final Drawable drawable = mDrawable; |
| 1361 | if (drawable != null && drawable.isStateful() |
| 1362 | && drawable.setState(getDrawableState())) { |
| 1363 | invalidateDrawable(drawable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 1367 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame] | 1368 | public void drawableHotspotChanged(float x, float y) { |
| 1369 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 1370 | |
| 1371 | if (mDrawable != null) { |
| 1372 | mDrawable.setHotspot(x, y); |
| 1373 | } |
| 1374 | } |
| 1375 | |
Andrey Kulikov | 3ac312a | 2018-11-27 15:52:17 +0000 | [diff] [blame] | 1376 | /** |
| 1377 | * Applies a temporary transformation {@link Matrix} to the view's drawable when it is drawn. |
| 1378 | * Allows custom scaling, translation, and perspective distortion during an animation. |
| 1379 | * |
| 1380 | * This method is a lightweight analogue of {@link ImageView#setImageMatrix(Matrix)} to use |
| 1381 | * only during animations as this matrix will be cleared after the next drawable |
| 1382 | * update or view's bounds change. |
| 1383 | * |
| 1384 | * @param matrix The transformation parameters in matrix form. |
| 1385 | */ |
| 1386 | public void animateTransform(@Nullable Matrix matrix) { |
George Mount | f6c763d | 2014-09-25 15:13:15 -0700 | [diff] [blame] | 1387 | if (mDrawable == null) { |
| 1388 | return; |
| 1389 | } |
George Mount | 990205e | 2014-06-24 09:36:18 -0700 | [diff] [blame] | 1390 | if (matrix == null) { |
Andrey Kulikov | 72be974 | 2018-09-28 00:22:42 +0100 | [diff] [blame] | 1391 | final int vwidth = getWidth() - mPaddingLeft - mPaddingRight; |
| 1392 | final int vheight = getHeight() - mPaddingTop - mPaddingBottom; |
| 1393 | mDrawable.setBounds(0, 0, vwidth, vheight); |
Andrey Kulikov | 3ac312a | 2018-11-27 15:52:17 +0000 | [diff] [blame] | 1394 | mDrawMatrix = null; |
George Mount | 990205e | 2014-06-24 09:36:18 -0700 | [diff] [blame] | 1395 | } else { |
| 1396 | mDrawable.setBounds(0, 0, mDrawableWidth, mDrawableHeight); |
| 1397 | if (mDrawMatrix == null) { |
| 1398 | mDrawMatrix = new Matrix(); |
| 1399 | } |
| 1400 | mDrawMatrix.set(matrix); |
| 1401 | } |
| 1402 | invalidate(); |
| 1403 | } |
| 1404 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 1405 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | protected void onDraw(Canvas canvas) { |
| 1407 | super.onDraw(canvas); |
| 1408 | |
| 1409 | if (mDrawable == null) { |
| 1410 | return; // couldn't resolve the URI |
| 1411 | } |
| 1412 | |
| 1413 | if (mDrawableWidth == 0 || mDrawableHeight == 0) { |
| 1414 | return; // nothing to draw (empty bounds) |
| 1415 | } |
| 1416 | |
| 1417 | if (mDrawMatrix == null && mPaddingTop == 0 && mPaddingLeft == 0) { |
| 1418 | mDrawable.draw(canvas); |
| 1419 | } else { |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1420 | final int saveCount = canvas.getSaveCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | canvas.save(); |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1422 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | if (mCropToPadding) { |
| 1424 | final int scrollX = mScrollX; |
| 1425 | final int scrollY = mScrollY; |
| 1426 | canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop, |
| 1427 | scrollX + mRight - mLeft - mPaddingRight, |
| 1428 | scrollY + mBottom - mTop - mPaddingBottom); |
| 1429 | } |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1430 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | canvas.translate(mPaddingLeft, mPaddingTop); |
| 1432 | |
| 1433 | if (mDrawMatrix != null) { |
| 1434 | canvas.concat(mDrawMatrix); |
| 1435 | } |
| 1436 | mDrawable.draw(canvas); |
| 1437 | canvas.restoreToCount(saveCount); |
| 1438 | } |
| 1439 | } |
| 1440 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1441 | /** |
| 1442 | * <p>Return the offset of the widget's text baseline from the widget's top |
| 1443 | * boundary. </p> |
| 1444 | * |
| 1445 | * @return the offset of the baseline within the widget's bounds or -1 |
| 1446 | * if baseline alignment is not supported. |
| 1447 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | @Override |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 1449 | @InspectableProperty |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1450 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | public int getBaseline() { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1452 | if (mBaselineAlignBottom) { |
| 1453 | return getMeasuredHeight(); |
| 1454 | } else { |
| 1455 | return mBaseline; |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * <p>Set the offset of the widget's text baseline from the widget's top |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 1461 | * boundary. This value is overridden by the {@link #setBaselineAlignBottom(boolean)} |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1462 | * property.</p> |
| 1463 | * |
| 1464 | * @param baseline The baseline to use, or -1 if none is to be provided. |
| 1465 | * |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1466 | * @see #setBaseline(int) |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1467 | * @attr ref android.R.styleable#ImageView_baseline |
| 1468 | */ |
| 1469 | public void setBaseline(int baseline) { |
| 1470 | if (mBaseline != baseline) { |
| 1471 | mBaseline = baseline; |
| 1472 | requestLayout(); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | /** |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 1477 | * Sets whether the baseline of this view to the bottom of the view. |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1478 | * Setting this value overrides any calls to setBaseline. |
| 1479 | * |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 1480 | * @param aligned If true, the image view will be baseline aligned by its bottom edge. |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1481 | * |
| 1482 | * @attr ref android.R.styleable#ImageView_baselineAlignBottom |
| 1483 | */ |
| 1484 | public void setBaselineAlignBottom(boolean aligned) { |
| 1485 | if (mBaselineAlignBottom != aligned) { |
| 1486 | mBaselineAlignBottom = aligned; |
| 1487 | requestLayout(); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | /** |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 1492 | * Checks whether this view's baseline is considered the bottom of the view. |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1493 | * |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 1494 | * @return True if the ImageView's baseline is considered the bottom of the view, false if otherwise. |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1495 | * @see #setBaselineAlignBottom(boolean) |
| 1496 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 1497 | @InspectableProperty |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 1498 | public boolean getBaselineAlignBottom() { |
| 1499 | return mBaselineAlignBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | /** |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 1503 | * Sets a tinting option for the image. |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1504 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | * @param color Color tint to apply. |
| 1506 | * @param mode How to apply the color. The standard mode is |
| 1507 | * {@link PorterDuff.Mode#SRC_ATOP} |
Alan Viverette | 6aa92d1 | 2015-08-25 13:19:25 -0400 | [diff] [blame] | 1508 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | * @attr ref android.R.styleable#ImageView_tint |
| 1510 | */ |
| 1511 | public final void setColorFilter(int color, PorterDuff.Mode mode) { |
| 1512 | setColorFilter(new PorterDuffColorFilter(color, mode)); |
| 1513 | } |
| 1514 | |
Jeff Sharkey | 2b95c24 | 2010-02-08 17:40:30 -0800 | [diff] [blame] | 1515 | /** |
| 1516 | * Set a tinting option for the image. Assumes |
| 1517 | * {@link PorterDuff.Mode#SRC_ATOP} blending mode. |
| 1518 | * |
| 1519 | * @param color Color tint to apply. |
| 1520 | * @attr ref android.R.styleable#ImageView_tint |
| 1521 | */ |
| 1522 | @RemotableViewMethod |
| 1523 | public final void setColorFilter(int color) { |
| 1524 | setColorFilter(color, PorterDuff.Mode.SRC_ATOP); |
| 1525 | } |
| 1526 | |
Joe Fernandez | 704d43b | 2017-04-18 10:35:38 -0700 | [diff] [blame] | 1527 | /** |
| 1528 | * Removes the image's {@link android.graphics.ColorFilter}. |
| 1529 | * |
| 1530 | * @see #setColorFilter(int) |
| 1531 | * @see #getColorFilter() |
| 1532 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1533 | public final void clearColorFilter() { |
| 1534 | setColorFilter(null); |
| 1535 | } |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 1536 | |
| 1537 | /** |
Adam Powell | 31049d7 | 2013-10-07 12:58:42 -0700 | [diff] [blame] | 1538 | * @hide Candidate for future API inclusion |
| 1539 | */ |
| 1540 | public final void setXfermode(Xfermode mode) { |
| 1541 | if (mXfermode != mode) { |
| 1542 | mXfermode = mode; |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 1543 | mHasXfermode = true; |
| 1544 | applyXfermode(); |
Adam Powell | 31049d7 | 2013-10-07 12:58:42 -0700 | [diff] [blame] | 1545 | invalidate(); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /** |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 1550 | * Returns the active color filter for this ImageView. |
| 1551 | * |
| 1552 | * @return the active color filter for this ImageView |
| 1553 | * |
| 1554 | * @see #setColorFilter(android.graphics.ColorFilter) |
| 1555 | */ |
| 1556 | public ColorFilter getColorFilter() { |
| 1557 | return mColorFilter; |
| 1558 | } |
| 1559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | /** |
| 1561 | * Apply an arbitrary colorfilter to the image. |
| 1562 | * |
| 1563 | * @param cf the colorfilter to apply (may be null) |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 1564 | * |
| 1565 | * @see #getColorFilter() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | */ |
| 1567 | public void setColorFilter(ColorFilter cf) { |
| 1568 | if (mColorFilter != cf) { |
| 1569 | mColorFilter = cf; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1570 | mHasColorFilter = true; |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 1571 | applyColorFilter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | invalidate(); |
| 1573 | } |
| 1574 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1575 | |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 1576 | /** |
| 1577 | * Returns the alpha that will be applied to the drawable of this ImageView. |
| 1578 | * |
Kevin Hufnagle | bdb308a | 2016-09-23 15:54:42 -0700 | [diff] [blame] | 1579 | * @return the alpha value that will be applied to the drawable of this |
| 1580 | * ImageView (between 0 and 255 inclusive, with 0 being transparent and |
| 1581 | * 255 being opaque) |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 1582 | * |
| 1583 | * @see #setImageAlpha(int) |
| 1584 | */ |
| 1585 | public int getImageAlpha() { |
| 1586 | return mAlpha; |
| 1587 | } |
| 1588 | |
| 1589 | /** |
| 1590 | * Sets the alpha value that should be applied to the image. |
| 1591 | * |
Kevin Hufnagle | bdb308a | 2016-09-23 15:54:42 -0700 | [diff] [blame] | 1592 | * @param alpha the alpha value that should be applied to the image (between |
| 1593 | * 0 and 255 inclusive, with 0 being transparent and 255 being opaque) |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 1594 | * |
| 1595 | * @see #getImageAlpha() |
| 1596 | */ |
| 1597 | @RemotableViewMethod |
| 1598 | public void setImageAlpha(int alpha) { |
| 1599 | setAlpha(alpha); |
| 1600 | } |
| 1601 | |
| 1602 | /** |
| 1603 | * Sets the alpha value that should be applied to the image. |
| 1604 | * |
| 1605 | * @param alpha the alpha value that should be applied to the image |
| 1606 | * |
| 1607 | * @deprecated use #setImageAlpha(int) instead |
| 1608 | */ |
| 1609 | @Deprecated |
Jeff Sharkey | 2b95c24 | 2010-02-08 17:40:30 -0800 | [diff] [blame] | 1610 | @RemotableViewMethod |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | public void setAlpha(int alpha) { |
| 1612 | alpha &= 0xFF; // keep it legal |
| 1613 | if (mAlpha != alpha) { |
| 1614 | mAlpha = alpha; |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 1615 | mHasAlpha = true; |
| 1616 | applyAlpha(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | invalidate(); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 1621 | private void applyXfermode() { |
| 1622 | if (mDrawable != null && mHasXfermode) { |
Jeff Sharkey | 2b95c24 | 2010-02-08 17:40:30 -0800 | [diff] [blame] | 1623 | mDrawable = mDrawable.mutate(); |
Adam Powell | 31049d7 | 2013-10-07 12:58:42 -0700 | [diff] [blame] | 1624 | mDrawable.setXfermode(mXfermode); |
Nader Jawad | e6a6321 | 2018-04-20 15:56:10 -0700 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | private void applyColorFilter() { |
| 1629 | if (mDrawable != null && mHasColorFilter) { |
| 1630 | mDrawable = mDrawable.mutate(); |
| 1631 | mDrawable.setColorFilter(mColorFilter); |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | private void applyAlpha() { |
| 1636 | if (mDrawable != null && mHasAlpha) { |
| 1637 | mDrawable = mDrawable.mutate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8); |
| 1639 | } |
| 1640 | } |
Adam Powell | 37419d7 | 2011-11-10 11:32:09 -0800 | [diff] [blame] | 1641 | |
Alan Viverette | e10dd53 | 2013-12-12 19:19:51 -0800 | [diff] [blame] | 1642 | @Override |
| 1643 | public boolean isOpaque() { |
| 1644 | return super.isOpaque() || mDrawable != null && mXfermode == null |
| 1645 | && mDrawable.getOpacity() == PixelFormat.OPAQUE |
| 1646 | && mAlpha * mViewAlphaScale >> 8 == 255 |
| 1647 | && isFilledByImage(); |
| 1648 | } |
| 1649 | |
| 1650 | private boolean isFilledByImage() { |
| 1651 | if (mDrawable == null) { |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
| 1655 | final Rect bounds = mDrawable.getBounds(); |
| 1656 | final Matrix matrix = mDrawMatrix; |
| 1657 | if (matrix == null) { |
| 1658 | return bounds.left <= 0 && bounds.top <= 0 && bounds.right >= getWidth() |
| 1659 | && bounds.bottom >= getHeight(); |
| 1660 | } else if (matrix.rectStaysRect()) { |
| 1661 | final RectF boundsSrc = mTempSrc; |
| 1662 | final RectF boundsDst = mTempDst; |
| 1663 | boundsSrc.set(bounds); |
| 1664 | matrix.mapRect(boundsDst, boundsSrc); |
| 1665 | return boundsDst.left <= 0 && boundsDst.top <= 0 && boundsDst.right >= getWidth() |
| 1666 | && boundsDst.bottom >= getHeight(); |
| 1667 | } else { |
| 1668 | // If the matrix doesn't map to a rectangle, assume the worst. |
| 1669 | return false; |
| 1670 | } |
| 1671 | } |
| 1672 | |
Adam Powell | 37419d7 | 2011-11-10 11:32:09 -0800 | [diff] [blame] | 1673 | @Override |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1674 | public void onVisibilityAggregated(boolean isVisible) { |
| 1675 | super.onVisibilityAggregated(isVisible); |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1676 | // Only do this for new apps post-Nougat |
| 1677 | if (mDrawable != null && !sCompatDrawableVisibilityDispatch) { |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1678 | mDrawable.setVisible(isVisible, false); |
Adam Powell | 37419d7 | 2011-11-10 11:32:09 -0800 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Adam Powell | 06f9eb8 | 2016-08-24 17:09:01 -0700 | [diff] [blame] | 1682 | @RemotableViewMethod |
| 1683 | @Override |
| 1684 | public void setVisibility(int visibility) { |
| 1685 | super.setVisibility(visibility); |
| 1686 | // Only do this for old apps pre-Nougat; new apps use onVisibilityAggregated |
| 1687 | if (mDrawable != null && sCompatDrawableVisibilityDispatch) { |
| 1688 | mDrawable.setVisible(visibility == VISIBLE, false); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | @Override |
| 1693 | protected void onAttachedToWindow() { |
| 1694 | super.onAttachedToWindow(); |
| 1695 | // Only do this for old apps pre-Nougat; new apps use onVisibilityAggregated |
| 1696 | if (mDrawable != null && sCompatDrawableVisibilityDispatch) { |
| 1697 | mDrawable.setVisible(getVisibility() == VISIBLE, false); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | @Override |
| 1702 | protected void onDetachedFromWindow() { |
| 1703 | super.onDetachedFromWindow(); |
| 1704 | // Only do this for old apps pre-Nougat; new apps use onVisibilityAggregated |
| 1705 | if (mDrawable != null && sCompatDrawableVisibilityDispatch) { |
| 1706 | mDrawable.setVisible(false, false); |
| 1707 | } |
| 1708 | } |
| 1709 | |
Adam Powell | 37419d7 | 2011-11-10 11:32:09 -0800 | [diff] [blame] | 1710 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1711 | public CharSequence getAccessibilityClassName() { |
| 1712 | return ImageView.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1713 | } |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 1714 | |
| 1715 | /** @hide */ |
| 1716 | @Override |
| 1717 | protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 1718 | super.encodeProperties(stream); |
| 1719 | stream.addProperty("layout:baseline", getBaseline()); |
| 1720 | } |
Jiaquan He | 1dd48d0 | 2017-05-01 14:18:39 -0700 | [diff] [blame] | 1721 | |
| 1722 | /** @hide */ |
| 1723 | @Override |
| 1724 | @TestApi |
| 1725 | public boolean isDefaultFocusHighlightNeeded(Drawable background, Drawable foreground) { |
| 1726 | final boolean lackFocusState = mDrawable == null || !mDrawable.isStateful() |
| 1727 | || !mDrawable.hasFocusStateSpecified(); |
| 1728 | return super.isDefaultFocusHighlightNeeded(background, foreground) && lackFocusState; |
| 1729 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1730 | } |