-
Notifications
You must be signed in to change notification settings - Fork 3
/
ViewUtil.java
81 lines (73 loc) · 2.77 KB
/
ViewUtil.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
package com.android.kickstart.utils;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.Transformation;
public class ViewUtil {
/**
* @param view : View object for animation
*/
public static void expandView(final View view) {
final int targetHeight = view.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with animation height of 0.
view.getLayoutParams().height = 1;
view.setVisibility(View.VISIBLE);
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = interpolatedTime == 1
? WindowManager.LayoutParams.WRAP_CONTENT
: (int) (targetHeight * interpolatedTime);
view.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
animation.setDuration((int) (targetHeight / view.getContext().getResources().getDisplayMetrics().density));
view.startAnimation(animation);
}
/**
* @param view : View object for animation
*/
public static void collapseView(final View view) {
final int initialHeight = view.getMeasuredHeight();
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
view.setVisibility(View.GONE);
} else {
view.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
view.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
animation.setDuration((int) (initialHeight / view.getContext().getResources().getDisplayMetrics().density));
view.startAnimation(animation);
}
/*
* @param view : View object for animation
* @param delay : Delay for view animation
* @param duration : Duration of animation
*/
public static void animateView(View view, long delay, long duration) {
view.setScaleX(0);
view.setScaleY(0);
view.animate()
.scaleX(1)
.scaleY(1)
.setDuration(duration)
.setStartDelay(delay)
.setInterpolator(new OvershootInterpolator())
.start();
}
}