/
ForkView.java
211 lines (167 loc) · 6.44 KB
/
ForkView.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.joker.rxweather.ui;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.joker.rxweather.common.Constants;
/**
* Created by Joker on 2015/11/5.
*/
@CoordinatorLayout.DefaultBehavior(ForkView.Behavior.class) public class ForkView extends TextView {
private static final String TAG = ForkView.class.getSimpleName();
public ForkView(Context context) {
super(context);
}
public ForkView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ForkView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (ForkView.this.getY() == 0 && ForkView.this.getVisibility() != GONE) {
ViewCompat.setY(ForkView.this, ForkView.calculateTranslation(ForkView.this));
ForkView.this.setVisibility(GONE);
}
}
@Override public Parcelable onSaveInstanceState() {
SavedState savedState = new SavedState(super.onSaveInstanceState());
savedState.stateToSave = ForkView.this.getTranslationY();
return savedState;
}
@Override public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
ForkView.this.setTranslationY(savedState.stateToSave);
}
static class SavedState extends BaseSavedState {
private float stateToSave;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
this.stateToSave = in.readFloat();
}
@Override public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeFloat(this.stateToSave);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
public static class Behavior extends android.support.design.widget.CoordinatorLayout.Behavior<ForkView> {
private boolean isOuting;
public Behavior() {
}
@Override public boolean layoutDependsOn(CoordinatorLayout parent, ForkView child, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override public boolean onDependentViewChanged(CoordinatorLayout parent, ForkView child, View dependency) {
if (dependency instanceof AppBarLayout) {
Behavior.this.updateVisibility(parent, (AppBarLayout) dependency, child);
}
return false;
}
private boolean updateVisibility(CoordinatorLayout parent, AppBarLayout appBarLayout, ForkView child) {
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
if (layoutParams.getAnchorId() != appBarLayout.getId()) {
return false;
}
int distanceToScroll = ForkView.calculateTranslation(child);
/*other animation......*/
//ViewCompat.setTranslationY(child, Math.abs(y) - distanceToScroll);
if (Math.abs(appBarLayout.getY()) >= getMinimumHeightForVisibleOverlappingContent(appBarLayout)) {
if (child.getVisibility() != VISIBLE) {
this.animateIn(child, distanceToScroll);
}
} else {
if (!Behavior.this.isOuting && child.getVisibility() == View.VISIBLE) {
this.animateOut(child, -distanceToScroll);
}
}
return true;
}
final int getMinimumHeightForVisibleOverlappingContent(AppBarLayout appBarLayout) {
int topInset = 0;
int minHeight = ViewCompat.getMinimumHeight(appBarLayout);
if (minHeight != 0) {
return minHeight * 2 + topInset;
} else {
int childCount = appBarLayout.getChildCount();
return childCount >= 1 ? ViewCompat.getMinimumHeight(appBarLayout.getChildAt(childCount - 1)) * 2 + topInset
: 0;
}
/* try {
Method method =
AppBarLayout.class.getDeclaredMethod("getMinimumHeightForVisibleOverlappingContent");
method.setAccessible(true);
method.invoke(appBarLayout);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}*/
}
private void animateIn(ForkView child, float distanceToScroll) {
child.setVisibility(VISIBLE);
ViewCompat
.animate(child)
.alpha(1.0f)
.translationY(distanceToScroll)
.setDuration(Constants.MILLISECONDS_400)
.setInterpolator(new FastOutSlowInInterpolator())
.withLayer()
.setListener(null);
}
private void animateOut(final ForkView child, float distanceToScroll) {
ViewCompat
.animate(child)
.alpha(0.4f)
.translationY(distanceToScroll)
.setDuration(Constants.MILLISECONDS_400)
.setInterpolator(new FastOutLinearInInterpolator())
.withLayer()
.setListener(new ViewPropertyAnimatorListener() {
public void onAnimationStart(View view) {
Behavior.this.isOuting = true;
}
public void onAnimationCancel(View view) {
Behavior.this.isOuting = false;
}
public void onAnimationEnd(View view) {
Behavior.this.isOuting = false;
view.setVisibility(View.GONE);
}
});
}
}
private static int calculateTranslation(View view) {
int height = view.getHeight();
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
int margins = params.topMargin + params.bottomMargin;
return height + margins;
}
}