1111import android .view .ViewGroup ;
1212import android .view .animation .Animation ;
1313import android .view .animation .AnimationUtils ;
14+ import android .view .animation .Transformation ;
1415
1516import com .amit .R ;
1617
@@ -47,7 +48,7 @@ public static void slideActivityFromRightToLeft(@NonNull Context context)
4748 * this activity will make the activity to slide in from left and slide out from right
4849 *
4950 * @param context - context of the activity
50- **/
51+ **/
5152 public static void slideActivityFromLeftToRight (@ NonNull Context context )
5253 {
5354 try
@@ -161,7 +162,7 @@ public static void slideActivityFromUpWithStay(@NonNull Context context)
161162 * this method will make the activity to slide from bottom to up.
162163 *
163164 * @param context - context of the activity
164- **/
165+ **/
165166 public static void slideActivityFromBottomToUp (@ NonNull Context context )
166167 {
167168 try
@@ -180,7 +181,7 @@ public static void slideActivityFromBottomToUp(@NonNull Context context)
180181 * this method will make the activity to slide from up to bottom.
181182 *
182183 * @param context - context of the activity
183- **/
184+ **/
184185 public static void slideActivityFromUpToBottom (@ NonNull Context context )
185186 {
186187 try
@@ -207,9 +208,7 @@ public static void slideActivityFromUpToBottom(@NonNull Context context)
207208 * @param duration - duration of the transition
208209 **/
209210 @ 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 )
213212 {
214213 try
215214 {
@@ -238,9 +237,7 @@ public static void explodeTransition(@NonNull Context context,
238237 * @param view - view to animate
239238 * @param duration - duration of animation
240239 **/
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 )
244241 {
245242 try
246243 {
@@ -265,9 +262,7 @@ public static void slideAnimFromRight(@NonNull Context context,
265262 * @param view - view to animate
266263 * @param duration - duration of animation
267264 **/
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 )
271266 {
272267 try
273268 {
@@ -293,10 +288,7 @@ public static void slideAnimFromLeft(@NonNull Context context,
293288 * @param duration - duration of the animation
294289 * @param animResId - anim resouce for animation
295290 **/
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 )
300292 {
301293 try
302294 {
@@ -319,7 +311,7 @@ public static void slideAnim(@NonNull Context context,
319311 *
320312 * @param context - context of the application
321313 * @param view - view to animate
322- **/
314+ **/
323315 public static void bounceAnim (Context context , View view )
324316 {
325317 try
@@ -335,4 +327,174 @@ public static void bounceAnim(Context context, View view)
335327 e .printStackTrace ();
336328 }
337329 }
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+ }
338500}
0 commit comments