This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.
How to fade a component in/out
Purpose. Make a component appear smoothly (0 → 1) or disappear (1 → 0). AlphaAnimation interpolates a float with ease-in-out; you apply that value to the component’s opacity and force a repaint.
APIs used
new AlphaAnimation(float start, float end, float duration [, float fps])— creates the animation.start/endare clamped to [0,1]; cubic ease-in-out interpolation;durationin seconds;fpsoptional (default 60).AlphaAnimation.onUpdate(Runnable)— callback on each frame; readgetValue()here.AlphaAnimation.onComplete(Runnable)— callback when reachingend(also triggered bystop()).AlphaAnimation.getValue()— current interpolated value.AlphaAnimation.start()/stop()/cancel()— starts / stops (triggersonComplete) / cancels (does not trigger).Component.alpha(float)— sets the component’s opacity (0–1, clamped). Javadoc discrepancy: the doc citessetAlpha, but the actual method isalpha(...).Component.repaint()— static method; requests a re-render (CrossFly renders on demand, so calling it insideonUpdateis required).
Full example (copy, paste and run)
package com.exemplo;
import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.animation.*;
/** Runnable main screen: applies a fade-in on a Label when opened. */
public class MainWindow extends Window
{
@Override
public void onShow() throws Exception
{
super.onShow();
setLayout(new VerticalLayout());
Label c = new Label("Olá");
add(c);
// Declare the variable beforehand so you can read getValue() inside onUpdate.
AlphaAnimation anim = new AlphaAnimation(0f, 1f, 0.3f); // 300 ms, 0 → 1
anim.onUpdate(() ->
{
c.alpha(anim.getValue()); // applies opacity
Component.repaint(); // render on demand: without this, the frame isn't redrawn
})
.onComplete(() -> Component.repaint())
.start();
}
}
The snippet below shows only the core. To run it, place it inside the
MainWindowskeleton above.
Minimal snippet
// verified in the source: AlphaAnimation clamp [0,1] ease-in-out; Component.alpha(float); Component.repaint() static
Label c = new Label("Olá");
add(c);
// Declare the variable beforehand so you can read getValue() inside onUpdate.
AlphaAnimation anim = new AlphaAnimation(0f, 1f, 0.3f); // 300 ms, 0 → 1
anim.onUpdate(() ->
{
c.alpha(anim.getValue()); // applies opacity
Component.repaint(); // render on demand: without this, the frame isn't redrawn
})
.onComplete(() -> Component.repaint())
.start();
Notes:
alpha(1f)(or>= 0.999) removes the opacity property (component 100% opaque);alpha(0f)makes it invisible but still present in the layout.- For a fade-out use
new AlphaAnimation(1f, 0f, 0.3f)and, inonComplete,hide()/remove the component if you wish. - Always call
Component.repaint()inonUpdate— CrossFly only redraws when requested.
Dig deeper
- Value animations
- Sibling recipe: Pulse in a loop · Move a component
- Related: Open and close a screen
Was this page helpful?Thanks for the feedback!