/
FlyRefreshLayout.java
167 lines (141 loc) · 6.07 KB
/
FlyRefreshLayout.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.race604.flyrefresh;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v4.view.animation.PathInterpolatorCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import com.race604.flyrefresh.internal.MountanScenceView;
import com.race604.flyrefresh.internal.SimpleAnimatorListener;
import com.race604.utils.UIUtils;
/**
* Created by jing on 15-5-27.
*/
public class FlyRefreshLayout extends PullHeaderLayout {
private AnimatorSet mFlyAnimator = null;
private OnPullRefreshListener mListener;
public FlyRefreshLayout(Context context) {
super(context);
init(context);
}
public FlyRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public FlyRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
MountanScenceView headerView = new MountanScenceView(getContext());
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mHeaderController.getMaxHeight());
setHeaderView(headerView, lp);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// Set default action icon if user not override
if (getIconView() == null) {
setActionDrawable(getResources().getDrawable(R.mipmap.ic_send));
}
}
@Override
protected void onStartRefreshAnimation() {
if (mFlyAnimator != null) {
mFlyAnimator.end();
}
final View iconView = getIconView();
UIUtils.clearAnimator(iconView);
AnimatorSet flyUpAnim = new AnimatorSet();
flyUpAnim.setDuration(800);
ObjectAnimator transX = ObjectAnimator.ofFloat(iconView, "translationX", 0, getWidth());
ObjectAnimator transY = ObjectAnimator.ofFloat(iconView, "translationY", 0, -mHeaderController.getHeight());
transY.setInterpolator(PathInterpolatorCompat.create(0.7f, 1f));
ObjectAnimator rotation = ObjectAnimator.ofFloat(iconView, "rotation", -45, 0);
rotation.setInterpolator(new DecelerateInterpolator());
ObjectAnimator rotationX = ObjectAnimator.ofFloat(iconView, "rotationX", 0, 60);
rotationX.setInterpolator(new DecelerateInterpolator());
flyUpAnim.playTogether(transX, transY, rotationX,
ObjectAnimator.ofFloat(iconView, "scaleX", 1, 0.5f),
ObjectAnimator.ofFloat(iconView, "scaleY", 1, 0.5f),
rotation
);
mFlyAnimator = flyUpAnim;
mFlyAnimator.start();
if (mListener != null) {
mListener.onRefresh(FlyRefreshLayout.this);
}
}
public void setOnPullRefreshListener(OnPullRefreshListener listener) {
mListener = listener;
}
public void onRefreshFinish() {
if (mFlyAnimator != null) {
mFlyAnimator.cancel();
}
final View iconView = getIconView();
UIUtils.clearAnimator(iconView);
final int offDistX = -iconView.getRight();
final int offDistY = -UIUtils.dpToPx(10);
AnimatorSet flyDownAnim = new AnimatorSet();
flyDownAnim.setDuration(800);
ObjectAnimator transX1 = ObjectAnimator.ofFloat(iconView, "translationX", getWidth(), offDistX);
ObjectAnimator transY1 = ObjectAnimator.ofFloat(iconView, "translationY", -mHeaderController.getHeight(), offDistY);
transY1.setInterpolator(PathInterpolatorCompat.create(0.1f, 1f));
ObjectAnimator rotation1 = ObjectAnimator.ofFloat(iconView, "rotation", iconView.getRotation(), 0);
rotation1.setInterpolator(new AccelerateInterpolator());
flyDownAnim.playTogether(transX1, transY1,
ObjectAnimator.ofFloat(iconView, "scaleX", 0.5f, 0.9f),
ObjectAnimator.ofFloat(iconView, "scaleY", 0.5f, 0.9f),
rotation1
);
flyDownAnim.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
iconView.setRotationY(180);
}
});
AnimatorSet flyInAnim = new AnimatorSet();
flyInAnim.setDuration(400);
flyInAnim.setInterpolator(new DecelerateInterpolator());
ObjectAnimator tranX2 = ObjectAnimator.ofFloat(iconView, "translationX", offDistX, 0);
ObjectAnimator tranY2 = ObjectAnimator.ofFloat(iconView, "translationY", offDistY, 0);
ObjectAnimator rotationX2 = ObjectAnimator.ofFloat(iconView, "rotationX", 30, 0);
flyInAnim.playTogether(tranX2, tranY2, rotationX2,
ObjectAnimator.ofFloat(iconView, "scaleX", 0.9f, 1f),
ObjectAnimator.ofFloat(iconView, "scaleY", 0.9f, 1f));
flyInAnim.setStartDelay(100);
flyInAnim.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
iconView.setRotationY(0);
}
});
mFlyAnimator = new AnimatorSet();
mFlyAnimator.playSequentially(flyDownAnim, flyInAnim);
mFlyAnimator.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
if (mListener != null) {
mListener.onRefreshAnimationEnd(FlyRefreshLayout.this);
}
}
});
mFlyAnimator.start();
}
@Override
protected void onMoveHeader(int state, float progress) {
super.onMoveHeader(state, progress);
if (mHeaderController.isOverHeight()) {
getIconView().setRotation((-45) * progress);
}
}
public interface OnPullRefreshListener {
void onRefresh(FlyRefreshLayout view);
void onRefreshAnimationEnd(FlyRefreshLayout view);
}
}