Hello, I've created the following ScaleTransition that provides a "zoom"
effect for components that include it. It's very simple and is useful,
for example, in ComponentMouseListeners. I'm sorry for not including
much comments but this is an old class I rescued from a past project and
didn't touch it.
It takes a multi-component approach, meaning many components can be
registered for this transition, instead of one component per transition.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.devpower.pivot;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.effects.Decorator;
import org.apache.pivot.wtk.effects.ScaleDecorator;
/**
* This transition provides a "zoom" effect for components that use it.
*
* @author emerino
*/
public class ScaleTransition extends Transition {
private class CustomScaleDecorator extends ScaleDecorator {
private float initialScaleX = 1.0f;
private float initialScaleY = 1.0f;
public void setInitialScales(ScaleDecorator decorator) {
initialScaleX = decorator.getScaleX();
initialScaleY = decorator.getScaleY();
}
public float getInitialScaleX() {
return initialScaleX;
}
public float getInitialScaleY() {
return initialScaleY;
}
}
private static final int DEFAULT_DURATION = 200;
private static final int DEFAULT_RATE = 28;
private Map<Component, CustomScaleDecorator> componentDecoratorMap =
Collections.synchronizedMap(new HashMap<Component, CustomScaleDecorator>());
private boolean reversed;
private float zoom = 0.3f;
public ScaleTransition() {
this(DEFAULT_DURATION, DEFAULT_RATE);
}
public ScaleTransition(int duration, int rate) {
super(duration, rate);
}
public Set<Component> getComponents() {
return componentDecoratorMap.keySet();
}
public void addComponent(Component component) {
if (component == null) {
throw new IllegalArgumentException("Component cannot be null");
}
if (!componentDecoratorMap.containsKey(component)) {
CustomScaleDecorator scaleDecorator = new CustomScaleDecorator();
for (Decorator decorator : component.getDecorators()) {
if (decorator instanceof ScaleDecorator) {
scaleDecorator.setInitialScales(scaleDecorator);
break;
}
}
componentDecoratorMap.put(component, scaleDecorator);
component.getDecorators().add(scaleDecorator);
}
}
public void addComponents(Collection<Component> components) {
for (Component c : components) {
addComponent(c);
}
}
public void setZoom(float zoom) {
if (isRunning()) {
throw new IllegalStateException("Transition in progress");
}
this.zoom = zoom;
}
public void setReversed(boolean reversed) {
if (isRunning()) {
throw new IllegalStateException("Transition in progress");
}
this.reversed = reversed;
}
@Override
protected void update() {
float scaleX = 0;
float scaleY = 0;
CustomScaleDecorator scaleDecorator = null;
for (Component component : componentDecoratorMap.keySet()) {
scaleDecorator = componentDecoratorMap.get(component);
if (!reversed) {
scaleX = scaleDecorator.getInitialScaleX() + (zoom * getPercentComplete());
scaleY = scaleDecorator.getInitialScaleY() + (zoom * getPercentComplete());
} else {
scaleX = (scaleDecorator.getInitialScaleX() + zoom) - (zoom * getPercentComplete());
scaleY = (scaleDecorator.getInitialScaleY() + zoom) - (zoom * getPercentComplete());
}
scaleDecorator.setScaleX(scaleX);
scaleDecorator.setScaleY(scaleY);
component.repaint();
}
}
public void removeComponent(Component component) {
if (componentDecoratorMap.containsKey(component)) {
component.getDecorators().remove(componentDecoratorMap.remove(component));
}
}
public void clearComponents() {
for (Component c : componentDecoratorMap.keySet()) {
removeComponent(c);
}
}
}