Search in sources :

Example 1 with Motion

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

the class Effects method squareShadow.

/**
 * Generates a square shadow and returns it
 *
 * @param width the width of the shadow image
 * @param height the height of the shadow image
 * @param blurRadius a shadow is blurred using a gaussian blur when available, a value of 10 is often satisfactory
 * @param opacity the opacity of the shadow between 0 - 1 where 1 is completely opaque
 * @return an image containing the shadow for source
 */
public static Image squareShadow(int width, int height, int blurRadius, float opacity) {
    Image img = Image.createImage(width + blurRadius * 2, height + blurRadius * 2, 0);
    Graphics g = img.getGraphics();
    int destAlpha = (int) (opacity * 255.0);
    g.setColor(0);
    Motion lin = Motion.createLinearMotion(2, destAlpha, blurRadius);
    // draw a gradient of sort for the shadow
    for (int iter = blurRadius - 1; iter >= 0; iter--) {
        lin.setCurrentMotionTime(iter);
        g.setAlpha(lin.getValue());
        g.drawRect(blurRadius + iter, blurRadius + iter, width - iter * 2, height - iter * 2);
    }
    if (Display.getInstance().isGaussianBlurSupported()) {
        img = Display.getInstance().gaussianBlurImage(img, blurRadius);
    }
    return img;
}
Also used : Graphics(com.codename1.ui.Graphics) Motion(com.codename1.ui.animations.Motion) RGBImage(com.codename1.ui.RGBImage) Image(com.codename1.ui.Image)

Example 2 with Motion

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

the class SwipeBackSupport method startBackTransition.

void startBackTransition(final Form currentForm, Form destination) {
    final Transition t = destination.getTransitionOutAnimator().copy(true);
    if (t instanceof CommonTransitions) {
        Transition originalTransition = currentForm.getTransitionOutAnimator();
        currentForm.setTransitionOutAnimator(CommonTransitions.createEmpty());
        Form blank = new Form() {

            protected boolean shouldSendPointerReleaseToOtherForm() {
                return true;
            }
        };
        blank.addPointerDraggedListener(pointerDragged);
        blank.addPointerReleasedListener(pointerReleased);
        blank.addPointerPressedListener(pointerPressed);
        blank.setTransitionInAnimator(CommonTransitions.createEmpty());
        blank.setTransitionOutAnimator(CommonTransitions.createEmpty());
        blank.show();
        currentForm.setTransitionOutAnimator(originalTransition);
        ((CommonTransitions) t).setMotion(new LazyValue<Motion>() {

            public Motion get(Object... args) {
                return new ManualMotion(((Integer) args[0]).intValue(), ((Integer) args[1]).intValue(), ((Integer) args[2]).intValue());
            }
        });
        t.init(currentForm, destination);
        t.initTransition();
        blank.setGlassPane(new Painter() {

            public void paint(Graphics g, Rectangle rect) {
                t.animate();
                t.paint(g);
            }
        });
    }
}
Also used : Graphics(com.codename1.ui.Graphics) Motion(com.codename1.ui.animations.Motion) CommonTransitions(com.codename1.ui.animations.CommonTransitions) Form(com.codename1.ui.Form) Transition(com.codename1.ui.animations.Transition) Painter(com.codename1.ui.Painter) Rectangle(com.codename1.ui.geom.Rectangle)

Example 3 with Motion

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

the class PulsateEditor method updateTimeline.

