IntAnimation.java

/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.statusshout.animations;

/**
 * Animates integers.
 */
public class IntAnimation {
    // Constants
    public static final int EASING_CURVE_LINEAR = 0;
    public static final int EASING_CURVE_INOUTQUAD = 1;
    public static final int EASING_CURVE_OUTINQUAD = 2;

    public static final int STATE_NOT_STARTED = 0;
    public static final int STATE_RUNNING = 1;
    public static final int STATE_STOPPED = 2;
    public static final int STATE_FINISHED = 3;

    protected static final int UPDATE_INTERVAL = 1000 / 40;
    protected static final int UNDEFINED = -1;

    // Members
    protected AnimationListener _listener = null;
    protected EasingCurve _easingCurve = null;
    protected volatile int _state = STATE_NOT_STARTED;
    protected volatile int _value = UNDEFINED;

    /**
     * Starts the animation.
     * @param first The "from" value.
     * @param last The "to" value.
     * @param duration The duration of the animation.
     * @param easingCurve The easing curve to use.
     * @return True if the animation was successfully started, false otherwise.
     */
    public synchronized boolean start(int first, int last, int duration, int easingCurve) {
        setEasingCurve(easingCurve);
        boolean success = false;
        
        if (_state != STATE_RUNNING
            && _easingCurve.calculate(first, last, duration))
        {
            success = start();
        }
        
        return success;
    }

    /**
     * Sets the animation listener.
     * @param listener The listener or null if none.
     */
    public void setListener(AnimationListener listener) {
        _listener = listener;
    }

    /**
     * Starts the animation.
     * @return True if the animation was started, false otherwise.
     */
    protected synchronized boolean start() {
        if (_easingCurve == null || _state == STATE_RUNNING) {
            System.out.println("AbstractAnimation.start(): No curve or already running!");
            return false;
        }
        
        new Thread() {
            public void run() {
                setState(STATE_RUNNING);
                long time = 0;
                long ticks = 0;
                
                while (_state == STATE_RUNNING) {
                    time = System.currentTimeMillis();
                    
                    try {
                        Thread.sleep(UPDATE_INTERVAL);
                    }
                    catch (InterruptedException e) {
                    }
                    
                    ticks = System.currentTimeMillis() - time;
                    
                    if (_easingCurve.hasNext((int)ticks)) {
                        _value = _easingCurve.getNextValue((int)ticks);
                    }
                    else {
                        _value = _easingCurve.getLastValue();
                        _state = STATE_FINISHED;
                    }
                    
                    if (_listener != null) {
                        _listener.onAnimatedValueChanged(_value);
                    }
                }
                
                if (_listener != null) {
                    _listener.onAnimationStateChanged(_state);
                }
            }
        }.start();
        
        return true;
    }

    /**
     * Stops the animation.
     */
    public synchronized void stop() {
        _state = STATE_STOPPED;
    }

    /** 
     * @return The state of the animation.
     */
    public int getState() {
        return _state;
    }

    /** 
     * @return The current animated value.
     */
    public synchronized int getCurrentValue() {
        return _value;
    }

    /**
     * Sets the state and notifies the listener.
     * @param state The state to set.
     */
    private synchronized void setState(final int state) {
        if (_state != state) {
            _state = state;
            
            if (_listener != null) {
                _listener.onAnimationStateChanged(_state);
            }
        }
    }

    /**
     * 
     * @param easingCurve
     */
    protected void setEasingCurve(int easingCurve) {
        switch (easingCurve) {
            case EASING_CURVE_LINEAR: _easingCurve = new LinearEasingCurve(); break;
            case EASING_CURVE_INOUTQUAD: _easingCurve = new InOutQuadEasingCurve(); break;
            case EASING_CURVE_OUTINQUAD: _easingCurve = new OutInQuadEasingCurve(); break;
            default: _easingCurve = new LinearEasingCurve(); break;
        }
    }
}

Last updated 9 October 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×