Skip to content

Commit 4d4fe8f

Browse files
committed
Add animation helpers
1 parent f1a5a6e commit 4d4fe8f

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package info.anodsplace.android.anim;
2+
3+
import android.animation.Animator;
4+
import android.animation.AnimatorListenerAdapter;
5+
import android.animation.AnimatorSet;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* @author alex
12+
* @date 2015-05-25
13+
*/
14+
public class AnimatorCollection {
15+
private List<Animator> mAnimators;
16+
private AnimatorSet mSet;
17+
18+
public AnimatorCollection() {
19+
mAnimators = new ArrayList<>();
20+
}
21+
22+
public void add(Animator anim) {
23+
if (anim != null) {
24+
mAnimators.add(anim);
25+
}
26+
}
27+
28+
public void clear() {
29+
mSet = null;
30+
mAnimators.clear();
31+
}
32+
33+
public AnimatorSet sequential() {
34+
AnimatorSet set = set();
35+
set.playSequentially(mAnimators);
36+
return set;
37+
}
38+
39+
public AnimatorSet together() {
40+
AnimatorSet set = set();
41+
set.playTogether(mAnimators);
42+
return set;
43+
}
44+
45+
public boolean isEmpty() {
46+
return mAnimators.isEmpty();
47+
}
48+
49+
public void addListener(AnimatorListenerAdapter listener) {
50+
AnimatorSet set = set();
51+
set.addListener(listener);
52+
}
53+
54+
private AnimatorSet set() {
55+
if (mSet == null) {
56+
mSet = new AnimatorSet();
57+
}
58+
return mSet;
59+
}
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package info.anodsplace.android.anim;
2+
3+
import android.animation.ValueAnimator;
4+
import android.view.View;
5+
import android.view.ViewGroup;
6+
import android.widget.LinearLayout;
7+
8+
/**
9+
* @author alex
10+
* @date 2015-05-25
11+
*/
12+
public class ResizeAnimator {
13+
14+
public static ValueAnimator height(int from, int to, final View view,int duration) {
15+
ValueAnimator anim = ValueAnimator.ofInt(from, to);
16+
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
17+
@Override
18+
public void onAnimationUpdate(ValueAnimator valueAnimator) {
19+
int val = (Integer) valueAnimator.getAnimatedValue();
20+
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
21+
layoutParams.height = val;
22+
view.setLayoutParams(layoutParams);
23+
}
24+
});
25+
anim.setDuration(duration);
26+
return anim;
27+
}
28+
public static ValueAnimator margin(int from, int to, final View view,int duration) {
29+
ValueAnimator anim = ValueAnimator.ofInt(from, to);
30+
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
31+
@Override
32+
public void onAnimationUpdate(ValueAnimator valueAnimator) {
33+
int val = (Integer) valueAnimator.getAnimatedValue();
34+
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
35+
layoutParams.topMargin= val;
36+
view.setLayoutParams(layoutParams);
37+
}
38+
});
39+
anim.setDuration(duration);
40+
return anim;
41+
}
42+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package info.anodsplace.android.anim;
2+
3+
import android.animation.Animator;
4+
import android.animation.AnimatorListenerAdapter;
5+
import android.animation.ValueAnimator;
6+
import android.os.Build;
7+
import android.view.View;
8+
import android.view.ViewAnimationUtils;
9+
10+
/**
11+
* @author alex
12+
* @date 2015-05-25
13+
*/
14+
public class RevealAnimatorCompat {
15+
private static final int ANIM_DURATION = 300;
16+
17+
public static Animator show(final View viewRoot, int x, int y, int delay) {
18+
Animator anim;
19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
20+
int finalRadius = Math.max(viewRoot.getWidth(), viewRoot.getHeight());
21+
anim = ViewAnimationUtils.createCircularReveal(viewRoot, x, y, 0, finalRadius);
22+
anim.setDuration(ANIM_DURATION);
23+
} else {
24+
// Kitkat compatibility
25+
anim = ValueAnimator.ofInt(0, 1);
26+
anim.setDuration(10);
27+
}
28+
anim.addListener(new AnimatorListenerAdapter() {
29+
@Override
30+
public void onAnimationStart(Animator animation) {
31+
viewRoot.setVisibility(View.VISIBLE);
32+
}
33+
});
34+
anim.setStartDelay(delay);
35+
return anim;
36+
}
37+
38+
public static Animator hide(final View viewRoot, int x, int y, int delay) {
39+
Animator anim;
40+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
41+
int initialRadius = viewRoot.getWidth();
42+
anim = ViewAnimationUtils.createCircularReveal(viewRoot, x, y, initialRadius, 0);
43+
anim.setDuration(ANIM_DURATION);
44+
} else {
45+
// Kitkat compatibility
46+
anim = ValueAnimator.ofInt(0, 1);
47+
anim.setDuration(10);
48+
}
49+
anim.addListener(new AnimatorListenerAdapter() {
50+
@Override
51+
public void onAnimationEnd(Animator animation) {
52+
viewRoot.setVisibility(View.INVISIBLE);
53+
}
54+
});
55+
anim.setDuration(ANIM_DURATION);
56+
anim.setStartDelay(delay);
57+
return anim;
58+
}
59+
}

0 commit comments

Comments
 (0)