Git Product home page Git Product logo

animated's Introduction

animated

animated introduces implicit animations, a completely new concept in JavaFX strongly inspired by Flutter's animations and motion widgets.

Index

  1. Getting started
  2. Implicit animations
  3. Animated containers
  4. Animated switchers
  5. Animated theme switch
  6. Animated values
  7. Other examples
  8. FXML
  9. Kotlin extensions

Getting started

Maven:

<dependency>
    <groupId>eu.iamgio</groupId>
    <artifactId>animated</artifactId>
    <version>1.3.0</version>
</dependency>

Gradle:

allprojects {
    repositories {
        mavenCentral()
    }
}
dependencies {
    implementation 'eu.iamgio:animated:1.3.0'
}

Note: v1.0.0 brought several important changes, including a different project structure, that may cause compilation errors when upgrading from 0.x versions.
Please check the migration guide to see what changed and quickly learn how to fix those errors.




Implicit animations

Forget about timelines, explicit animations and other stuff that pollutes your code. This animation system will provide versatility to your code and interface.

Demo
Code

Animated animated = new Animated(child, AnimationProperty.of(child.opacityProperty()));
root.getChildren().add(animated);

// Later...
child.setOpacity(0.5); // Plays the transition

This approach instantiates an Animated node that contains one child and is bound to a property.
Now that we have set an animated bound, we can see that child.setOpacity(someValue) creates a transition between the initial and final value.

A single Animated object can take multiple properties at once, and they can also be added later by accessing Animated#getTargetProperties.

Presets

Pre-made animation properties represent a concise and efficient way to create animated bindings, rather than manually referencing to the raw JavaFX property as we previously did.

  • AnimatedBlur
  • AnimatedColor
  • AnimatedDropShadow.Color
  • AnimatedDropShadow.Radius
  • AnimatedLayout
  • AnimatedOpacity
  • AnimatedPrefSize
  • AnimatedRotation
  • AnimatedScale
  • AnimatedTranslatePosition

When these properties are instantiated via their zero-arguments constructor, the target node references to the Animated's child.

// Before
new Animated(child,
        AnimationProperty.of(child.prefWidthProperty()),
        AnimationProperty.of(child.prefHeightProperty())
);

// After: better!
new Animated(child, new AnimatedPrefSize());

Independent animations

Animated is a node that has to be added to the scene in order to work.
Here is a different approach that is independent from the scene:

AnimationProperty.of(node.opacityProperty()).register();
// or
new AnimatedOpacity(node).register();

// Later...
node.setOpacity(0.5); // Plays the transition

Animations are not only visual: you can also animate other changes such as audio volume!

Custom animations

The default animation is linear and lasts 1 second. It can be customized by calling withSettings(AnimationSettings settings) or custom(Function<AnimationSettings, AnimationSettings> settings), both methods available on animated nodes and animation properties.

Examples:

Animated animated = new Animated(child, AnimationProperty.of(child.opacityProperty()));
    .custom(settings -> settings.withDuration(Duration.seconds(.5)).withCurve(Curve.EASE_IN_OUT));
Animated animated = new Animated(child,
    new AnimatedOpacity()
        .custom(settings -> settings.withDuration(Duration.seconds(.8))),
    new AnimatedRotation()
        .custom(settings -> settings.withDuration(Duration.seconds(.5))),
).custom(settings -> settings.withCurve(Curve.EASE_OUT));



Animated containers

animated provides custom implementations of VBox and HBox that animate their content whenever their children are affected by a change.
This feature is based on animations from AnimateFX.

Demo
Code

Constructors:

  • Animation in, Animation out wraps two AnimateFX objects into customizable animated objects;
  • AnimationFX in, AnimationFX out takes two raw AnimateFX animations that cannot be customized;
  • AnimationPair animation takes a pair of animations, mostly used with pre-made pairs (e.g. AnimationPair.fade()).

pause() and resume() allow disabling/enabling animations so that you can switch back to the regular implementation and forth.

Example:

