Search in sources :

Example 26 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class AndroidImplementation method captureAudio.

public void captureAudio(final ActionListener response) {
    if (!checkForPermission(Manifest.permission.RECORD_AUDIO, "This is required to record the audio")) {
        return;
    }
    try {
        final Form current = Display.getInstance().getCurrent();
        final File temp = File.createTempFile("mtmp", ".3gpp");
        temp.deleteOnExit();
        if (recorder != null) {
            recorder.release();
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder.setOutputFile(temp.getAbsolutePath());
        final Form recording = new Form("Recording");
        recording.setTransitionInAnimator(CommonTransitions.createEmpty());
        recording.setTransitionOutAnimator(CommonTransitions.createEmpty());
        recording.setLayout(new BorderLayout());
        recorder.prepare();
        recorder.start();
        final Label time = new Label("00:00");
        time.getAllStyles().setAlignment(Component.CENTER);
        Font f = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
        f = f.derive(getDisplayHeight() / 10, Font.STYLE_PLAIN);
        time.getAllStyles().setFont(f);
        recording.addComponent(BorderLayout.CENTER, time);
        recording.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) {
                int seconds = sec % 60;
                int minutes = sec / 60;
                String secStr = seconds < 10 ? "0" + seconds : "" + seconds;
                String minStr = minutes < 10 ? "0" + minutes : "" + minutes;
                String txt = minStr + ":" + secStr;
                time.setText(txt);
            }
        });
        Container south = new Container(new com.codename1.ui.layouts.GridLayout(1, 2));
        Command cancel = new Command("Cancel") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }
                current.showBack();
                response.actionPerformed(null);
            }
        };
        recording.setBackCommand(cancel);
        south.add(new com.codename1.ui.Button(cancel));
        south.add(new com.codename1.ui.Button(new Command("Save") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }
                current.showBack();
                response.actionPerformed(new ActionEvent(temp.getAbsolutePath()));
            }
        }));
        recording.addComponent(BorderLayout.SOUTH, south);
        recording.show();
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException("failed to start audio recording");
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) Font(com.codename1.ui.Font) Paint(android.graphics.Paint) BorderLayout(com.codename1.ui.layouts.BorderLayout) com.codename1.ui(com.codename1.ui) Animation(com.codename1.ui.animations.Animation) MediaRecorder(android.media.MediaRecorder) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 27 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class AndroidImplementation method repaint.

@Override
public void repaint(Animation cmp) {
    if (myView != null && myView.alwaysRepaintAll()) {
        if (cmp instanceof Component) {
            Component c = (Component) cmp;
            c.setDirtyRegion(null);
            if (c.getParent() != null) {
                cmp = c.getComponentForm();
            } else {
                Form f = getCurrentForm();
                if (f != null) {
                    cmp = f;
                }
            }
        } else {
            // make sure the form is repainted for standalone anims e.g. in the case
            // of replace animation
            Form f = getCurrentForm();
            if (f != null) {
                super.repaint(f);
            }
        }
    }
    super.repaint(cmp);
}
Also used : BrowserComponent(com.codename1.ui.BrowserComponent) Component(com.codename1.ui.Component) PeerComponent(com.codename1.ui.PeerComponent)

Example 28 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class Component method growShrink.

/**
 * Grows or shrinks this component to its new preferred size, this method
 * essentially takes a component whose preferred size has changed and creates a "growing"
 * effect that lasts for the duration. Notice that some components (such as text areas)
 * don't report proper preferred size untill they are laid out once. Hence the first time
 * around a text area (or container containing a text area) will not produce the expected
 * effect. This can be solved by invoking revalidate before the call to this method only the
 * first time around!
 *
 * @param duration the duration in milliseconds for the grow/shrink animation
 */
public void growShrink(int duration) {
    Motion wMotion = Motion.createSplineMotion(getWidth(), getPreferredW(), duration);
    Motion hMotion = Motion.createSplineMotion(getHeight(), getPreferredH(), duration);
    wMotion.start();
    hMotion.start();
    setPreferredSize(new Dimension(getWidth(), getHeight()));
    // we are using bgpainter just to save the cost of creating another class
    getComponentForm().registerAnimated(new BGPainter(wMotion, hMotion));
    getComponentForm().revalidate();
}
Also used : Motion(com.codename1.ui.animations.Motion) Dimension(com.codename1.ui.geom.Dimension)

Example 29 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class Component method animate.

/**
 * {@inheritDoc}
 */
