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