private void updateTimeline() {
    AnimationObject[] anim = new AnimationObject[get(frames) * 2 - 1];
    internalImages = new EncodedImage[get(frames)];
    int small = Math.min(get(smallSize), get(largeSize));
    int large = Math.max(get(smallSize), get(largeSize));
    int dur = get(duration);
    int segment = dur / anim.length;
    Motion calculator = Motion.createSplineMotion(small, large, dur / 2);
    float ratioOfLargest = ((float) large) / 100.0f;
    int timelineWidth = (int) (ratioOfLargest * ((float) sourceImage.getWidth()));
    int timelineHeight = (int) (ratioOfLargest * ((float) sourceImage.getHeight()));
    for (int iter = 0; iter < internalImages.length; iter++) {
        calculator.setCurrentMotionTime(segment * iter);
        int percentAtTime = calculator.getValue();
        float ratioAtTime = ((float) percentAtTime) / 100.0f;
        int currentWidth = (int) (sourceImage.getWidth() * ratioAtTime);
        int currentHeight = (int) (sourceImage.getHeight() * ratioAtTime);
        internalImages[iter] = EncodedImage.create(scale(sourceImage, currentWidth, currentHeight));
        anim[iter] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
        anim[iter].setStartTime(segment * iter);
        anim[iter].setEndTime(segment * iter + segment);
        // peek at the next frame to calculate the diff
        calculator.setCurrentMotionTime(segment * iter + segment);
        int percentAtNextFrame = calculator.getValue();
        float ratioAtNextFrame = ((float) percentAtNextFrame) / 100.0f;
        int nextWidth = (int) (sourceImage.getWidth() * ratioAtNextFrame);
        int nextHeight = (int) (sourceImage.getHeight() * ratioAtNextFrame);
        anim[iter].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentHeight, nextHeight);
        anim[iter].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentWidth, nextWidth);
        anim[iter].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - nextWidth) / 2);
        anim[iter].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - nextHeight) / 2);
        // create the "inverse" shrinking animation object
        if (iter > 0) {
            int nextOffset = anim.length - iter;
            anim[nextOffset] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
            anim[nextOffset].setStartTime(segment * nextOffset);
            if (iter == 1) {
                // this resolves any rounding errors that might have occured in the creation of the frames
                anim[nextOffset].setEndTime(dur);
            } else {
                anim[nextOffset].setEndTime(segment * nextOffset + segment);
            }
            // peek at the previous frame to calculate the diff
            calculator.setCurrentMotionTime(segment * iter - segment);
            int percentAtPreviousFrame = calculator.getValue();
            float ratioAtPreviousFrame = ((float) percentAtPreviousFrame) / 100.0f;
            int previousWidth = (int) (sourceImage.getWidth() * ratioAtPreviousFrame);
            int previousHeight = (int) (sourceImage.getHeight() * ratioAtPreviousFrame);
            anim[nextOffset].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentHeight, previousHeight);
            anim[nextOffset].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentWidth, previousWidth);
            anim[nextOffset].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - previousWidth) / 2);
            anim[nextOffset].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - previousHeight) / 2);
        }
    }
    currentImage = Timeline.createTimeline(dur, anim, new com.codename1.ui.geom.Dimension(timelineWidth, timelineHeight));
    previewLabel.setIcon(currentImage);
    previewLabel.repaint();
    preview.repaint();
}
Also used : Motion(com.codename1.ui.animations.Motion) AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 4 with Motion

use of com.codename1.ui.animations.Motion 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 5 with Motion

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

the class Component method startTensile.

void startTensile(int offset, int dest, boolean vertical) {
    Motion draggedMotion;
    if (tensileDragEnabled) {
        draggedMotion = Motion.createDecelerationMotion(offset, dest, 500);
        draggedMotion.start();
    } else {
        draggedMotion = Motion.createLinearMotion(offset, dest, 0);
        draggedMotion.start();
    }
    decelerationMotion = draggedMotion;
    if (vertical) {
        draggedMotionY = draggedMotion;
    } else {
        draggedMotionX = draggedMotion;
    }
    // just to be sure, there are some cases where this doesn't work as expected
    Form p = getComponentForm();
    if (p != null) {
        p.registerAnimatedInternal(this);
    }
}
Also used : Motion(com.codename1.ui.animations.Motion)

Aggregations

Motion (com.codename1.ui.animations.Motion)14 Component (com.codename1.ui.Component)5 Form (com.codename1.ui.Form)5 Graphics (com.codename1.ui.Graphics)5 Animation (com.codename1.ui.animations.Animation)3 Container (com.codename1.ui.Container)2 Image (com.codename1.ui.Image)2 RGBImage (com.codename1.ui.RGBImage)2 AnimationObject (com.codename1.ui.animations.AnimationObject)2 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)2 Point (com.codename1.ui.geom.Point)2 Style (com.codename1.ui.plaf.Style)2 Vector (java.util.Vector)2 BufferedOutputStream (com.codename1.io.BufferedOutputStream)1 BrowserComponent (com.codename1.ui.BrowserComponent)1 Dialog (com.codename1.ui.Dialog)1 Display (com.codename1.ui.Display)1 Painter (com.codename1.ui.Painter)1 PeerComponent (com.codename1.ui.PeerComponent)1 CommonTransitions (com.codename1.ui.animations.CommonTransitions)1