public boolean animate() {
    if (!visible) {
        return false;
    }
    Image bgImage = getStyle().getBgImage();
    boolean animateBackground = bgImage != null && bgImage.isAnimation() && bgImage.animate();
    Motion m = getAnimationMotion();
    // perform regular scrolling
    if (m != null && destScrollY != -1 && destScrollY != getScrollY()) {
        // change the variable directly for efficiency both in removing redundant
        // repaints and scroll checks
        setScrollY(m.getValue());
        if (destScrollY == scrollY) {
            destScrollY = -1;
            deregisterAnimatedInternal();
            updateTensileHighlightIntensity(0, 0, m != null);
        }
        return true;
    }
    boolean animateY = false;
    boolean animateX = false;
    // perform the dragging motion if exists
    if (draggedMotionY != null) {
        // change the variable directly for efficiency both in removing redundant
        // repaints and scroll checks
        int dragVal = draggedMotionY.getValue();
        // this can't be a part of the parent if since we need the last value to arrive
        if (draggedMotionY.isFinished()) {
            if (dragVal < 0) {
                startTensile(dragVal, 0, true);
            } else {
                int iv = getInvisibleAreaUnderVKB();
                int edge = (getScrollDimension().getHeight() - getHeight() + iv);
                if (dragVal > edge && edge > 0) {
                    startTensile(dragVal, getScrollDimension().getHeight() - getHeight() + iv, true);
                } else {
                    if (snapToGrid && getScrollY() < edge && getScrollY() > 0) {
                        boolean tVal = tensileDragEnabled;
                        tensileDragEnabled = true;
                        int dest = getGridPosY();
                        int scroll = getScrollY();
                        if (dest != scroll) {
                            startTensile(scroll, dest, true);
                        } else {
                            draggedMotionY = null;
                        }
                        tensileDragEnabled = tVal;
                    } else {
                        draggedMotionY = null;
                    }
                }
            }
            // special callback to scroll Y to allow developers to override the setScrollY method effectively
            setScrollY(dragVal);
            updateTensileHighlightIntensity(dragVal, getScrollDimension().getHeight() - getHeight() + getInvisibleAreaUnderVKB(), false);
        }
        if (scrollListeners != null) {
            scrollListeners.fireScrollEvent(this.scrollX, dragVal, this.scrollX, this.scrollY);
        }
        scrollY = dragVal;
        onScrollY(scrollY);
        updateTensileHighlightIntensity(0, 0, false);
        animateY = true;
    }
    if (draggedMotionX != null) {
        // change the variable directly for efficiency both in removing redundant
        // repaints and scroll checks
        int dragVal = draggedMotionX.getValue();
        // this can't be a part of the parent if since we need the last value to arrive
        if (draggedMotionX.isFinished()) {
            if (dragVal < 0) {
                startTensile(dragVal, 0, false);
            } else {
                int edge = (getScrollDimension().getWidth() - getWidth());
                if (dragVal > edge && edge > 0) {
                    startTensile(dragVal, getScrollDimension().getWidth() - getWidth(), false);
                } else {
                    if (snapToGrid && getScrollX() < edge && getScrollX() > 0) {
                        boolean tVal = tensileDragEnabled;
                        tensileDragEnabled = true;
                        int dest = getGridPosX();
                        int scroll = getScrollX();
                        if (dest != scroll) {
                            startTensile(scroll, dest, false);
                        } else {
                            draggedMotionX = null;
                        }
                        tensileDragEnabled = tVal;
                    } else {
                        draggedMotionX = null;
                    }
                }
            }
            // special callback to scroll X to allow developers to override the setScrollY method effectively
            setScrollX(dragVal);
        }
        if (scrollListeners != null) {
            scrollListeners.fireScrollEvent(dragVal, this.scrollY, this.scrollX, this.scrollY);
        }
        scrollX = dragVal;
        onScrollX(scrollX);
        animateX = true;
    }
    if (animateY || animateX) {
        return true;
    }
    if (getClientProperty("$pullToRelease") != null) {
        return true;
    }
    Painter bgp = getStyle().getBgPainter();
    boolean animateBackgroundB = bgp != null && bgp.getClass() != BGPainter.class && bgp instanceof Animation && (bgp != this) && ((Animation) bgp).animate();
    animateBackground = animateBackgroundB || animateBackground;
    if (getUIManager().getLookAndFeel().isFadeScrollBar()) {
        if (tensileHighlightIntensity > 0) {
            tensileHighlightIntensity = Math.max(0, tensileHighlightIntensity - (scrollOpacityChangeSpeed * 2));
        }
        if (scrollOpacity > 0 && !dragActivated) {
            scrollOpacity = Math.max(0, scrollOpacity - scrollOpacityChangeSpeed);
            return true;
        }
    }
    if (!animateBackground && (destScrollY == -1 || destScrollY == scrollY) && !animateBackground && m == null && draggedMotionY == null && draggedMotionX == null && !dragActivated) {
        tryDeregisterAnimated();
    }
    return animateBackground;
}
Also used : Motion(com.codename1.ui.animations.Motion) Animation(com.codename1.ui.animations.Animation) ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) Point(com.codename1.ui.geom.Point)

Example 30 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class Component method initScrollMotion.

private void initScrollMotion() {
    // the component might not be registered for animation if it started off
    // as smaller than the screen and grew (e.g. by adding components to the container
    // once it is visible).
    Form f = getComponentForm();
    if (f != null) {
        f.registerAnimatedInternal(this);
    }
    Motion m = Motion.createLinearMotion(initialScrollY, destScrollY, getScrollAnimationSpeed());
    setAnimationMotion(m);
    m.start();
}
Also used : Motion(com.codename1.ui.animations.Motion)

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