11
11
import android .view .ViewGroup ;
12
12
import android .view .animation .Animation ;
13
13
import android .view .animation .AnimationUtils ;
14
+ import android .view .animation .Transformation ;
14
15
15
16
import com .amit .R ;
16
17
@@ -47,7 +48,7 @@ public static void slideActivityFromRightToLeft(@NonNull Context context)
47
48
* this activity will make the activity to slide in from left and slide out from right
48
49
*
49
50
* @param context - context of the activity
50
- **/
51
+ **/
51
52
public static void slideActivityFromLeftToRight (@ NonNull Context context )
52
53
{
53
54
try
@@ -161,7 +162,7 @@ public static void slideActivityFromUpWithStay(@NonNull Context context)
161
162
* this method will make the activity to slide from bottom to up.
162
163
*
163
164
* @param context - context of the activity
164
- **/
165
+ **/
165
166
public static void slideActivityFromBottomToUp (@ NonNull Context context )
166
167
{
167
168
try
@@ -180,7 +181,7 @@ public static void slideActivityFromBottomToUp(@NonNull Context context)
180
181
* this method will make the activity to slide from up to bottom.
181
182
*
182
183
* @param context - context of the activity
183
- **/
184
+ **/
184
185
public static void slideActivityFromUpToBottom (@ NonNull Context context )
185
186
{
186
187
try
@@ -207,9 +208,7 @@ public static void slideActivityFromUpToBottom(@NonNull Context context)
207
208
* @param duration - duration of the transition
208
209
**/
209
210
@ TargetApi (21 )
210
- public static void explodeTransition (@ NonNull Context context ,
211
- ViewGroup viewGroup ,
212
- int duration )
211
+ public static void explodeTransition (@ NonNull Context context , ViewGroup viewGroup , int duration )
213
212
{
214
213
try
215
214
{
@@ -238,9 +237,7 @@ public static void explodeTransition(@NonNull Context context,
238
237
* @param view - view to animate
239
238
* @param duration - duration of animation
240
239
**/
241
- public static void slideAnimFromRight (@ NonNull Context context ,
242
- @ NonNull View view ,
243
- int duration )
240
+ public static void slideAnimFromRight (@ NonNull Context context , @ NonNull View view , int duration )
244
241
{
245
242
try
246
243
{
@@ -265,9 +262,7 @@ public static void slideAnimFromRight(@NonNull Context context,
265
262
* @param view - view to animate
266
263
* @param duration - duration of animation
267
264
**/
268
- public static void slideAnimFromLeft (@ NonNull Context context ,
269
- @ NonNull View view ,
270
- int duration )
265
+ public static void slideAnimFromLeft (@ NonNull Context context , @ NonNull View view , int duration )
271
266
{
272
267
try
273
268
{
@@ -293,10 +288,7 @@ public static void slideAnimFromLeft(@NonNull Context context,
293
288
* @param duration - duration of the animation
294
289
* @param animResId - anim resouce for animation
295
290
**/
296
- public static void slideAnim (@ NonNull Context context ,
297
- @ NonNull View view ,
298
- int duration ,
299
- @ AnimRes int animResId )
291
+ public static void slideAnim (@ NonNull Context context , @ NonNull View view , int duration , @ AnimRes int animResId )
300
292
{
301
293
try
302
294
{
@@ -319,7 +311,7 @@ public static void slideAnim(@NonNull Context context,
319
311
*
320
312
* @param context - context of the application
321
313
* @param view - view to animate
322
- **/
314
+ **/
323
315
public static void bounceAnim (Context context , View view )
324
316
{
325
317
try
@@ -335,4 +327,174 @@ public static void bounceAnim(Context context, View view)
335
327
e .printStackTrace ();
336
328
}
337
329
}
330
+
331
+ /**
332
+ * 2019 July 22 - Monday - 12:30 PM
333
+ * expand view method
334
+ *
335
+ * this method is used to expand the view to its content's height
336
+ *
337
+ * @param v - View that needs to be expanded
338
+ **/
339
+ public static void expandView (final View v )
340
+ {
341
+ int matchParentMeasureSpec = View .MeasureSpec .makeMeasureSpec (((View ) v .getParent ()).getWidth (), View .MeasureSpec .EXACTLY );
342
+ int wrapContentMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
343
+
344
+ v .measure (matchParentMeasureSpec , wrapContentMeasureSpec );
345
+ final int targetHeight = v .getMeasuredHeight ();
346
+
347
+ // Older versions of android (pre API 21) cancel animations for views with a height of 0.
348
+ v .getLayoutParams ().height = 1 ;
349
+ v .setVisibility (View .VISIBLE );
350
+
351
+ Animation a = new Animation ()
352
+ {
353
+ @ Override
354
+ protected void applyTransformation (float interpolatedTime , Transformation t )
355
+ {
356
+ v .getLayoutParams ().height = interpolatedTime == 1
357
+ ? ViewGroup .LayoutParams .WRAP_CONTENT
358
+ : (int ) (targetHeight * interpolatedTime );
359
+
360
+ v .requestLayout ();
361
+ }
362
+
363
+ @ Override
364
+ public boolean willChangeBounds ()
365
+ {
366
+ return true ;
367
+ }
368
+ };
369
+
370
+ // Expansion speed of 1dp/ms
371
+ a .setDuration ((int ) (targetHeight / v .getContext ().getResources ().getDisplayMetrics ().density ));
372
+ v .startAnimation (a );
373
+ }
374
+
375
+ /**
376
+ * 2019 July 22 - Monday - 12:30 PM
377
+ * collapse view method
378
+ *
379
+ * this method is used to collapse the view to its content's height
380
+ *
381
+ * @param v - View that needs to be collapsed
382
+ **/
383
+ public static void collapseView (final View v )
384
+ {
385
+ final int initialHeight = v .getMeasuredHeight ();
386
+
387
+ Animation a = new Animation ()
388
+ {
389
+ @ Override
390
+ protected void applyTransformation (float interpolatedTime , Transformation t )
391
+ {
392
+ if (interpolatedTime == 1 )
393
+ {
394
+ v .setVisibility (View .GONE );
395
+ }
396
+ else
397
+ {
398
+ v .getLayoutParams ().height = initialHeight - (int ) (initialHeight * interpolatedTime );
399
+ v .requestLayout ();
400
+ }
401
+ }
402
+
403
+ @ Override
404
+ public boolean willChangeBounds ()
405
+ {
406
+ return true ;
407
+ }
408
+ };
409
+
410
+ // Collapse speed of 1dp/ms
411
+ a .setDuration ((int ) (initialHeight / v .getContext ().getResources ().getDisplayMetrics ().density ));
412
+ v .startAnimation (a );
413
+ }
414
+
415
+ /**
416
+ * 2019 July 22 - Monday - 12:30 PM
417
+ * expand view method
418
+ *
419
+ * this method is used to expand the view to its content's height
420
+ *
421
+ * @param v - View that needs to be expanded
422
+ *
423
+ * @param animDuration - duration of the animation for expanding the view
424
+ **/
425
+ public static void expandView (final View v , int animDuration )
426
+ {
427
+ int matchParentMeasureSpec = View .MeasureSpec .makeMeasureSpec (((View ) v .getParent ()).getWidth (), View .MeasureSpec .EXACTLY );
428
+ int wrapContentMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
429
+
430
+ v .measure (matchParentMeasureSpec , wrapContentMeasureSpec );
431
+ final int targetHeight = v .getMeasuredHeight ();
432
+
433
+ // Older versions of android (pre API 21) cancel animations for views with a height of 0.
434
+ v .getLayoutParams ().height = 1 ;
435
+ v .setVisibility (View .VISIBLE );
436
+
437
+ Animation a = new Animation ()
438
+ {
439
+ @ Override
440
+ protected void applyTransformation (float interpolatedTime , Transformation t )
441
+ {
442
+ v .getLayoutParams ().height = interpolatedTime == 1
443
+ ? ViewGroup .LayoutParams .WRAP_CONTENT
444
+ : (int ) (targetHeight * interpolatedTime );
445
+
446
+ v .requestLayout ();
447
+ }
448
+
449
+ @ Override
450
+ public boolean willChangeBounds ()
451
+ {
452
+ return true ;
453
+ }
454
+ };
455
+
456
+ a .setDuration (animDuration );
457
+ v .startAnimation (a );
458
+ }
459
+
460
+ /**
461
+ * 2019 July 22 - Monday - 12:30 PM
462
+ * collapse view method
463
+ *
464
+ * this method is used to collapse the view to its content's height
465
+ *
466
+ * @param v - View that needs to be collapsed
467
+ *
468
+ * @param animDuration - duration of the animation for collapsing the view
469
+ **/
470
+ public static void collapseView (final View v , int animDuration )
471
+ {
472
+ final int initialHeight = v .getMeasuredHeight ();
473
+
474
+ Animation a = new Animation ()
475
+ {
476
+ @ Override
477
+ protected void applyTransformation (float interpolatedTime , Transformation t )
478
+ {
479
+ if (interpolatedTime == 1 )
480
+ {
481
+ v .setVisibility (View .GONE );
482
+ }
483
+ else
484
+ {
485
+ v .getLayoutParams ().height = initialHeight - (int ) (initialHeight * interpolatedTime );
486
+ v .requestLayout ();
487
+ }
488
+ }
489
+
490
+ @ Override
491
+ public boolean willChangeBounds ()
492
+ {
493
+ return true ;
494
+ }
495
+ };
496
+
497
+ a .setDuration (animDuration );
498
+ v .startAnimation (a );
499
+ }
338
500
}
0 commit comments