Search in sources :

Example 6 with Animation

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;
}
Also used : Graphics(com.codename1.ui.Graphics) Motion(com.codename1.ui.animations.Motion) Form(com.codename1.ui.Form) Animation(com.codename1.ui.animations.Animation)

Example 7 with Animation

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");
    }
}
Also used : Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) IOException(java.io.IOException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) Graphics(com.codename1.ui.Graphics) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Animation(com.codename1.ui.animations.Animation) RecordControl(javax.microedition.media.control.RecordControl)

Example 8 with Animation

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();
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Border(com.codename1.ui.plaf.Border)

Example 9 with Animation

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);
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 10 with Animation

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();
        }
    };
}
Also used : Motion(com.codename1.ui.animations.Motion) ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) Point(com.codename1.ui.geom.Point)

Aggregations

Animation (com.codename1.ui.animations.Animation)13 Motion (com.codename1.ui.animations.Motion)8 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)7 Dimension (com.codename1.ui.geom.Dimension)7 Style (com.codename1.ui.plaf.Style)7 Form (com.codename1.ui.Form)6 Component (com.codename1.ui.Component)5 Graphics (com.codename1.ui.Graphics)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 ArrayList (java.util.ArrayList)5 ActionListener (com.codename1.ui.events.ActionListener)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 FontImage (com.codename1.ui.FontImage)3 Transition (com.codename1.ui.animations.Transition)3 IOException (java.io.IOException)3 BufferedOutputStream (com.codename1.io.BufferedOutputStream)2 Dialog (com.codename1.ui.Dialog)2 Image (com.codename1.ui.Image)2 PeerComponent (com.codename1.ui.PeerComponent)2 AnimationObject (com.codename1.ui.animations.AnimationObject)2