/
TransitionOptions.java
117 lines (106 loc) · 3.9 KB
/
TransitionOptions.java
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
package com.bumptech.glide;
import androidx.annotation.NonNull;
import com.bumptech.glide.request.transition.NoTransition;
import com.bumptech.glide.request.transition.TransitionFactory;
import com.bumptech.glide.request.transition.ViewAnimationFactory;
import com.bumptech.glide.request.transition.ViewPropertyAnimationFactory;
import com.bumptech.glide.request.transition.ViewPropertyTransition;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Util;
/**
* A base class for setting a transition to use on a resource when a load completes.
*
* <p>Note: Implementations must implement equals/hashcode.
*
* @param <CHILD> The implementation of this class to return to chain methods.
* @param <TranscodeType> The type of resource that will be animated.
*/
public abstract class TransitionOptions<
CHILD extends TransitionOptions<CHILD, TranscodeType>, TranscodeType>
implements Cloneable {
private TransitionFactory<? super TranscodeType> transitionFactory = NoTransition.getFactory();
/**
* Removes any existing animation put on the builder. Will be overridden by subsequent calls that
* put an animation.
*
* @return This request builder.
*/
@NonNull
public final CHILD dontTransition() {
return transition(NoTransition.getFactory());
}
/**
* Sets an {@link android.view.animation.Animation} to run on the wrapped target when an resource
* load finishes. Will only be run if the resource was loaded asynchronously (i.e. was not in the
* memory cache).
*
* @param viewAnimationId The resource id of the {@link android.view.animation.Animation} to use
* as the transition.
* @return This request builder.
*/
@NonNull
public final CHILD transition(int viewAnimationId) {
return transition(new ViewAnimationFactory<>(viewAnimationId));
}
/**
* Sets an animator to run a {@link android.view.ViewPropertyAnimator} on a view that the target
* may be wrapping when a resource load finishes. Will only be run if the load was loaded
* asynchronously (i.e. was not in the memory cache).
*
* @param animator The {@link com.bumptech.glide.request.transition.ViewPropertyTransition
* .Animator} to run.
* @return This request builder.
*/
@NonNull
public final CHILD transition(@NonNull ViewPropertyTransition.Animator animator) {
return transition(new ViewPropertyAnimationFactory<>(animator));
}
/**
* Uses the given {@link TransitionFactory} to build a {@link
* com.bumptech.glide.request.transition.Transition} for each request started with these {@code
* TransitionOptions}.
*
* @return This request builder.
*/
@NonNull
public final CHILD transition(
@NonNull TransitionFactory<? super TranscodeType> transitionFactory) {
this.transitionFactory = Preconditions.checkNotNull(transitionFactory);
return self();
}
@SuppressWarnings({
// cast to CHILD is safe given the generic argument represents the object's runtime class
"unchecked",
// CHILD is the correct class name.
"PMD.CloneMethodReturnTypeMustMatchClassName",
// we don't want to throw to be user friendly
"PMD.CloneThrowsCloneNotSupportedException"
})
@Override
public final CHILD clone() {
try {
return (CHILD) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
final TransitionFactory<? super TranscodeType> getTransitionFactory() {
return transitionFactory;
}
@SuppressWarnings("unchecked")
private CHILD self() {
return (CHILD) this;
}
@Override
public boolean equals(Object o) {
if (o instanceof TransitionOptions) {
TransitionOptions<?, ?> other = (TransitionOptions<?, ?>) o;
return Util.bothNullOrEqual(transitionFactory, other.transitionFactory);
}
return false;
}
@Override
public int hashCode() {
return transitionFactory != null ? transitionFactory.hashCode() : 0;
}
}