public final class MorphTransition

  1. Object
  2. Transition
  3. MorphTransition

ImplementsAnimation

A transition inspired by the Android L release morph activity effect allowing a set of components in one form/container to morph into another in a different container/form.

Beyond the original position / size morph, this transition can also tween the opacity, rotation and scale of each morphed target and can morph arbitrary rendered elements (an Image, a rendered SVG, a snapshot of any Component, …) that don’t have to be named components living in a Form – see #morph(MorphElement).

The transition is also scrubbable: instead of letting the framework drive it from the animation clock you can step it to an arbitrary normalized position t in [0, 1] with #setProgress(double). Scrubbing is deterministic (it never reads the wall clock), is repeatable, and can move backwards, which makes it compose cleanly with a frame-export loop that renders the same animation at a set of progress fractions.

Nested types

class MorphTransition.MorphElementDescribes an arbitrary rendered element that the morph tweens between two states: position, size, opacity, rotation and scale.

Methods

public MorphTransition snapshotMode(boolean enabled)Enables the image-snapshot path.
public boolean isSnapshotMode()Returns the current snapshot-mode setting.
public static MorphTransition create(int duration)Creates a transition with the given duration, this transition should be modified with the builder methods such as morph
public Transition copy(boolean reverse)Create a copy of the transition, usually the transition used is a copy.
public MorphTransition morph(String cmp)Morphs the component with the given source name in the source container hierarchy to the component with the same name in the destination hierarchy
public MorphTransition morph(String source, String to)Morphs the component with the given source name in the source container hierarchy to the component with the given name in the destination hierarchy
public MorphTransition opacity(String sourceName, float fromOpacity, float toOpacity)Tweens the opacity of a named morph target from fromOpacity to toOpacity (both in the [0, 1] range) over the course of the transition.
public MorphTransition rotation(String sourceName, float fromDegrees, float toDegrees)Tweens the rotation of a named morph target from fromDegrees to toDegrees about its center.
public MorphTransition scale(String sourceName, float fromScale, float toScale)Tweens an extra scale multiplier (about the target center, on top of the interpolated bounds) of a named morph target from fromScale to toScale.
public MorphTransition morph(MorphTransition.MorphElement element)Morphs an arbitrary rendered element (an Image, a rendered SVG, a snapshot of any Component) between two states.
public MorphTransition setProgress(double t)Steps the transition to a normalized progress t in [0, 1] without reading the animation clock.
public double getProgress()Returns the last normalized progress requested through #setProgress(double) (0 until the first call).
public boolean isScrubbing()Returns true once #setProgress(double) has been used to drive this transition manually.
public float getCurrentOpacity(String sourceName)The interpolated opacity (0..1) of a named morph target at the current progress, or 1 when no opacity tween is registered for that name.
public float getCurrentRotation(String sourceName)The interpolated rotation (degrees) of a named morph target at the current progress, or 0 when no rotation tween is registered for that name.
public float getCurrentScale(String sourceName)The interpolated scale multiplier of a named morph target at the current progress, or 1 when no scale tween is registered for that name.
public void initTransition()Callback thats invoked before a transition begins, the source form may be null for the first form in the application.
public boolean animate()Allows the animation to reduce “repaint” calls when it returns false.
public void cleanup()Optional operation to cleanup the garbage left over by a running transition
public void paint(Graphics g)Draws the animation, within a component the standard paint method would be invoked since it bares the exact same signature.

Inherited methods

Method details

snapshotMode

public MorphTransition snapshotMode(boolean enabled)

Enables the image-snapshot path. Each (source, dest) pair is rendered once into an Image at initTransition() (clipped to the component’s own bounds; off-viewport children do not contribute pixels), then the tween draws those images at the interpolated (x, y, w, h).

Use this when:

  • The source lives inside a scrolling container whose scrollX/scrollY would otherwise leak off-viewport child pixels into the morph (the cross-form morph clipping artifact).
  • The source has children with dynamic content (a BrowserComponent, a video frame, a custom-painted background) that should be frozen visually for the duration of the animation.
  • The source’s parent applies a clip that the layered pane wouldn’t replicate.

Snapshot mode is also required for the per-named-target #rotation, #scale and #opacity tweens to have a visible effect, since those transforms are applied while drawing the captured image.

Default is off to preserve back-compat with the legacy live-paint path. Always pair with a screenshot regression test (see scripts/hellocodenameone/.../MorphTransitionTest).

Parameters

enabled boolean
true to snapshot, false for the legacy live-paint mode

Returns

