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