blob: ac677607e4205acea958c6f03b81f2af2baf11f1 (
plain)
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
|
package com.bumptech.glide.request.target;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.bumptech.glide.request.animation.GlideAnimation;
/**
* A {@link com.bumptech.glide.request.target.Target} that can display an {@link android.graphics.drawable.Drawable} in
* an {@link android.widget.ImageView}.
*/
public class DrawableImageViewTarget extends ViewTarget<ImageView, Drawable> {
private static final float SQUARE_RATIO_MARGIN = 0.05f;
private final ImageView view;
public DrawableImageViewTarget(ImageView view) {
super(view);
this.view = view;
}
/**
* {@inheritDoc}
* If no {@link com.bumptech.glide.request.animation.GlideAnimation} is given or if the animation does not set the
* {@link android.graphics.drawable.Drawable} on the view, the drawable is set using
* {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* @param resource {@inheritDoc}
* @param animation {@inheritDoc}
*/
@Override
public void onResourceReady(Drawable resource, GlideAnimation<Drawable> animation) {
if (!(resource instanceof Animatable)) {
//TODO: Try to generalize this to other sizes/shapes.
// This is a dirty hack that tries to make loading square thumbnails and then square full images less costly
// by forcing both the smaller thumb and the larger version to have exactly the same intrinsic dimensions.
// If a drawable is replaced in an ImageView by another drawable with different intrinsic dimensions,
// the ImageView requests a layout. Scrolling rapidly while replacing thumbs with larger images triggers
// lots of these calls and causes significant amounts of jank.
float viewRatio = view.getWidth() / (float) view.getHeight();
float drawableRatio = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight();
if (Math.abs(viewRatio - 1f) <= SQUARE_RATIO_MARGIN
&& Math.abs(drawableRatio - 1f) <= SQUARE_RATIO_MARGIN) {
resource = new SquaringDrawable(resource, view.getWidth());
}
}
if (animation == null || !animation.animate(view.getDrawable(), resource, view)) {
view.setImageDrawable(resource);
}
}
/**
* Sets the given {@link android.graphics.drawable.Drawable} on the view using
* {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* @param placeholder {@inheritDoc}
*/
@Override
public void setPlaceholder(Drawable placeholder) {
view.setImageDrawable(placeholder);
}
}
|