this transition (for chaining with #morph(String) etc.)

isSnapshotMode

public boolean isSnapshotMode()
Returns the current snapshot-mode setting. See #snapshotMode(boolean).

create

public static MorphTransition create(int duration)
Creates a transition with the given duration, this transition should be modified with the builder methods such as morph

Parameters

duration int
the duration of the transition

Returns

a new Morph transition instance

copy

public Transition copy(boolean reverse)
Create a copy of the transition, usually the transition used is a copy.

Parameters

reverse boolean
creates a new transition instance with “reverse” behavior useful for signifying “back” operations

Returns

new transition instance

morph

public MorphTransition morph(String cmp)
Morphs the component with the given source name in the source container hierarchy to the component with the same name in the destination hierarchy

Parameters

cmp String
the compoennt name

Returns

this so morph operations can be chained as MorphTransition t = MorphTransition.create(300).morph(“a”).(“c”);

morph

public MorphTransition morph(String source, String to)
Morphs the component with the given source name in the source container hierarchy to the component with the given name in the destination hierarchy

Returns

this so morph operations can be chained as MorphTransition t = MorphTransition.create(300).morph(“a”, “b”).(“c”, “d”);

opacity

public MorphTransition opacity(String sourceName, float fromOpacity, float toOpacity)
Tweens the opacity of a named morph target from fromOpacity to toOpacity (both in the [0, 1] range) over the course of the transition. The named target must also be registered with #morph(String) / #morph(String, String). The opacity is applied while drawing the captured snapshot, so this requires #snapshotMode(boolean) to be on to have a visible effect.

Parameters

sourceName String
the source-side name passed to #morph(String)
fromOpacity float
the opacity at t = 0, 0 (transparent) to 1 (opaque)
toOpacity float
the opacity at t = 1

Returns

this transition for chaining

rotation

public MorphTransition rotation(String sourceName, float fromDegrees, float toDegrees)
Tweens the rotation of a named morph target from fromDegrees to toDegrees about its center. Requires #snapshotMode(boolean) to have a visible effect and an affine-capable Graphics (every modern port); where affine isn’t supported the rotation is silently skipped.

Parameters

sourceName String
the source-side name passed to #morph(String)
fromDegrees float
rotation in degrees at t = 0
toDegrees float
rotation in degrees at t = 1

Returns

this transition for chaining

scale

public MorphTransition scale(String sourceName, float fromScale, float toScale)
Tweens an extra scale multiplier (about the target center, on top of the interpolated bounds) of a named morph target from fromScale to toScale. A value of 1 leaves the interpolated size unchanged; use this for a “pop” that overshoots the target box. Requires #snapshotMode(boolean) to have a visible effect.

Parameters

sourceName String
the source-side name passed to #morph(String)
fromScale float
scale multiplier at t = 0
toScale float
scale multiplier at t = 1

Returns

this transition for chaining

morph

public MorphTransition morph(MorphTransition.MorphElement element)
Morphs an arbitrary rendered element (an Image, a rendered SVG, a snapshot of any Component) between two states. Unlike the named-component morph this does not require the element to be a child of the source / destination form – the element is drawn as an overlay at the interpolated position, size, opacity, rotation and scale. Multiple elements can be registered and they compose with the named-component morphs.

Parameters

element MorphTransition.MorphElement
the element description built via MorphElement#create

Returns

this transition for chaining

setProgress

public MorphTransition setProgress(double t)

Steps the transition to a normalized progress t in [0, 1] without reading the animation clock. 0 is the source state, 1 the destination state; intermediate values are eased with the same ease-in-out curve the clock-driven path uses. The call is deterministic, repeatable and may move backwards, so it composes with a frame-export loop that renders the animation at a set of progress fractions:

MorphTransition t = MorphTransition.create(600).morph(element);
t.init(source, dest);
t.initTransition();
for (double f = 0; f <= 1.0; f += 1.0 / frames) {
    t.setProgress(f);
    t.paint(frameGraphics);
}
t.cleanup();

#initTransition() must have been invoked first (the AbstractTransitionScreenshotTest harness does this for you). Scrubbing and the clock-driven animate() lifecycle are not meant to be mixed on the same instance.

Parameters

t double
the normalized progress, clamped to [0, 1]

Returns

this transition for chaining

getProgress

public double getProgress()
Returns the last normalized progress requested through #setProgress(double) (0 until the first call).

isScrubbing

public boolean isScrubbing()
Returns true once #setProgress(double) has been used to drive this transition manually.

getCurrentOpacity

public float getCurrentOpacity(String sourceName)
The interpolated opacity (0..1) of a named morph target at the current progress, or 1 when no opacity tween is registered for that name. Primarily useful for tests / custom rendering.

getCurrentRotation

public float getCurrentRotation(String sourceName)
The interpolated rotation (degrees) of a named morph target at the current progress, or 0 when no rotation tween is registered for that name.

getCurrentScale

public float getCurrentScale(String sourceName)
The interpolated scale multiplier of a named morph target at the current progress, or 1 when no scale tween is registered for that name.

initTransition

public void initTransition()
Callback thats invoked before a transition begins, the source form may be null for the first form in the application.

animate

public boolean animate()
Allows the animation to reduce “repaint” calls when it returns false. It is called once for every frame. Frames are defined by the com.codename1.ui.Display class.

Returns

true if a repaint is desired or false if no repaint is necessary

cleanup

public void cleanup()
Optional operation to cleanup the garbage left over by a running transition

paint

public void paint(Graphics g)
Draws the animation, within a component the standard paint method would be invoked since it bares the exact same signature.

Parameters

g Graphics
graphics context