AnimatedVBox vBox = new AnimatedVBox(AnimationPair.fade());

// Later...
vBox.getChildren().add(someNode);    // someNode fades in
vBox.getChildren().remove(someNode); // someNode fades out



Animated switchers

The library also provides an AnimatedSwitcher node that creates a transition whenever its child changes.
As for animated containers, this feature relies on AnimateFX.

Demo
Code

See animated containers for information about constructors.
Right after the instantiation, calling of(Node child) will set the initial child without any animation played.

Example:

AnimatedSwitcher switcher = new AnimatedSwitcher(
    new Animation(new FadeInDown()).setSpeed(2), 
    new Animation(new FadeOutDown()).setSpeed(1.5)
).of(firstChild);
root.getChildren().add(switcher);

// Later...
switcher.setChild(secondChild); // Plays the transition

Related: animated text

AnimatedLabel uses a switcher to animate text.

Example:

AnimatedLabel label = new AnimatedLabel("Text", AnimationPair.fade());

// Later...
label.setText("New text"); // Plays the transition



Animated theme switch

It is possible to create a transition whenever the stylesheets of the scene change via AnimatedThemeSwitcher, based on AnimateFX.

Theme
Code

scene.getStylesheets().setAll("/light.css"); // Initial theme
AnimatedThemeSwitcher themeSwitcher = new AnimatedThemeSwitcher(scene, new CircleClipOut());
themeSwitcher.init(); // Required!

// Later...
scene.getStylesheets().setAll("/dark.css"); // Plays the transition

// This also works with add, set, remove and other List methods.

Note that not every type of root can be animated properly, such as VBox and HBox. Parents that allow overlapping children, i.e. Pane, are suggested.




Animated values

The animated binding API provides a way to animate the content of a Label every time its associated value changes.

Currency
Code

AnimatedValueLabel<Integer> label = new AnimatedValueLabel<>(0)
                .custom(settings -> settings.withCurve(Curve.EASE_IN_OUT));

// We can also customize the displayed text
label.setTextMapper(value -> "The value is " + value);

// Later...
label.setValue(10); // Plays the transition



Other examples

Button
Button color and border

Shadow
Drop shadows + label

Root switch
Root switch

Layout alignment
Layout alignment (inspired by the Edge home page)




FXML

  • Animated

    <?import eu.iamgio.animated.binding.Animated?>
    <?import eu.iamgio.animated.binding.AnimationSettings?>
    <?import eu.iamgio.animated.binding.presets.AnimatedOpacity?>
    
    <Animated>
          <child>
              <MyNode/>
          </child>
          <targetProperties>
              <!-- Properties to animate, related to the current child -->
              <AnimatedOpacity>
                  <settings>
                      <AnimationSettings duration="1500ms" curve="EASE_IN_OUT"/>
                  </settings>
              </AnimatedOpacity>
              <!-- Multiple properties are supported -->
          </targetProperties>
      </Animated>
  • AnimatedContainer

    <?import eu.iamgio.animated.transition.container.AnimatedHBox?>
    <?import eu.iamgio.animated.transition.Animation?>
    
    <AnimatedHBox>
        <!-- Optional: defaults to FadeIn -->
        <in>
            <Animation type="ZoomIn"/>
        </in>
        <!-- Optional: defaults to FadeOut -->
        <out>
            <Animation type="ZoomOut"/>
        </out>
        <!-- Optional initial children -->
        <MyNode1/>      
        <MyNode2/>
        <!-- ... -->      
    </AnimatedHBox>
  • AnimatedSwitcher

    <?import eu.iamgio.animated.transition.AnimatedSwitcher?>
    <?import eu.iamgio.animated.transition.Animation?>
    
    <AnimatedSwitcher>
        <!-- Optional: defaults to FadeIn -->
        <in>
            <Animation type="RectangleClipIn"/>
        </in>
        <!-- Optional: defaults to FadeOut -->
        <out>
            <Animation type="SlideOutUp" speed="0.5"/>
        </out>
        <!-- Optional initial child -->
        <child>
            <InitialChild/>
        </child>
    </AnimatedSwitcher>
  • AnimatedLabel

    <?import eu.iamgio.animated.transition.AnimatedLabel?>
    <?import eu.iamgio.animated.transition.Animation?>
    
    <AnimatedLabel text="Initial text">
        <!-- Optional: defaults to FadeIn -->
        <in>
            <Animation type="BounceIn" delay="300ms"/>
        </in>
        <!-- Optional: defaults to FadeOut -->
        <in>
            <Animation type="BounceOut"/>
        </in>
    </AnimatedLabel>
  • AnimatedValueLabel
    FXML has issues with generic types, so you will need to instantiate an AnimatedIntValueLabel or AnimatedDoubleValueLabel.

    <?import eu.iamgio.animated.binding.AnimationSettings?>
    <?import eu.iamgio.animated.binding.value.AnimatedIntValueLabel?>
    
    <AnimatedIntValueLabel value="0">
        <!-- Optional customization -->
        <settings>
            <AnimationSettings duration="1500ms" curve="EASE_IN_OUT"/>
        </settings>
    </AnimatedIntValueLabel>

