/
ScalingLayout.java
368 lines (324 loc) · 10.7 KB
/
ScalingLayout.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package iammert.com.view.scalinglib;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.FrameLayout;
/**
* Created by mertsimsek on 30/09/2017.
*/
public class ScalingLayout extends FrameLayout {
/**
* Settings
*/
ScalingLayoutSettings settings;
/**
* Current radius
*/
private float currentRadius;
/**
* Width is dependent value. It depends on
* radius. If radius gets updated,
* layout width will be updated according to this change.
*/
private int currentWidth;
/**
* If layout has margins, margin has to be change
* according to radius.
*/
private float[] maxMargins;
private float[] currentMargins;
/**
* State for layout.
*/
private State state;
/**
* Values to draw rounded on layout
*/
private Path path;
private Path outlinePath;
private RectF rectF;
private Paint maskPaint;
/**
* Animator to expand and collapse
*/
private ValueAnimator valueAnimator;
/**
* Listener to notify observer about
* progress and collapse/expand
*/
private ScalingLayoutListener scalingLayoutListener;
/**
* CustomOutline for elevation shadows
*/
@Nullable
private ScalingLayoutOutlineProvider viewOutline;
public ScalingLayout(@NonNull Context context) {
super(context);
init(context, null);
}
public ScalingLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public ScalingLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@SuppressLint("NewApi")
public ScalingLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
/**
* Initialize layout
*
* @param context
* @param attributeSet
*/
public void init(Context context, AttributeSet attributeSet) {
settings = new ScalingLayoutSettings(context, attributeSet);
settings.setElevation(ViewCompat.getElevation(this));
state = State.COLLAPSED;
path = new Path();
outlinePath = new Path();
rectF = new RectF(0, 0, 0, 0);
maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
setLayerType(LAYER_TYPE_HARDWARE, null);
valueAnimator = ValueAnimator.ofFloat(0, 0);
valueAnimator.setDuration(200);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
setRadius((Float) valueAnimator.getAnimatedValue());
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (maxMargins == null) {
maxMargins = new float[4];
currentMargins = new float[4];
ViewGroup.MarginLayoutParams marginLayoutParams = ((ViewGroup.MarginLayoutParams) getLayoutParams());
currentMargins[0] = maxMargins[0] = marginLayoutParams.leftMargin;
currentMargins[1] = maxMargins[1] = marginLayoutParams.topMargin;
currentMargins[2] = maxMargins[2] = marginLayoutParams.rightMargin;
currentMargins[3] = maxMargins[3] = marginLayoutParams.bottomMargin;
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (!settings.isInitialized()) {
settings.initialize(w, h);
currentWidth = w;
currentRadius = settings.getMaxRadius();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
viewOutline = new ScalingLayoutOutlineProvider(w, h, currentRadius);
}
}
rectF.set(0, 0, w, h);
updateViewOutline(h, currentWidth, currentRadius);
invalidate();
}
@Override
protected void dispatchDraw(Canvas canvas) {
int save = canvas.save();
super.dispatchDraw(canvas);
canvas.restoreToCount(save);
path.reset();
path.addRoundRect(rectF, currentRadius, currentRadius, Path.Direction.CCW);
canvas.drawPath(path, maskPaint);
}
/**
* Provides current {@link ScalingLayoutSettings}
*
* @return
*/
public ScalingLayoutSettings getSettings() {
return settings;
}
/**
* get current state of layout
*
* @return
*/
public State getState() {
return state;
}
/**
* Expand layout to screen
*/
public void expand() {
valueAnimator.setFloatValues(settings.getMaxRadius(), 0);
valueAnimator.start();
}
/**
* Collapse layout to initial position
*/
public void collapse() {
valueAnimator.setFloatValues(0, settings.getMaxRadius());
valueAnimator.start();
}
/**
* This method takes a progress parameter value
* between 0.0f and 1.0f. And apply this
* progress value to layout.
*
* @param progress
*/
public void setProgress(float progress) {
if (progress > 1.0f || progress < 0.0f) {
return;
}
setRadius(settings.getMaxRadius() - settings.getMaxRadius() * progress);
}
public void setListener(ScalingLayoutListener scalingLayoutListener) {
this.scalingLayoutListener = scalingLayoutListener;
}
/**
* Updates view outline borders and radius
*
* @param height
* @param width
* @param radius
*/
private void updateViewOutline(int height, int width, float radius) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && ViewCompat.getElevation(this) > 0f) {
try {
viewOutline.setHeight(height);
viewOutline.setWidth(width);
viewOutline.setRadius(radius);
setOutlineProvider(viewOutline);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Set radius will update layout radius
* Also layouts margins and width depend on
* radius. So If you update radius, your layout's width
* and margins will be updated.
*
* @param radius
*/
private void setRadius(float radius) {
if (radius < 0) {
return;
}
updateCurrentRadius(radius);
updateCurrentWidth(currentRadius);
updateCurrentMargins(currentRadius);
updateState(currentRadius);
updateCurrentElevation();
getLayoutParams().width = currentWidth;
((ViewGroup.MarginLayoutParams) getLayoutParams())
.setMargins((int) currentMargins[0],
(int) currentMargins[1],
(int) currentMargins[2],
(int) currentMargins[3]);
requestLayout();
}
/**
* Update current radius
*
* @param radius
*/
private void updateCurrentRadius(float radius) {
currentRadius = radius < settings.getMaxRadius() ? radius : settings.getMaxRadius();
}
/**
* Update layout width with given radius value
*
* @param currentRadius
*/
private void updateCurrentWidth(float currentRadius) {
int diffPixel = settings.getMaxWidth() - settings.getInitialWidth();
float calculatedWidth = (diffPixel - (currentRadius * diffPixel / settings.getMaxRadius())) + settings.getInitialWidth();
if (calculatedWidth > settings.getMaxWidth()) {
currentWidth = settings.getMaxWidth();
} else if (calculatedWidth < settings.getInitialWidth()) {
currentWidth = settings.getInitialWidth();
} else {
currentWidth = (int) calculatedWidth;
}
}
/**
* Update layout margins with given radius value
*
* @param currentRadius
*/
private void updateCurrentMargins(float currentRadius) {
currentMargins[0] = maxMargins[0] * currentRadius / settings.getMaxRadius();
currentMargins[1] = maxMargins[1] * currentRadius / settings.getMaxRadius();
currentMargins[2] = maxMargins[2] * currentRadius / settings.getMaxRadius();
currentMargins[3] = maxMargins[3] * currentRadius / settings.getMaxRadius();
}
/**
* Updates layout state
*
* @param currentRadius
*/
private void updateState(float currentRadius) {
if (currentRadius == 0) {
state = State.EXPANDED;
} else if (currentRadius == settings.getMaxRadius()) {
state = State.COLLAPSED;
} else {
state = State.PROGRESSING;
}
notifyListener();
}
private void updateCurrentElevation() {
ViewCompat.setElevation(this, settings.getElevation());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && ViewCompat.getElevation(this) > 0f) {
try {
setOutlineProvider(getOutlineProvider());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Notify observers about change
*/
private void notifyListener() {
if (scalingLayoutListener != null) {
if (state == State.COLLAPSED) {
scalingLayoutListener.onCollapsed();
} else if (state == State.EXPANDED) {
scalingLayoutListener.onExpanded();
} else {
scalingLayoutListener.onProgress(currentRadius / settings.getMaxRadius());
}
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public ViewOutlineProvider getOutlineProvider() {
return new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setConvexPath(path);
}
};
}
}