use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.
the class OnOffSwitch method animateTo.
private void animateTo(final boolean value, final int position) {
int switchButtonPadInt = UIManager.getInstance().getThemeConstant("switchButtonPadInt", 16);
if (Display.getInstance().getDisplayWidth() > 480) {
// is retina
switchButtonPadInt *= 2;
}
final Motion current = Motion.createEaseInOutMotion(Math.abs(position), switchMaskImage.getWidth() - 2 * switchButtonPadInt, 100);
current.start();
deltaX = position;
getComponentForm().registerAnimated(new Animation() {
public boolean animate() {
deltaX = current.getValue();
if (value) {
deltaX *= -1;
}
dragged = true;
if (current.isFinished()) {
dragged = false;
Form f = getComponentForm();
if (f != null) {
f.deregisterAnimated(this);
}
OnOffSwitch.this.setValue(value);
}
repaint();
return false;
}
public void paint(Graphics g) {
}
});
dragged = true;
}
use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.
the class GameCanvasImplementation method captureAudio.
public void captureAudio(ActionListener response) {
captureResponse = response;
try {
final Form current = Display.getInstance().getCurrent();
final MMAPIPlayer player = MMAPIPlayer.createPlayer("capture://audio", null);
RecordControl record = (RecordControl) player.nativePlayer.getControl("RecordControl");
if (record == null) {
player.cleanup();
throw new RuntimeException("Capture Audio is not supported on this device");
}
final Form cam = new Form();
cam.setTransitionInAnimator(CommonTransitions.createEmpty());
cam.setTransitionOutAnimator(CommonTransitions.createEmpty());
cam.setLayout(new BorderLayout());
cam.show();
player.play();
final Label time = new Label("0:00");
cam.addComponent(BorderLayout.SOUTH, time);
cam.revalidate();
ActionListener l = new ActionListener() {
boolean recording = false;
OutputStream out = null;
String audioPath = null;
RecordControl record;
public void actionPerformed(ActionEvent evt) {
if (!recording) {
record = (RecordControl) player.nativePlayer.getControl("RecordControl");
recording = true;
String type = record.getContentType();
String prefix = "";
if (type.endsWith("wav")) {
prefix = ".wav";
} else if (type.endsWith("amr")) {
prefix = ".amr";
} else if (type.endsWith("3gpp")) {
prefix = ".3gp";
}
audioPath = getOutputMediaFile() + prefix;
try {
out = FileSystemStorage.getInstance().openOutputStream(audioPath);
record.setRecordStream(out);
record.startRecord();
cam.registerAnimated(new Animation() {
long current = System.currentTimeMillis();
long zero = current;
int sec = 0;
public boolean animate() {
long now = System.currentTimeMillis();
if (now - current > 1000) {
current = now;
sec++;
return true;
}
return false;
}
public void paint(Graphics g) {
String txt = sec / 60 + ":" + sec % 60;
time.setText(txt);
}
});
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("failed to store audio to " + audioPath);
} finally {
}
} else {
if (out != null) {
try {
record.stopRecord();
record.commit();
out.close();
player.cleanup();
} catch (Exception ex) {
ex.printStackTrace();
}
}
captureResponse.actionPerformed(new ActionEvent(audioPath));
current.showBack();
}
}
};
cam.addGameKeyListener(Display.GAME_FIRE, l);
cam.addPointerReleasedListener(l);
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException("failed to start camera");
}
}
use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.
the class Component method deinitializeImpl.
/**
* Cleansup the initialization flags in the hierachy, notice that paint calls might
* still occur after deinitilization mostly to perform transitions etc.
* <p>However interactivity, animation and event tracking code can and probably
* should be removed by this method.
*/
void deinitializeImpl() {
if (isInitialized()) {
hideNativeOverlay();
paintLockRelease();
setInitialized(false);
setDirtyRegion(null);
Style stl = getStyle();
Image i = stl.getBgImage();
if (i != null) {
i.unlock();
} else {
Border b = stl.getBorder();
if (b != null) {
b.unlock();
}
}
Painter p = stl.getBgPainter();
if (p instanceof BGPainter) {
((BGPainter) p).radialCache = null;
}
deinitialize();
}
}
use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.
the class Component method createStyleAnimation.
/**
* Creates an animation that will transform the current component to the styling of the destination UIID when
* completed. Notice that fonts will only animate within the truetype and native familiy and we recommend that you
* don't shift weight/typeface/style as this might diminish the effect.<br>
* <b>Important: </b> Only unselected styles are animated but once the animation completes all styles are applied.
* @param destUIID the UIID to which this component will gradually shift
* @param duration the duration of the animation or the number of steps
* @return an animation component that can either be stepped or played
*/
public ComponentAnimation createStyleAnimation(final String destUIID, final int duration) {
final Style sourceStyle = getUnselectedStyle();
final Style destStyle = hasInlineUnselectedStyle() ? getUIManager().parseComponentStyle(getInlineStylesTheme(), destUIID, getInlineStylesUIID(destUIID), getInlineUnselectedStyleStrings()) : getUIManager().getComponentStyle(destUIID);
return createStyleAnimation(sourceStyle, destStyle, duration, destUIID);
}
use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.
the class Component method createStyleAnimation.
ComponentAnimation createStyleAnimation(final Style sourceStyle, final Style destStyle, final int duration, final String destUIID) {
int d = duration;
Motion m = null;
if (sourceStyle.getFgColor() != destStyle.getFgColor()) {
m = Motion.createLinearColorMotion(sourceStyle.getFgColor(), destStyle.getFgColor(), d);
}
final Motion fgColorMotion = m;
m = null;
if (sourceStyle.getOpacity() != destStyle.getOpacity()) {
m = Motion.createLinearColorMotion(sourceStyle.getOpacity(), destStyle.getOpacity(), d);
}
final Motion opacityMotion = m;
m = null;
if (sourceStyle.getFont().getHeight() != destStyle.getFont().getHeight() && sourceStyle.getFont().isTTFNativeFont()) {
// allows for fractional font sizes
m = Motion.createLinearMotion(sourceStyle.getFont().getHeight() * 100, destStyle.getFont().getHeight() * 100, d);
}
final Motion fontMotion = m;
m = null;
if (sourceStyle.getPaddingTop() != destStyle.getPaddingTop()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingTop(), destStyle.getPaddingTop(), d);
}
final Motion paddingTop = m;
m = null;
if (sourceStyle.getPaddingBottom() != destStyle.getPaddingBottom()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingBottom(), destStyle.getPaddingBottom(), d);
}
final Motion paddingBottom = m;
m = null;
if (sourceStyle.getPaddingLeftNoRTL() != destStyle.getPaddingLeftNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingLeftNoRTL(), destStyle.getPaddingLeftNoRTL(), d);
}
final Motion paddingLeft = m;
m = null;
if (sourceStyle.getPaddingRightNoRTL() != destStyle.getPaddingRightNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingRightNoRTL(), destStyle.getPaddingRightNoRTL(), d);
}
final Motion paddingRight = m;
m = null;
if (sourceStyle.getMarginTop() != destStyle.getMarginTop()) {
m = Motion.createLinearMotion(sourceStyle.getMarginTop(), destStyle.getMarginTop(), d);
}
final Motion marginTop = m;
m = null;
if (sourceStyle.getMarginBottom() != destStyle.getMarginBottom()) {
m = Motion.createLinearMotion(sourceStyle.getMarginBottom(), destStyle.getMarginBottom(), d);
}
final Motion marginBottom = m;
m = null;
if (sourceStyle.getMarginLeftNoRTL() != destStyle.getMarginLeftNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getMarginLeftNoRTL(), destStyle.getMarginLeftNoRTL(), d);
}
final Motion marginLeft = m;
m = null;
if (sourceStyle.getMarginRightNoRTL() != destStyle.getMarginRightNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getMarginRightNoRTL(), destStyle.getMarginRightNoRTL(), d);
}
final Motion marginRight = m;
m = null;
if (paddingLeft != null || paddingRight != null || paddingTop != null || paddingBottom != null) {
// convert the padding to pixels for smooth animation
int left = sourceStyle.getPaddingLeftNoRTL();
int right = sourceStyle.getPaddingRightNoRTL();
int top = sourceStyle.getPaddingTop();
int bottom = sourceStyle.getPaddingBottom();
sourceStyle.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
sourceStyle.setPadding(top, bottom, left, right);
}
if (marginLeft != null || marginRight != null || marginTop != null || marginBottom != null) {
// convert the margin to pixels for smooth animation
int left = sourceStyle.getMarginLeftNoRTL();
int right = sourceStyle.getMarginRightNoRTL();
int top = sourceStyle.getMarginTop();
int bottom = sourceStyle.getMarginBottom();
sourceStyle.setMarginUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
sourceStyle.setMargin(top, bottom, left, right);
}
final AnimationTransitionPainter ap = new AnimationTransitionPainter();
if (sourceStyle.getBgTransparency() != 0 || destStyle.getBgTransparency() != 0 || (sourceStyle.getBorder() != null && sourceStyle.getBorder().isEmptyBorder()) || (destStyle.getBorder() != null && destStyle.getBorder().isEmptyBorder()) || sourceStyle.getBgImage() != null || destStyle.getBgImage() != null) {
ap.original = sourceStyle.getBgPainter();
ap.dest = destStyle.getBgPainter();
ap.originalStyle = sourceStyle;
ap.destStyle = destStyle;
if (ap.dest == null) {
ap.dest = new BGPainter();
}
sourceStyle.setBgPainter(ap);
}
final Motion bgMotion = Motion.createLinearMotion(0, 255, d);
return new ComponentAnimation() {
private boolean finished;
private boolean stepMode;
private boolean started;
@Override
public boolean isStepModeSupported() {
return true;
}
@Override
public int getMaxSteps() {
return duration;
}
@Override
public void setStep(int step) {
stepMode = true;
if (!finished) {
if (bgMotion != null) {
bgMotion.setCurrentMotionTime(step);
}
if (fgColorMotion != null) {
fgColorMotion.setCurrentMotionTime(step);
}
if (opacityMotion != null) {
opacityMotion.setCurrentMotionTime(step);
}
if (fontMotion != null) {
fontMotion.setCurrentMotionTime(step);
}
if (paddingTop != null) {
paddingTop.setCurrentMotionTime(step);
}
if (paddingBottom != null) {
paddingBottom.setCurrentMotionTime(step);
}
if (paddingLeft != null) {
paddingLeft.setCurrentMotionTime(step);
}
if (paddingRight != null) {
paddingRight.setCurrentMotionTime(step);
}
if (marginTop != null) {
marginTop.setCurrentMotionTime(step);
}
if (marginBottom != null) {
marginBottom.setCurrentMotionTime(step);
}
if (marginLeft != null) {
marginLeft.setCurrentMotionTime(step);
}
if (marginRight != null) {
marginRight.setCurrentMotionTime(step);
}
}
super.setStep(step);
}
@Override
public boolean isInProgress() {
if (!stepMode && !started) {
return true;
}
return stepMode || !((bgMotion == null || bgMotion.isFinished()) && (opacityMotion == null || opacityMotion.isFinished()) && (fgColorMotion == null || fgColorMotion.isFinished()) && (paddingLeft == null || paddingLeft.isFinished()) && (paddingRight == null || paddingRight.isFinished()) && (paddingTop == null || paddingTop.isFinished()) && (paddingBottom == null || paddingBottom.isFinished()) && (marginLeft == null || marginLeft.isFinished()) && (marginRight == null || marginRight.isFinished()) && (marginTop == null || marginTop.isFinished()) && (marginBottom == null || marginBottom.isFinished()) && (fontMotion == null || fontMotion.isFinished()));
}
@Override
protected void updateState() {
if (finished) {
return;
}
if (!started && !stepMode) {
started = true;
if (bgMotion != null) {
bgMotion.start();
}
if (opacityMotion != null) {
opacityMotion.start();
}
if (fgColorMotion != null) {
fgColorMotion.start();
}
if (fontMotion != null) {
fontMotion.start();
}
if (paddingTop != null) {
paddingTop.start();
}
if (paddingBottom != null) {
paddingBottom.start();
}
if (paddingLeft != null) {
paddingLeft.start();
}
if (paddingRight != null) {
paddingRight.start();
}
if (marginTop != null) {
marginTop.start();
}
if (marginBottom != null) {
marginBottom.start();
}
if (marginLeft != null) {
marginLeft.start();
}
if (marginRight != null) {
marginRight.start();
}
}
if (!isInProgress()) {
finished = true;
if (destUIID != null) {
setUIID(destUIID);
}
} else {
if (opacityMotion != null) {
sourceStyle.setOpacity(opacityMotion.getValue());
}
if (fgColorMotion != null) {
sourceStyle.setFgColor(fgColorMotion.getValue());
}
if (bgMotion != null) {
ap.alpha = bgMotion.getValue();
}
if (fontMotion != null) {
Font fnt = sourceStyle.getFont();
fnt = fnt.derive(((float) fontMotion.getValue()) / 100.0f, fnt.getStyle());
sourceStyle.setFont(fnt);
}
if (paddingTop != null) {
sourceStyle.setPadding(TOP, paddingTop.getValue());
}
if (paddingBottom != null) {
sourceStyle.setPadding(BOTTOM, paddingBottom.getValue());
}
if (paddingLeft != null) {
sourceStyle.setPadding(LEFT, paddingLeft.getValue());
}
if (paddingRight != null) {
sourceStyle.setPadding(RIGHT, paddingRight.getValue());
}
if (marginTop != null) {
sourceStyle.setMargin(TOP, marginTop.getValue());
}
if (marginBottom != null) {
sourceStyle.setMargin(BOTTOM, marginBottom.getValue());
}
if (marginLeft != null) {
sourceStyle.setMargin(LEFT, marginLeft.getValue());
}
if (marginRight != null) {
sourceStyle.setMargin(RIGHT, marginRight.getValue());
}
}
}
@Override
public void flush() {
if (bgMotion != null) {
bgMotion.finish();
}
if (opacityMotion != null) {
opacityMotion.finish();
}
if (fgColorMotion != null) {
fgColorMotion.finish();
}
if (fontMotion != null) {
fontMotion.finish();
}
if (paddingTop != null) {
paddingTop.finish();
}
if (paddingBottom != null) {
paddingBottom.finish();
}
if (paddingLeft != null) {
paddingLeft.finish();
}
if (paddingRight != null) {
paddingRight.finish();
}
if (marginTop != null) {
marginTop.finish();
}
if (marginBottom != null) {
marginBottom.finish();
}
if (marginLeft != null) {
marginLeft.finish();
}
if (marginRight != null) {
marginRight.finish();
}
updateState();
}
};
}
Aggregations