When instantiating an <Animation type="..."/>, the class name (case sensitive) is searched in the following packages:




Kotlin extensions

Extension functions make the library less verbose with Kotlin.
Example:

val animated: Animated = Animated(child, child.someProperty().animated())
val pair: AnimationPair = FadeIn().options(speed = 1.5) outTo FadeOut()

animated's People

Contributors

iamgio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

animated's Issues

Unsearchable name

Is it possible to change the library name? animated is a very generic name that is hard to get search results for, even when you search for animated javafx.

add two node into AnimatedVBox at first and it cause a NullPointerException

I use the version 0.40

I just add 2 node into the AnimatedVBox

val contentVbox = AnimatedVBox(AnimationPair(null,BounceOutRight()))
        contentVbox.add(Text("hello"))
        contentVbox.add(Text("hell2o"))

the exception is this

java.lang.NullPointerException
	at eu.iamgio.animated.AnimatedContainer$Handler.relocate(AnimatedContainer.java:180)
	at eu.iamgio.animated.AnimatedContainer$Handler.lambda$null$0(AnimatedContainer.java:133)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run$$$capture(InvokeLaterDispatcher.java:95)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
	at java.lang.Thread.run(Thread.java:748)

the image is this

imageUrl: https://img2022.cnblogs.com/blog/1210268/202202/1210268-20220211160512243-1641573797.png

by the way,the version of kotlin what you use is to high for my project ๐Ÿ˜‚

I just use the kotlin 1.3.72 because of use the tornadofx framework

AnimatedThemeSwitcher change duration

Hi! I have a JavaFX app window which contains a button that toggles the theme from light to dark, and i added AnimatedThemeSwitcher into the function bound to the said button. I was wondering if I could make it have another duration, like half a second or something? Because if i press that button twice or thrice after the initial change happens, the application freezes for a bit and then it switches the theme.

Theme switching animation buggy when using GridPane as root

Hi again! I've encountered a small problem with using AnimatedThemeSwitcher to switch themes when using a GridPane as root.

So, I am loading an FXML file that has a GridPane containing a few nodes, and it seems the screenshot with the old theme applied gets set only on the first row and it doesn't span across all of the rows, so the animation plays on the top half of the app screen, while the bottom half is instantly displayed with the new theme applied.

Recording.2024-02-14.205835.mp4

add a playback callback

I want to perform some actions at the end of the animation, such as closing the page, which is to pan the page out of the visible range, and then clear it. Now that there is no animation, you need to monitor the coordinates, which becomes very troublesome.I hope there is a function that can be called when the animation is finished playing.It is best to set callback functions for multiple animations separately.Or rather, this is a lifecycle. Thank you !.

Add module-info

It is quite standard for JavaFX libraries and projects to be modular, can you please add a proper module-info with an appropriate name.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.