Skip to content

Commit f5ba374

Browse files
committed
Use accurate rounding in FitCenter when checking to see if we can skip
floor is necessary to make sure that we populate all pixels in the Bitmap, but it's not necessary to use to check if our input Bitmap has already been transformed and can be returned as is. Using floor can cause us to re-draw a Bitmap unnecessarily. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=167789795
1 parent a41cc9b commit f5ba374

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

library/src/main/java/com/bumptech/glide/load/resource/bitmap/TransformationUtils.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,10 @@ public static Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitma
127127
final float heightPercentage = height / (float) inBitmap.getHeight();
128128
final float minPercentage = Math.min(widthPercentage, heightPercentage);
129129

130-
// take the floor of the target width/height, not round. If the matrix
131-
// passed into drawBitmap rounds differently, we want to slightly
132-
// overdraw, not underdraw, to avoid artifacts from bitmap reuse.
133-
final int targetWidth = (int) (minPercentage * inBitmap.getWidth());
134-
final int targetHeight = (int) (minPercentage * inBitmap.getHeight());
130+
// Round here in case we've decoded exactly the image we want, but take the floor below to
131+
// avoid a line of garbage or blank pixels in images.
132+
int targetWidth = Math.round(minPercentage * inBitmap.getWidth());
133+
int targetHeight = Math.round(minPercentage * inBitmap.getHeight());
135134

136135
if (inBitmap.getWidth() == targetWidth && inBitmap.getHeight() == targetHeight) {
137136
if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -140,6 +139,12 @@ public static Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitma
140139
return inBitmap;
141140
}
142141

142+
// Take the floor of the target width/height, not round. If the matrix
143+
// passed into drawBitmap rounds differently, we want to slightly
144+
// overdraw, not underdraw, to avoid artifacts from bitmap reuse.
145+
targetWidth = (int) (minPercentage * inBitmap.getWidth());
146+
targetHeight = (int) (minPercentage * inBitmap.getHeight());
147+
143148
Bitmap.Config config = getSafeConfig(inBitmap);
144149
Bitmap toReuse = pool.get(targetWidth, targetHeight, config);
145150

0 commit comments

Comments
 (0)