Explain Codes LogoExplain Codes Logo

Android adding simple animations while setvisibility(View.GONE)

java
animation-engineering
view-visibility
transition-management
Nikita BarsukovbyNikita Barsukov·Jan 7, 2025
TLDR

For an unobtrusive fade-out animation before hiding a view, sequence a fade to zero alpha using animate().alpha(0f) and conceal it afterwards utilizing withEndAction():

yourView.animate() .alpha(0f) .setDuration(200) // Faster than a sneeze .withEndAction(() -> yourView.setVisibility(View.GONE)) // Ninja move! .start();

Pro tip: Use ViewPropertyAnimator to flawlessly maneuver when flipping visibility.

Effective Animations Through Timing

Animations can drastically enhance user experience when timed aptly. To dodge jarring transitions, contemplate:

  • Elongating animation duration for complex transitions to appear smooth. A prolonged time-span could maintain fluidity.
yourView.animate() .translationX(100) .alpha(0f) .setDuration(500) //Slow roll like a grandma with a shopping cart .withEndAction(() -> yourView.setVisibility(View.GONE)) .start();
  • Postponing animation commencement to give user's eyes a moment to catch up and process the UI changes:
yourView.animate() .setStartDelay(100) .alpha(0f) .setDuration(200) // Now you see me, now you don't! .withEndAction(() -> yourView.setVisibility(View.GONE)) .start();
  • Implementing interpolators to manipulate the acceleration and deceleration of the animation, augmenting the natural feel:
yourView.animate() .alpha(0f) .setInterpolator(new DecelerateInterpolator()) .setDuration(300) // Pretty as sipping hot cocoa by the fire .withEndAction(() -> yourView.setVisibility(View.GONE)) .start();

Chalk Out Animations for Varying Views

Every view in Android might demand an inconsistent animation approach based upon its function and the context within the app. Here's what to consider supporting different animation strategies:

  • View groups (like LinearLayout, RelativeLayout): Apply TransitionManager.beginDelayedTransition() before changing the visibility for automated layout transitions.
  • List items: Utilize custom animations through TranslateAnimation for a perception of sliding in/out from the edges.
TranslateAnimation slideIn = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f ); slideIn.setDuration(300); listItem.startAnimation(slideIn); // Slide in DMs like...
  • Shared element transitions: Use ActivityOptionsCompat.makeSceneTransitionAnimation() to give your app an advanced feel.
  • Persistent UI elements: Persistent animations like rotating or pulsing can keep users locked in.
  • Maintain the state of the view post-animation with setFillAfter(true) for animations where the view's novel position should persist.

Customizing Automated Layout Changes

Android provides android:animateLayoutChanges="true" for automated layout animations, which can be set on the ViewGroup in your XML layout file:

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:animateLayoutChanges="true"> <!-- Child views would be right here --> </LinearLayout>

If you crave a bespoke animation set, or to have control over your transitions, use TransitionManager with explicit Transition instances:

// A custom transition ready to roll Transition fade = new Fade(); fade.addTarget(yourView); fade.setDuration(250); // Steady as a clock // Brand them with a delayed transition TransitionManager.beginDelayedTransition(yourParentViewGroup, fade); // Give your view a GONE status yourView.setVisibility(View.GONE); // Abracadabra!