1package com.bumptech.glide.request.target; 2 3import android.graphics.drawable.Drawable; 4import android.widget.ImageView; 5import com.bumptech.glide.request.GlideAnimation; 6 7public class DrawableImageViewTarget extends ViewTarget<ImageView, Drawable> { 8 private static final float SQUARE_RATIO_MARGIN = 0.05f; 9 private final ImageView view; 10 11 public DrawableImageViewTarget(ImageView view) { 12 super(view); 13 this.view = view; 14 } 15 16 @Override 17 public void onResourceReady(Drawable resource, GlideAnimation<Drawable> animation) { 18 19 //TODO: Try to generalize this to other sizes/shapes. 20 // This is a dirty hack that tries to make loading square thumbnails and then square full images less costly by 21 // forcing both the smaller thumb and the larger version to have exactly the same intrinsic dimensions. If a 22 // drawable is replaced in an ImageView by another drawable with different intrinsic dimensions, the ImageView 23 // requests a layout. Scrolling rapidly while replacing thumbs with larger images triggers lots of these calls 24 // and causes significant amounts of jank. 25 float viewRatio = view.getWidth() / (float) view.getHeight(); 26 float drawableRatio = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight(); 27 if (Math.abs(viewRatio - 1f) <= SQUARE_RATIO_MARGIN && Math.abs(drawableRatio - 1f) <= SQUARE_RATIO_MARGIN) { 28 resource = new SquaringDrawable(resource, view.getWidth()); 29 } 30 31 if (animation == null || !animation.animate(view.getDrawable(), resource, view, this)) { 32 view.setImageDrawable(resource); 33 } 34 } 35 36 @Override 37 public void setPlaceholder(Drawable placeholder) { 38 view.setImageDrawable(placeholder); 39 } 40} 41