2

I'm working on a custom tween effect targeting Android 2.2. This is not a straight View animation (many things are happening based on the progress of the tween), so the Animation classes available in 2.2 aren't sufficient (apparently Animator does this but is not available).

I've implemented the basic logic (porting JS and AS tweening engines I'd written earlier), and it seems to work fine, but is a little slow. For example, running an interval of 25ms in JS or AS produces a smooth visual effect, but seems "chunky" in the Android implementation - reducing the interval to 10ms seemed to help some but it certainly isn't as smooth as the built-in animations.

I'm using Timer and Timer task - I've read that ScheduledThreadPoolExecutor is "preferred" but from what I've read the advantages seem to be more to do with exception handling and multiple tasks (I'll only ever have the one tween running).

Is Timer particularly slow? If ScheduledThreadPoolExecutor more efficient? Is there another, better alternative that I'm not aware of?

TYIA

momo
  • 3,885
  • 4
  • 33
  • 54

3 Answers3

4

for future searchers, the answer was to use just a straight Handler and sendMessage (without a delay).

after lots of experimentation, including Threads, Timers, Executors, etc, the best performance, most predictable result, and simplest code was basically just:

private Handler handler = new Handler() {
    @Override
    public void handleMessage(final Message message) {
        switch (message.what) {
            case TWEEN:
            try {
                double progress = timeKeeper.getEasedProgress(5);
                float position = (float) originalValue + ((destinationValue - originalValue) * progress));  
                setValue(position);
                if(!timeKeeper.atEnd()){
                    sendEmptyMessage(TWEEN);
                }
            } catch (Exception e) {
            }
        }               
    }
};

where originalValue, destinationValue and setValue are just arbitrary members to handle the delta of the tween. timeKeeper is a very simple class that just measures ellapsed time, and returns the delta (ellapsed / duration). getEasedProgress just applies some basic easing interpolation to that delta.

thanks to pskink from the google android developers mailing list for pointing me in the right direction.

momo
  • 3,885
  • 4
  • 33
  • 54
2

The main advantage I see with ScheduledThreadPoolExecutor is you can pool number of threads, if one thread is somehow hungup other thread can be used from the defined pool. Here is interesting SO discussion on this topic.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • thanks for the comment. that link you included is a link to this page, btw. What would you suggest for an short duration, high interval timed operation like this? – momo Feb 10 '12 at 21:56
  • good info in that link - i wasn't using a daemon and will try that now. +1 – momo Feb 10 '12 at 22:09
2

You do not need a ScheduledThreadPoolExecutor because you do not need a ThreadPool, you only need a single thread to manage your animation. The slowdown is probably in the implementation of your animation engine. I'm not sure how effective Timer is.

onit
  • 6,306
  • 3
  • 24
  • 31
  • thanks for the comment. the animation engine is straight math, and should be very fast by itself (the code that's reacting to that math - not part of the engine itself but rather listeners - might not be). What would you suggest for an short duration, high interval timed operation like this? – momo Feb 10 '12 at 21:55
  • @BigMoMo I haven't implemented an animation engine, but why not just implement a Thread that sleeps every 25ms, and then draws the animation? I'm assuming your AS and JS animations run smooth on a computer, which probably has much more processing power than your android phone as well. – onit Feb 10 '12 at 22:00