| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.DrawableRes; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.TestApi; |
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.content.ContentResolver; |
| 25 |
import android.content.Context; |
| 26 |
import android.content.res.ColorStateList; |
| 27 |
import android.content.res.Resources; |
| 28 |
import android.content.res.TypedArray; |
| 29 |
import android.graphics.Bitmap; |
| 30 |
import android.graphics.BlendMode; |
| 31 |
import android.graphics.Canvas; |
| 32 |
import android.graphics.ColorFilter; |
| 33 |
import android.graphics.ImageDecoder; |
| 34 |
import android.graphics.Matrix; |
| 35 |
import android.graphics.PixelFormat; |
| 36 |
import android.graphics.PorterDuff; |
| 37 |
import android.graphics.PorterDuffColorFilter; |
| 38 |
import android.graphics.Rect; |
| 39 |
import android.graphics.RectF; |
| 40 |
import android.graphics.Xfermode; |
| 41 |
import android.graphics.drawable.BitmapDrawable; |
| 42 |
import android.graphics.drawable.Drawable; |
| 43 |
import android.graphics.drawable.Icon; |
| 44 |
import android.net.Uri; |
| 45 |
import android.os.Build; |
| 46 |
import android.os.Handler; |
| 47 |
import android.text.TextUtils; |
| 48 |
import android.util.AttributeSet; |
| 49 |
import android.util.Log; |
| 50 |
import android.view.RemotableViewMethod; |
| 51 |
import android.view.View; |
| 52 |
import android.view.ViewDebug; |
| 53 |
import android.view.ViewHierarchyEncoder; |
| 54 |
import android.view.accessibility.AccessibilityEvent; |
| 55 |
import android.view.inspector.InspectableProperty; |
| 56 |
import android.widget.RemoteViews.RemoteView; |
| 57 |
|
| 58 |
import com.android.internal.R; |
| 59 |
|
| 60 |
import java.io.IOException; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@RemoteView |
| 100 |
public class ImageView extends View { |
| 101 |
private static final String LOG_TAG = "ImageView"; |
| 102 |
|
| 103 |
|
| 104 |
@UnsupportedAppUsage |
| 105 |
private Uri mUri; |
| 106 |
@UnsupportedAppUsage |
| 107 |
private int mResource = 0; |
| 108 |
private Matrix mMatrix; |
| 109 |
private ScaleType mScaleType; |
| 110 |
private boolean mHaveFrame = false; |
| 111 |
@UnsupportedAppUsage |
| 112 |
private boolean mAdjustViewBounds = false; |
| 113 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 114 |
private int mMaxWidth = Integer.MAX_VALUE; |
| 115 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 116 |
private int mMaxHeight = Integer.MAX_VALUE; |
| 117 |
|
| 118 |
|
| 119 |
private ColorFilter mColorFilter = null; |
| 120 |
private boolean mHasColorFilter = false; |
| 121 |
private Xfermode mXfermode; |
| 122 |
private boolean mHasXfermode = false; |
| 123 |
@UnsupportedAppUsage |
| 124 |
private int mAlpha = 255; |
| 125 |
private boolean mHasAlpha = false; |
| 126 |
private final int mViewAlphaScale = 256; |
| 127 |
|
| 128 |
@UnsupportedAppUsage |
| 129 |
private Drawable mDrawable = null; |
| 130 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 131 |
private BitmapDrawable mRecycleableBitmapDrawable = null; |
| 132 |
private ColorStateList mDrawableTintList = null; |
| 133 |
private BlendMode mDrawableBlendMode = null; |
| 134 |
private boolean mHasDrawableTint = false; |
| 135 |
private boolean mHasDrawableBlendMode = false; |
| 136 |
|
| 137 |
private int[] mState = null; |
| 138 |
private boolean mMergeState = false; |
| 139 |
private boolean mHasLevelSet = false; |
| 140 |
private int mLevel = 0; |
| 141 |
@UnsupportedAppUsage |
| 142 |
private int mDrawableWidth; |
| 143 |
@UnsupportedAppUsage |
| 144 |
private int mDrawableHeight; |
| 145 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 124051687) |
| 146 |
private Matrix mDrawMatrix = null; |
| 147 |
|
| 148 |
|
| 149 |
private final RectF mTempSrc = new RectF(); |
| 150 |
private final RectF mTempDst = new RectF(); |
| 151 |
|
| 152 |
@UnsupportedAppUsage |
| 153 |
private boolean mCropToPadding; |
| 154 |
|
| 155 |
private int mBaseline = -1; |
| 156 |
private boolean mBaselineAlignBottom = false; |
| 157 |
|
| 158 |
|
| 159 |
private static boolean sCompatDone; |
| 160 |
|
| 161 |
|
| 162 |
private static boolean sCompatAdjustViewBounds; |
| 163 |
|
| 164 |
|
| 165 |
private static boolean sCompatUseCorrectStreamDensity; |
| 166 |
|
| 167 |
|
| 168 |
private static boolean sCompatDrawableVisibilityDispatch; |
| 169 |
|
| 170 |
private static final ScaleType[] sScaleTypeArray = { |
| 171 |
ScaleType.MATRIX, |
| 172 |
ScaleType.FIT_XY, |
| 173 |
ScaleType.FIT_START, |
| 174 |
ScaleType.FIT_CENTER, |
| 175 |
ScaleType.FIT_END, |
| 176 |
ScaleType.CENTER, |
| 177 |
ScaleType.CENTER_CROP, |
| 178 |
ScaleType.CENTER_INSIDE |
| 179 |
}; |
| 180 |
|
| 181 |
public ImageView(Context context) { |
| 182 |
super(context); |
| 183 |
initImageView(); |
| 184 |
} |
| 185 |
|
| 186 |
public ImageView(Context context, @Nullable AttributeSet attrs) { |
| 187 |
this(context, attrs, 0); |
| 188 |
} |
| 189 |
|
| 190 |
public ImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 191 |
this(context, attrs, defStyleAttr, 0); |
| 192 |
} |
| 193 |
|
| 194 |
public ImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, |
| 195 |
int defStyleRes) { |
| 196 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 197 |
|
| 198 |
initImageView(); |
| 199 |
|
| 200 |
final TypedArray a = context.obtainStyledAttributes( |
| 201 |
attrs, R.styleable.ImageView, defStyleAttr, defStyleRes); |
| 202 |
saveAttributeDataForStyleable(context, R.styleable.ImageView, |
| 203 |
attrs, a, defStyleAttr, defStyleRes); |
| 204 |
|
| 205 |
final Drawable d = a.getDrawable(R.styleable.ImageView_src); |
| 206 |
if (d != null) { |
| 207 |
setImageDrawable(d); |
| 208 |
} |
| 209 |
|
| 210 |
mBaselineAlignBottom = a.getBoolean(R.styleable.ImageView_baselineAlignBottom, false); |
| 211 |
mBaseline = a.getDimensionPixelSize(R.styleable.ImageView_baseline, -1); |
| 212 |
|
| 213 |
setAdjustViewBounds(a.getBoolean(R.styleable.ImageView_adjustViewBounds, false)); |
| 214 |
setMaxWidth(a.getDimensionPixelSize(R.styleable.ImageView_maxWidth, Integer.MAX_VALUE)); |
| 215 |
setMaxHeight(a.getDimensionPixelSize(R.styleable.ImageView_maxHeight, Integer.MAX_VALUE)); |
| 216 |
|
| 217 |
final int index = a.getInt(R.styleable.ImageView_scaleType, -1); |
| 218 |
if (index >= 0) { |
| 219 |
setScaleType(sScaleTypeArray[index]); |
| 220 |
} |
| 221 |
|
| 222 |
if (a.hasValue(R.styleable.ImageView_tint)) { |
| 223 |
mDrawableTintList = a.getColorStateList(R.styleable.ImageView_tint); |
| 224 |
mHasDrawableTint = true; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
mDrawableBlendMode = BlendMode.SRC_ATOP; |
| 229 |
mHasDrawableBlendMode = true; |
| 230 |
} |
| 231 |
|
| 232 |
if (a.hasValue(R.styleable.ImageView_tintMode)) { |
| 233 |
mDrawableBlendMode = Drawable.parseBlendMode(a.getInt( |
| 234 |
R.styleable.ImageView_tintMode, -1), mDrawableBlendMode); |
| 235 |
mHasDrawableBlendMode = true; |
| 236 |
} |
| 237 |
|
| 238 |
applyImageTint(); |
| 239 |
|
| 240 |
final int alpha = a.getInt(R.styleable.ImageView_drawableAlpha, 255); |
| 241 |
if (alpha != 255) { |
| 242 |
setImageAlpha(alpha); |
| 243 |
} |
| 244 |
|
| 245 |
mCropToPadding = a.getBoolean( |
| 246 |
R.styleable.ImageView_cropToPadding, false); |
| 247 |
|
| 248 |
a.recycle(); |
| 249 |
|
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
private void initImageView() { |
| 254 |
mMatrix = new Matrix(); |
| 255 |
mScaleType = ScaleType.FIT_CENTER; |
| 256 |
|
| 257 |
if (!sCompatDone) { |
| 258 |
final int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion; |
| 259 |
sCompatAdjustViewBounds = targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 260 |
sCompatUseCorrectStreamDensity = targetSdkVersion > Build.VERSION_CODES.M; |
| 261 |
sCompatDrawableVisibilityDispatch = targetSdkVersion < Build.VERSION_CODES.N; |
| 262 |
sCompatDone = true; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) { |
| 268 |
setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_NO); |
| 269 |
} |
| 270 |
if (getImportantForContentCapture() == IMPORTANT_FOR_CONTENT_CAPTURE_AUTO) { |
| 271 |
setImportantForContentCapture(IMPORTANT_FOR_CONTENT_CAPTURE_YES); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
protected boolean verifyDrawable(@NonNull Drawable dr) { |
| 277 |
return mDrawable == dr || super.verifyDrawable(dr); |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public void jumpDrawablesToCurrentState() { |
| 282 |
super.jumpDrawablesToCurrentState(); |
| 283 |
if (mDrawable != null) mDrawable.jumpToCurrentState(); |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public void invalidateDrawable(@NonNull Drawable dr) { |
| 288 |
if (dr == mDrawable) { |
| 289 |
if (dr != null) { |
| 290 |
|
| 291 |
final int w = dr.getIntrinsicWidth(); |
| 292 |
final int h = dr.getIntrinsicHeight(); |
| 293 |
if (w != mDrawableWidth || h != mDrawableHeight) { |
| 294 |
mDrawableWidth = w; |
| 295 |
mDrawableHeight = h; |
| 296 |
|
| 297 |
configureBounds(); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
invalidate(); |
| 308 |
} else { |
| 309 |
super.invalidateDrawable(dr); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public boolean hasOverlappingRendering() { |
| 315 |
return (getBackground() != null && getBackground().getCurrent() != null); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
@Override |
| 320 |
public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
| 321 |
super.onPopulateAccessibilityEventInternal(event); |
| 322 |
|
| 323 |
final CharSequence contentDescription = getContentDescription(); |
| 324 |
if (!TextUtils.isEmpty(contentDescription)) { |
| 325 |
event.getText().add(contentDescription); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
@InspectableProperty |
| 341 |
public boolean getAdjustViewBounds() { |
| 342 |
return mAdjustViewBounds; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
@android.view.RemotableViewMethod |
| 363 |
public void setAdjustViewBounds(boolean adjustViewBounds) { |
| 364 |
mAdjustViewBounds = adjustViewBounds; |
| 365 |
if (adjustViewBounds) { |
| 366 |
setScaleType(ScaleType.FIT_CENTER); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
@InspectableProperty |
| 380 |
public int getMaxWidth() { |
| 381 |
return mMaxWidth; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
@android.view.RemotableViewMethod |
| 405 |
public void setMaxWidth(int maxWidth) { |
| 406 |
mMaxWidth = maxWidth; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
@InspectableProperty |
| 419 |
public int getMaxHeight() { |
| 420 |
return mMaxHeight; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
@android.view.RemotableViewMethod |
| 444 |
public void setMaxHeight(int maxHeight) { |
| 445 |
mMaxHeight = maxHeight; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
@InspectableProperty(name = "src") |
| 456 |
public Drawable getDrawable() { |
| 457 |
if (mDrawable == mRecycleableBitmapDrawable) { |
| 458 |
|
| 459 |
mRecycleableBitmapDrawable = null; |
| 460 |
} |
| 461 |
return mDrawable; |
| 462 |
} |
| 463 |
|
| 464 |
private class ImageDrawableCallback implements Runnable { |
| 465 |
|
| 466 |
private final Drawable drawable; |
| 467 |
private final Uri uri; |
| 468 |
private final int resource; |
| 469 |
|
| 470 |
ImageDrawableCallback(Drawable drawable, Uri uri, int resource) { |
| 471 |
this.drawable = drawable; |
| 472 |
this.uri = uri; |
| 473 |
this.resource = resource; |
| 474 |
} |
| 475 |
|
| 476 |
@Override |
| 477 |
public void run() { |
| 478 |
setImageDrawable(drawable); |
| 479 |
mUri = uri; |
| 480 |
mResource = resource; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
@android.view.RemotableViewMethod(asyncImpl="setImageResourceAsync") |
| 497 |
public void setImageResource(@DrawableRes int resId) { |
| 498 |
|
| 499 |
|
| 500 |
final int oldWidth = mDrawableWidth; |
| 501 |
final int oldHeight = mDrawableHeight; |
| 502 |
|
| 503 |
updateDrawable(null); |
| 504 |
mResource = resId; |
| 505 |
mUri = null; |
| 506 |
|
| 507 |
resolveUri(); |
| 508 |
|
| 509 |
if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) { |
| 510 |
requestLayout(); |
| 511 |
} |
| 512 |
invalidate(); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
@UnsupportedAppUsage |
| 517 |
public Runnable setImageResourceAsync(@DrawableRes int resId) { |
| 518 |
Drawable d = null; |
| 519 |
if (resId != 0) { |
| 520 |
try { |
| 521 |
d = getContext().getDrawable(resId); |
| 522 |
} catch (Exception e) { |
| 523 |
Log.w(LOG_TAG, "Unable to find resource: " + resId, e); |
| 524 |
resId = 0; |
| 525 |
} |
| 526 |
} |
| 527 |
return new ImageDrawableCallback(d, null, resId); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
@android.view.RemotableViewMethod(asyncImpl="setImageURIAsync") |
| 553 |
public void setImageURI(@Nullable Uri uri) { |
| 554 |
if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) { |
| 555 |
updateDrawable(null); |
| 556 |
mResource = 0; |
| 557 |
mUri = uri; |
| 558 |
|
| 559 |
final int oldWidth = mDrawableWidth; |
| 560 |
final int oldHeight = mDrawableHeight; |
| 561 |
|
| 562 |
resolveUri(); |
| 563 |
|
| 564 |
if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) { |
| 565 |
requestLayout(); |
| 566 |
} |
| 567 |
invalidate(); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
@UnsupportedAppUsage |
| 573 |
public Runnable setImageURIAsync(@Nullable Uri uri) { |
| 574 |
if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) { |
| 575 |
Drawable d = uri == null ? null : getDrawableFromUri(uri); |
| 576 |
if (d == null) { |
| 577 |
|
| 578 |
uri = null; |
| 579 |
} |
| 580 |
return new ImageDrawableCallback(d, uri, 0); |
| 581 |
} |
| 582 |
return null; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public void setImageDrawable(@Nullable Drawable drawable) { |
| 592 |
if (mDrawable != drawable) { |
| 593 |
mResource = 0; |
| 594 |
mUri = null; |
| 595 |
|
| 596 |
final int oldWidth = mDrawableWidth; |
| 597 |
final int oldHeight = mDrawableHeight; |
| 598 |
|
| 599 |
updateDrawable(drawable); |
| 600 |
|
| 601 |
if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) { |
| 602 |
requestLayout(); |
| 603 |
} |
| 604 |
invalidate(); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
@android.view.RemotableViewMethod(asyncImpl="setImageIconAsync") |
| 622 |
public void setImageIcon(@Nullable Icon icon) { |
| 623 |
setImageDrawable(icon == null ? null : icon.loadDrawable(mContext)); |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
public Runnable setImageIconAsync(@Nullable Icon icon) { |
| 628 |
return new ImageDrawableCallback(icon == null ? null : icon.loadDrawable(mContext), null, 0); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
@android.view.RemotableViewMethod |
| 653 |
public void setImageTintList(@Nullable ColorStateList tint) { |
| 654 |
mDrawableTintList = tint; |
| 655 |
mHasDrawableTint = true; |
| 656 |
|
| 657 |
applyImageTint(); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
@Nullable |
| 669 |
@InspectableProperty(name = "tint") |
| 670 |
public ColorStateList getImageTintList() { |
| 671 |
return mDrawableTintList; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
public void setImageTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 686 |
setImageTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
@RemotableViewMethod |
| 701 |
public void setImageTintBlendMode(@Nullable BlendMode blendMode) { |
| 702 |
mDrawableBlendMode = blendMode; |
| 703 |
mHasDrawableBlendMode = true; |
| 704 |
|
| 705 |
applyImageTint(); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
@Nullable |
| 715 |
@InspectableProperty(name = "tintMode") |
| 716 |
public PorterDuff.Mode getImageTintMode() { |
| 717 |
return mDrawableBlendMode != null |
| 718 |
? BlendMode.blendModeToPorterDuffMode(mDrawableBlendMode) : null; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
@Nullable |
| 728 |
@InspectableProperty(name = "blendMode", attributeId = android.R.styleable.ImageView_tintMode) |
| 729 |
public BlendMode getImageTintBlendMode() { |
| 730 |
return mDrawableBlendMode; |
| 731 |
} |
| 732 |
|
| 733 |
private void applyImageTint() { |
| 734 |
if (mDrawable != null && (mHasDrawableTint || mHasDrawableBlendMode)) { |
| 735 |
mDrawable = mDrawable.mutate(); |
| 736 |
|
| 737 |
if (mHasDrawableTint) { |
| 738 |
mDrawable.setTintList(mDrawableTintList); |
| 739 |
} |
| 740 |
|
| 741 |
if (mHasDrawableBlendMode) { |
| 742 |
mDrawable.setTintBlendMode(mDrawableBlendMode); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
if (mDrawable.isStateful()) { |
| 748 |
mDrawable.setState(getDrawableState()); |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
@android.view.RemotableViewMethod |
| 759 |
public void setImageBitmap(Bitmap bm) { |
| 760 |
|
| 761 |
|
| 762 |
mDrawable = null; |
| 763 |
if (mRecycleableBitmapDrawable == null) { |
| 764 |
mRecycleableBitmapDrawable = new BitmapDrawable(mContext.getResources(), bm); |
| 765 |
} else { |
| 766 |
mRecycleableBitmapDrawable.setBitmap(bm); |
| 767 |
} |
| 768 |
setImageDrawable(mRecycleableBitmapDrawable); |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
public void setImageState(int[] state, boolean merge) { |
| 779 |
mState = state; |
| 780 |
mMergeState = merge; |
| 781 |
if (mDrawable != null) { |
| 782 |
refreshDrawableState(); |
| 783 |
resizeFromDrawable(); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
@Override |
| 788 |
public void setSelected(boolean selected) { |
| 789 |
super.setSelected(selected); |
| 790 |
resizeFromDrawable(); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
@android.view.RemotableViewMethod |
| 800 |
public void setImageLevel(int level) { |
| 801 |
mLevel = level; |
| 802 |
mHasLevelSet = true; |
| 803 |
if (mDrawable != null) { |
| 804 |
mDrawable.setLevel(level); |
| 805 |
resizeFromDrawable(); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
public enum ScaleType { |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
MATRIX (0), |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
FIT_XY (1), |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
FIT_START (2), |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
FIT_CENTER (3), |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
FIT_END (4), |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
CENTER (5), |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
CENTER_CROP (6), |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
CENTER_INSIDE (7); |
| 861 |
|
| 862 |
ScaleType(int ni) { |
| 863 |
nativeInt = ni; |
| 864 |
} |
| 865 |
final int nativeInt; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
public void setScaleType(ScaleType scaleType) { |
| 877 |
if (scaleType == null) { |
| 878 |
throw new NullPointerException(); |
| 879 |
} |
| 880 |
|
| 881 |
if (mScaleType != scaleType) { |
| 882 |
mScaleType = scaleType; |
| 883 |
|
| 884 |
requestLayout(); |
| 885 |
invalidate(); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
@InspectableProperty |
| 896 |
public ScaleType getScaleType() { |
| 897 |
return mScaleType; |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
public Matrix getImageMatrix() { |
| 908 |
if (mDrawMatrix == null) { |
| 909 |
return new Matrix(Matrix.IDENTITY_MATRIX); |
| 910 |
} |
| 911 |
return mDrawMatrix; |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
public void setImageMatrix(Matrix matrix) { |
| 922 |
|
| 923 |
if (matrix != null && matrix.isIdentity()) { |
| 924 |
matrix = null; |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
if (matrix == null && !mMatrix.isIdentity() || |
| 929 |
matrix != null && !mMatrix.equals(matrix)) { |
| 930 |
mMatrix.set(matrix); |
| 931 |
configureBounds(); |
| 932 |
invalidate(); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
@InspectableProperty |
| 946 |
public boolean getCropToPadding() { |
| 947 |
return mCropToPadding; |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
public void setCropToPadding(boolean cropToPadding) { |
| 960 |
if (mCropToPadding != cropToPadding) { |
| 961 |
mCropToPadding = cropToPadding; |
| 962 |
requestLayout(); |
| 963 |
invalidate(); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
@UnsupportedAppUsage |
| 968 |
private void resolveUri() { |
| 969 |
if (mDrawable != null) { |
| 970 |
return; |
| 971 |
} |
| 972 |
|
| 973 |
if (getResources() == null) { |
| 974 |
return; |
| 975 |
} |
| 976 |
|
| 977 |
Drawable d = null; |
| 978 |
|
| 979 |
if (mResource != 0) { |
| 980 |
try { |
| 981 |
d = mContext.getDrawable(mResource); |
| 982 |
} catch (Exception e) { |
| 983 |
Log.w(LOG_TAG, "Unable to find resource: " + mResource, e); |
| 984 |
|
| 985 |
mResource = 0; |
| 986 |
} |
| 987 |
} else if (mUri != null) { |
| 988 |
d = getDrawableFromUri(mUri); |
| 989 |
|
| 990 |
if (d == null) { |
| 991 |
Log.w(LOG_TAG, "resolveUri failed on bad bitmap uri: " + mUri); |
| 992 |
|
| 993 |
mUri = null; |
| 994 |
} |
| 995 |
} else { |
| 996 |
return; |
| 997 |
} |
| 998 |
|
| 999 |
updateDrawable(d); |
| 1000 |
} |
| 1001 |
|
| 1002 |
private Drawable getDrawableFromUri(Uri uri) { |
| 1003 |
final String scheme = uri.getScheme(); |
| 1004 |
if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { |
| 1005 |
try { |
| 1006 |
|
| 1007 |
ContentResolver.OpenResourceIdResult r = |
| 1008 |
mContext.getContentResolver().getResourceId(uri); |
| 1009 |
return r.r.getDrawable(r.id, mContext.getTheme()); |
| 1010 |
} catch (Exception e) { |
| 1011 |
Log.w(LOG_TAG, "Unable to open content: " + uri, e); |
| 1012 |
} |
| 1013 |
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme) |
| 1014 |
|| ContentResolver.SCHEME_FILE.equals(scheme)) { |
| 1015 |
try { |
| 1016 |
Resources res = sCompatUseCorrectStreamDensity ? getResources() : null; |
| 1017 |
ImageDecoder.Source src = ImageDecoder.createSource(mContext.getContentResolver(), |
| 1018 |
uri, res); |
| 1019 |
return ImageDecoder.decodeDrawable(src, (decoder, info, s) -> { |
| 1020 |
decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE); |
| 1021 |
}); |
| 1022 |
} catch (IOException e) { |
| 1023 |
Log.w(LOG_TAG, "Unable to open content: " + uri, e); |
| 1024 |
} |
| 1025 |
} else { |
| 1026 |
return Drawable.createFromPath(uri.toString()); |
| 1027 |
} |
| 1028 |
return null; |
| 1029 |
} |
| 1030 |
|
| 1031 |
@Override |
| 1032 |
public int[] onCreateDrawableState(int extraSpace) { |
| 1033 |
if (mState == null) { |
| 1034 |
return super.onCreateDrawableState(extraSpace); |
| 1035 |
} else if (!mMergeState) { |
| 1036 |
return mState; |
| 1037 |
} else { |
| 1038 |
return mergeDrawableStates( |
| 1039 |
super.onCreateDrawableState(extraSpace + mState.length), mState); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
@UnsupportedAppUsage |
| 1044 |
private void updateDrawable(Drawable d) { |
| 1045 |
if (d != mRecycleableBitmapDrawable && mRecycleableBitmapDrawable != null) { |
| 1046 |
mRecycleableBitmapDrawable.setBitmap(null); |
| 1047 |
} |
| 1048 |
|
| 1049 |
boolean sameDrawable = false; |
| 1050 |
|
| 1051 |
if (mDrawable != null) { |
| 1052 |
sameDrawable = mDrawable == d; |
| 1053 |
mDrawable.setCallback(null); |
| 1054 |
unscheduleDrawable(mDrawable); |
| 1055 |
if (!sCompatDrawableVisibilityDispatch && !sameDrawable && isAttachedToWindow()) { |
| 1056 |
mDrawable.setVisible(false, false); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
mDrawable = d; |
| 1061 |
|
| 1062 |
if (d != null) { |
| 1063 |
d.setCallback(this); |
| 1064 |
d.setLayoutDirection(getLayoutDirection()); |
| 1065 |
if (d.isStateful()) { |
| 1066 |
d.setState(getDrawableState()); |
| 1067 |
} |
| 1068 |
if (!sameDrawable || sCompatDrawableVisibilityDispatch) { |
| 1069 |
final boolean visible = sCompatDrawableVisibilityDispatch |
| 1070 |
? getVisibility() == VISIBLE |
| 1071 |
: isAttachedToWindow() && getWindowVisibility() == VISIBLE && isShown(); |
| 1072 |
d.setVisible(visible, true); |
| 1073 |
} |
| 1074 |
if (mHasLevelSet) { |
| 1075 |
d.setLevel(mLevel); |
| 1076 |
} |
| 1077 |
mDrawableWidth = d.getIntrinsicWidth(); |
| 1078 |
mDrawableHeight = d.getIntrinsicHeight(); |
| 1079 |
applyImageTint(); |
| 1080 |
applyColorFilter(); |
| 1081 |
applyAlpha(); |
| 1082 |
applyXfermode(); |
| 1083 |
|
| 1084 |
configureBounds(); |
| 1085 |
} else { |
| 1086 |
mDrawableWidth = mDrawableHeight = -1; |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
@UnsupportedAppUsage |
| 1091 |
private void resizeFromDrawable() { |
| 1092 |
final Drawable d = mDrawable; |
| 1093 |
if (d != null) { |
| 1094 |
int w = d.getIntrinsicWidth(); |
| 1095 |
if (w < 0) w = mDrawableWidth; |
| 1096 |
int h = d.getIntrinsicHeight(); |
| 1097 |
if (h < 0) h = mDrawableHeight; |
| 1098 |
if (w != mDrawableWidth || h != mDrawableHeight) { |
| 1099 |
mDrawableWidth = w; |
| 1100 |
mDrawableHeight = h; |
| 1101 |
requestLayout(); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
@Override |
| 1107 |
public void onRtlPropertiesChanged(int layoutDirection) { |
| 1108 |
super.onRtlPropertiesChanged(layoutDirection); |
| 1109 |
|
| 1110 |
if (mDrawable != null) { |
| 1111 |
mDrawable.setLayoutDirection(layoutDirection); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
private static final Matrix.ScaleToFit[] sS2FArray = { |
| 1116 |
Matrix.ScaleToFit.FILL, |
| 1117 |
Matrix.ScaleToFit.START, |
| 1118 |
Matrix.ScaleToFit.CENTER, |
| 1119 |
Matrix.ScaleToFit.END |
| 1120 |
}; |
| 1121 |
|
| 1122 |
@UnsupportedAppUsage |
| 1123 |
private static Matrix.ScaleToFit scaleTypeToScaleToFit(ScaleType st) { |
| 1124 |
|
| 1125 |
return sS2FArray[st.nativeInt - 1]; |
| 1126 |
} |
| 1127 |
|
| 1128 |
@Override |
| 1129 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 1130 |
resolveUri(); |
| 1131 |
int w; |
| 1132 |
int h; |
| 1133 |
|
| 1134 |
|
| 1135 |
float desiredAspect = 0.0f; |
| 1136 |
|
| 1137 |
|
| 1138 |
boolean resizeWidth = false; |
| 1139 |
|
| 1140 |
|
| 1141 |
boolean resizeHeight = false; |
| 1142 |
|
| 1143 |
final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 1144 |
final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 1145 |
|
| 1146 |
if (mDrawable == null) { |
| 1147 |
|
| 1148 |
mDrawableWidth = -1; |
| 1149 |
mDrawableHeight = -1; |
| 1150 |
w = h = 0; |
| 1151 |
} else { |
| 1152 |
w = mDrawableWidth; |
| 1153 |
h = mDrawableHeight; |
| 1154 |
if (w <= 0) w = 1; |
| 1155 |
if (h <= 0) h = 1; |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
if (mAdjustViewBounds) { |
| 1160 |
resizeWidth = widthSpecMode != MeasureSpec.EXACTLY; |
| 1161 |
resizeHeight = heightSpecMode != MeasureSpec.EXACTLY; |
| 1162 |
|
| 1163 |
desiredAspect = (float) w / (float) h; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
final int pleft = mPaddingLeft; |
| 1168 |
final int pright = mPaddingRight; |
| 1169 |
final int ptop = mPaddingTop; |
| 1170 |
final int pbottom = mPaddingBottom; |
| 1171 |
|
| 1172 |
int widthSize; |
| 1173 |
int heightSize; |
| 1174 |
|
| 1175 |
if (resizeWidth || resizeHeight) { |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
widthSize = resolveAdjustedSize(w + pleft + pright, mMaxWidth, widthMeasureSpec); |
| 1183 |
|
| 1184 |
|
| 1185 |
heightSize = resolveAdjustedSize(h + ptop + pbottom, mMaxHeight, heightMeasureSpec); |
| 1186 |
|
| 1187 |
if (desiredAspect != 0.0f) { |
| 1188 |
|
| 1189 |
final float actualAspect = (float)(widthSize - pleft - pright) / |
| 1190 |
(heightSize - ptop - pbottom); |
| 1191 |
|
| 1192 |
if (Math.abs(actualAspect - desiredAspect) > 0.0000001) { |
| 1193 |
|
| 1194 |
boolean done = false; |
| 1195 |
|
| 1196 |
|
| 1197 |
if (resizeWidth) { |
| 1198 |
int newWidth = (int)(desiredAspect * (heightSize - ptop - pbottom)) + |
| 1199 |
pleft + pright; |
| 1200 |
|
| 1201 |
|
| 1202 |
if (!resizeHeight && !sCompatAdjustViewBounds) { |
| 1203 |
widthSize = resolveAdjustedSize(newWidth, mMaxWidth, widthMeasureSpec); |
| 1204 |
} |
| 1205 |
|
| 1206 |
if (newWidth <= widthSize) { |
| 1207 |
widthSize = newWidth; |
| 1208 |
done = true; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
if (!done && resizeHeight) { |
| 1214 |
int newHeight = (int)((widthSize - pleft - pright) / desiredAspect) + |
| 1215 |
ptop + pbottom; |
| 1216 |
|
| 1217 |
|
| 1218 |
if (!resizeWidth && !sCompatAdjustViewBounds) { |
| 1219 |
heightSize = resolveAdjustedSize(newHeight, mMaxHeight, |
| 1220 |
heightMeasureSpec); |
| 1221 |
} |
| 1222 |
|
| 1223 |
if (newHeight <= heightSize) { |
| 1224 |
heightSize = newHeight; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
} |
| 1229 |
} else { |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
w += pleft + pright; |
| 1235 |
h += ptop + pbottom; |
| 1236 |
|
| 1237 |
w = Math.max(w, getSuggestedMinimumWidth()); |
| 1238 |
h = Math.max(h, getSuggestedMinimumHeight()); |
| 1239 |
|
| 1240 |
widthSize = resolveSizeAndState(w, widthMeasureSpec, 0); |
| 1241 |
heightSize = resolveSizeAndState(h, heightMeasureSpec, 0); |
| 1242 |
} |
| 1243 |
|
| 1244 |
setMeasuredDimension(widthSize, heightSize); |
| 1245 |
} |
| 1246 |
|
| 1247 |
private int resolveAdjustedSize(int desiredSize, int maxSize, |
| 1248 |
int measureSpec) { |
| 1249 |
int result = desiredSize; |
| 1250 |
final int specMode = MeasureSpec.getMode(measureSpec); |
| 1251 |
final int specSize = MeasureSpec.getSize(measureSpec); |
| 1252 |
switch (specMode) { |
| 1253 |
case MeasureSpec.UNSPECIFIED: |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
result = Math.min(desiredSize, maxSize); |
| 1258 |
break; |
| 1259 |
case MeasureSpec.AT_MOST: |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
result = Math.min(Math.min(desiredSize, specSize), maxSize); |
| 1264 |
break; |
| 1265 |
case MeasureSpec.EXACTLY: |
| 1266 |
|
| 1267 |
result = specSize; |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
return result; |
| 1271 |
} |
| 1272 |
|
| 1273 |
@Override |
| 1274 |
protected boolean setFrame(int l, int t, int r, int b) { |
| 1275 |
final boolean changed = super.setFrame(l, t, r, b); |
| 1276 |
mHaveFrame = true; |
| 1277 |
configureBounds(); |
| 1278 |
return changed; |
| 1279 |
} |
| 1280 |
|
| 1281 |
private void configureBounds() { |
| 1282 |
if (mDrawable == null || !mHaveFrame) { |
| 1283 |
return; |
| 1284 |
} |
| 1285 |
|
| 1286 |
final int dwidth = mDrawableWidth; |
| 1287 |
final int dheight = mDrawableHeight; |
| 1288 |
|
| 1289 |
final int vwidth = getWidth() - mPaddingLeft - mPaddingRight; |
| 1290 |
final int vheight = getHeight() - mPaddingTop - mPaddingBottom; |
| 1291 |
|
| 1292 |
final boolean fits = (dwidth < 0 || vwidth == dwidth) |
| 1293 |
&& (dheight < 0 || vheight == dheight); |
| 1294 |
|
| 1295 |
if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) { |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
mDrawable.setBounds(0, 0, vwidth, vheight); |
| 1300 |
mDrawMatrix = null; |
| 1301 |
} else { |
| 1302 |
|
| 1303 |
|
| 1304 |
mDrawable.setBounds(0, 0, dwidth, dheight); |
| 1305 |
|
| 1306 |
if (ScaleType.MATRIX == mScaleType) { |
| 1307 |
|
| 1308 |
if (mMatrix.isIdentity()) { |
| 1309 |
mDrawMatrix = null; |
| 1310 |
} else { |
| 1311 |
mDrawMatrix = mMatrix; |
| 1312 |
} |
| 1313 |
} else if (fits) { |
| 1314 |
|
| 1315 |
mDrawMatrix = null; |
| 1316 |
} else if (ScaleType.CENTER == mScaleType) { |
| 1317 |
|
| 1318 |
mDrawMatrix = mMatrix; |
| 1319 |
mDrawMatrix.setTranslate(Math.round((vwidth - dwidth) * 0.5f), |
| 1320 |
Math.round((vheight - dheight) * 0.5f)); |
| 1321 |
} else if (ScaleType.CENTER_CROP == mScaleType) { |
| 1322 |
mDrawMatrix = mMatrix; |
| 1323 |
|
| 1324 |
float scale; |
| 1325 |
float dx = 0, dy = 0; |
| 1326 |
|
| 1327 |
if (dwidth * vheight > vwidth * dheight) { |
| 1328 |
scale = (float) vheight / (float) dheight; |
| 1329 |
dx = (vwidth - dwidth * scale) * 0.5f; |
| 1330 |
} else { |
| 1331 |
scale = (float) vwidth / (float) dwidth; |
| 1332 |
dy = (vheight - dheight * scale) * 0.5f; |
| 1333 |
} |
| 1334 |
|
| 1335 |
mDrawMatrix.setScale(scale, scale); |
| 1336 |
mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy)); |
| 1337 |
} else if (ScaleType.CENTER_INSIDE == mScaleType) { |
| 1338 |
mDrawMatrix = mMatrix; |
| 1339 |
float scale; |
| 1340 |
float dx; |
| 1341 |
float dy; |
| 1342 |
|
| 1343 |
if (dwidth <= vwidth && dheight <= vheight) { |
| 1344 |
scale = 1.0f; |
| 1345 |
} else { |
| 1346 |
scale = Math.min((float) vwidth / (float) dwidth, |
| 1347 |
(float) vheight / (float) dheight); |
| 1348 |
} |
| 1349 |
|
| 1350 |
dx = Math.round((vwidth - dwidth * scale) * 0.5f); |
| 1351 |
dy = Math.round((vheight - dheight * scale) * 0.5f); |
| 1352 |
|
| 1353 |
mDrawMatrix.setScale(scale, scale); |
| 1354 |
mDrawMatrix.postTranslate(dx, dy); |
| 1355 |
} else { |
| 1356 |
|
| 1357 |
mTempSrc.set(0, 0, dwidth, dheight); |
| 1358 |
mTempDst.set(0, 0, vwidth, vheight); |
| 1359 |
|
| 1360 |
mDrawMatrix = mMatrix; |
| 1361 |
mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType)); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
@Override |
| 1367 |
protected void drawableStateChanged() { |
| 1368 |
super.drawableStateChanged(); |
| 1369 |
|
| 1370 |
final Drawable drawable = mDrawable; |
| 1371 |
if (drawable != null && drawable.isStateful() |
| 1372 |
&& drawable.setState(getDrawableState())) { |
| 1373 |
invalidateDrawable(drawable); |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
@Override |
| 1378 |
public void drawableHotspotChanged(float x, float y) { |
| 1379 |
super.drawableHotspotChanged(x, y); |
| 1380 |
|
| 1381 |
if (mDrawable != null) { |
| 1382 |
mDrawable.setHotspot(x, y); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
public void animateTransform(@Nullable Matrix matrix) { |
| 1397 |
if (mDrawable == null) { |
| 1398 |
return; |
| 1399 |
} |
| 1400 |
if (matrix == null) { |
| 1401 |
final int vwidth = getWidth() - mPaddingLeft - mPaddingRight; |
| 1402 |
final int vheight = getHeight() - mPaddingTop - mPaddingBottom; |
| 1403 |
mDrawable.setBounds(0, 0, vwidth, vheight); |
| 1404 |
mDrawMatrix = null; |
| 1405 |
} else { |
| 1406 |
mDrawable.setBounds(0, 0, mDrawableWidth, mDrawableHeight); |
| 1407 |
if (mDrawMatrix == null) { |
| 1408 |
mDrawMatrix = new Matrix(); |
| 1409 |
} |
| 1410 |
mDrawMatrix.set(matrix); |
| 1411 |
} |
| 1412 |
invalidate(); |
| 1413 |
} |
| 1414 |
|
| 1415 |
@Override |
| 1416 |
protected void onDraw(Canvas canvas) { |
| 1417 |
super.onDraw(canvas); |
| 1418 |
|
| 1419 |
if (mDrawable == null) { |
| 1420 |
return; |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (mDrawableWidth == 0 || mDrawableHeight == 0) { |
| 1424 |
return; |
| 1425 |
} |
| 1426 |
|
| 1427 |
if (mDrawMatrix == null && mPaddingTop == 0 && mPaddingLeft == 0) { |
| 1428 |
mDrawable.draw(canvas); |
| 1429 |
} else { |
| 1430 |
final int saveCount = canvas.getSaveCount(); |
| 1431 |
canvas.save(); |
| 1432 |
|
| 1433 |
if (mCropToPadding) { |
| 1434 |
final int scrollX = mScrollX; |
| 1435 |
final int scrollY = mScrollY; |
| 1436 |
canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop, |
| 1437 |
scrollX + mRight - mLeft - mPaddingRight, |
| 1438 |
scrollY + mBottom - mTop - mPaddingBottom); |
| 1439 |
} |
| 1440 |
|
| 1441 |
canvas.translate(mPaddingLeft, mPaddingTop); |
| 1442 |
|
| 1443 |
if (mDrawMatrix != null) { |
| 1444 |
canvas.concat(mDrawMatrix); |
| 1445 |
} |
| 1446 |
mDrawable.draw(canvas); |
| 1447 |
canvas.restoreToCount(saveCount); |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
@Override |
| 1459 |
@InspectableProperty |
| 1460 |
@ViewDebug.ExportedProperty(category = "layout") |
| 1461 |
public int getBaseline() { |
| 1462 |
if (mBaselineAlignBottom) { |
| 1463 |
return getMeasuredHeight(); |
| 1464 |
} else { |
| 1465 |
return mBaseline; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
public void setBaseline(int baseline) { |
| 1480 |
if (mBaseline != baseline) { |
| 1481 |
mBaseline = baseline; |
| 1482 |
requestLayout(); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
public void setBaselineAlignBottom(boolean aligned) { |
| 1495 |
if (mBaselineAlignBottom != aligned) { |
| 1496 |
mBaselineAlignBottom = aligned; |
| 1497 |
requestLayout(); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
@InspectableProperty |
| 1508 |
public boolean getBaselineAlignBottom() { |
| 1509 |
return mBaselineAlignBottom; |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
public final void setColorFilter(int color, PorterDuff.Mode mode) { |
| 1522 |
setColorFilter(new PorterDuffColorFilter(color, mode)); |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
@RemotableViewMethod |
| 1533 |
public final void setColorFilter(int color) { |
| 1534 |
setColorFilter(color, PorterDuff.Mode.SRC_ATOP); |
| 1535 |
} |
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
public final void clearColorFilter() { |
| 1544 |
setColorFilter(null); |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
public final void setXfermode(Xfermode mode) { |
| 1551 |
if (mXfermode != mode) { |
| 1552 |
mXfermode = mode; |
| 1553 |
mHasXfermode = true; |
| 1554 |
applyXfermode(); |
| 1555 |
invalidate(); |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
public ColorFilter getColorFilter() { |
| 1567 |
return mColorFilter; |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
public void setColorFilter(ColorFilter cf) { |
| 1578 |
if (mColorFilter != cf) { |
| 1579 |
mColorFilter = cf; |
| 1580 |
mHasColorFilter = true; |
| 1581 |
applyColorFilter(); |
| 1582 |
invalidate(); |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
public int getImageAlpha() { |
| 1596 |
return mAlpha; |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
@RemotableViewMethod |
| 1608 |
public void setImageAlpha(int alpha) { |
| 1609 |
setAlpha(alpha); |
| 1610 |
} |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
@Deprecated |
| 1620 |
@RemotableViewMethod |
| 1621 |
public void setAlpha(int alpha) { |
| 1622 |
alpha &= 0xFF; |
| 1623 |
if (mAlpha != alpha) { |
| 1624 |
mAlpha = alpha; |
| 1625 |
mHasAlpha = true; |
| 1626 |
applyAlpha(); |
| 1627 |
invalidate(); |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
private void applyXfermode() { |
| 1632 |
if (mDrawable != null && mHasXfermode) { |
| 1633 |
mDrawable = mDrawable.mutate(); |
| 1634 |
mDrawable.setXfermode(mXfermode); |
| 1635 |
} |
| 1636 |
} |
| 1637 |
|
| 1638 |
private void applyColorFilter() { |
| 1639 |
if (mDrawable != null && mHasColorFilter) { |
| 1640 |
mDrawable = mDrawable.mutate(); |
| 1641 |
mDrawable.setColorFilter(mColorFilter); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
private void applyAlpha() { |
| 1646 |
if (mDrawable != null && mHasAlpha) { |
| 1647 |
mDrawable = mDrawable.mutate(); |
| 1648 |
mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8); |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
@Override |
| 1653 |
public boolean isOpaque() { |
| 1654 |
return super.isOpaque() || mDrawable != null && mXfermode == null |
| 1655 |
&& mDrawable.getOpacity() == PixelFormat.OPAQUE |
| 1656 |
&& mAlpha * mViewAlphaScale >> 8 == 255 |
| 1657 |
&& isFilledByImage(); |
| 1658 |
} |
| 1659 |
|
| 1660 |
private boolean isFilledByImage() { |
| 1661 |
if (mDrawable == null) { |
| 1662 |
return false; |
| 1663 |
} |
| 1664 |
|
| 1665 |
final Rect bounds = mDrawable.getBounds(); |
| 1666 |
final Matrix matrix = mDrawMatrix; |
| 1667 |
if (matrix == null) { |
| 1668 |
return bounds.left <= 0 && bounds.top <= 0 && bounds.right >= getWidth() |
| 1669 |
&& bounds.bottom >= getHeight(); |
| 1670 |
} else if (matrix.rectStaysRect()) { |
| 1671 |
final RectF boundsSrc = mTempSrc; |
| 1672 |
final RectF boundsDst = mTempDst; |
| 1673 |
boundsSrc.set(bounds); |
| 1674 |
matrix.mapRect(boundsDst, boundsSrc); |
| 1675 |
return boundsDst.left <= 0 && boundsDst.top <= 0 && boundsDst.right >= getWidth() |
| 1676 |
&& boundsDst.bottom >= getHeight(); |
| 1677 |
} else { |
| 1678 |
|
| 1679 |
return false; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
@Override |
| 1684 |
public void onVisibilityAggregated(boolean isVisible) { |
| 1685 |
super.onVisibilityAggregated(isVisible); |
| 1686 |
|
| 1687 |
if (mDrawable != null && !sCompatDrawableVisibilityDispatch) { |
| 1688 |
mDrawable.setVisible(isVisible, false); |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
@RemotableViewMethod |
| 1693 |
@Override |
| 1694 |
public void setVisibility(int visibility) { |
| 1695 |
super.setVisibility(visibility); |
| 1696 |
|
| 1697 |
if (mDrawable != null && sCompatDrawableVisibilityDispatch) { |
| 1698 |
mDrawable.setVisible(visibility == VISIBLE, false); |
| 1699 |
} |
| 1700 |
} |
| 1701 |
|
| 1702 |
@Override |
| 1703 |
protected void onAttachedToWindow() { |
| 1704 |
super.onAttachedToWindow(); |
| 1705 |
|
| 1706 |
if (mDrawable != null && sCompatDrawableVisibilityDispatch) { |
| 1707 |
mDrawable.setVisible(getVisibility() == VISIBLE, false); |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
@Override |
| 1712 |
protected void onDetachedFromWindow() { |
| 1713 |
super.onDetachedFromWindow(); |
| 1714 |
|
| 1715 |
if (mDrawable != null && sCompatDrawableVisibilityDispatch) { |
| 1716 |
mDrawable.setVisible(false, false); |
| 1717 |
} |
| 1718 |
} |
| 1719 |
|
| 1720 |
@Override |
| 1721 |
public CharSequence getAccessibilityClassName() { |
| 1722 |
return ImageView.class.getName(); |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
@Override |
| 1727 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 1728 |
super.encodeProperties(stream); |
| 1729 |
stream.addProperty("layout:baseline", getBaseline()); |
| 1730 |
} |
| 1731 |
|
| 1732 |
|
| 1733 |
@Override |
| 1734 |
@TestApi |
| 1735 |
public boolean isDefaultFocusHighlightNeeded(Drawable background, Drawable foreground) { |
| 1736 |
final boolean lackFocusState = mDrawable == null || !mDrawable.isStateful() |
| 1737 |
|| !mDrawable.hasFocusStateSpecified(); |
| 1738 |
return super.isDefaultFocusHighlightNeeded(background, foreground) && lackFocusState; |
| 1739 |
} |
| 1740 |
} |