Is there a 'lighter' method of solving this custom AnimationRunnable?
<p>I've been working on this problem for a couple days now and have been unable to solve the problem.</p>

<p>I have a number of animations all implemented as Runnables that will run continually while the user uses the application. There are 4 different animations spanning from logo_animation000 to 115 and z_animation000 to 515:</p>

<ul>
<li>ConnectingAnimation (Running while the application is connecting to a device)</li>
<li>ConnectingCompleteAnimation (Run once when the connection is complete)</li>
<li>TransitiontoSleepingAnimation(Run once when the state of the application changes)</li>
<li>SleepingAnimation(Run while the state of the application is : "Sleeping"</li>
</ul>

<p>These animations are too big to work by using the regular animation-list and as such I was forced to use my own implementation. I "solved" this problem with the following class :</p>

<pre><code>    abstract class MyAnimationRunnable implements Runnable {
        protected final int interval;
        protected final String pattern;
        protected final OptimisedImageView view;
        protected long firstFrameAt;
        protected int current;
        protected Drawable drawable;
        protected int frameCounter = 0;

        public MyAnimationRunnable(int interval, int firstFrame, String pattern, OptimisedImageView view) {
            this.interval = interval;
            this.pattern = pattern;
            this.view = view;
            this.current = firstFrame;
            this.firstFrameAt = SystemClock.elapsedRealtime();
            handler.post(this);
        }

        @Override
        public void run() {
            final String name = String.format(Locale.ENGLISH, pattern, current);
            final int id = getResources().getIdentifier(name, "drawable", getPackageName());
            if (id != 0) {
                final Drawable drawable = getResources().getDrawable(id);
                view.setImageDrawable(drawable);
            }
            current++;
            if (runAgain()) {
                frameCounter++;
                long nextAt = firstFrameAt + (frameCounter * interval);
                long delay = nextAt - SystemClock.elapsedRealtime() - 1;
                if (delay &gt; 0) {
                    handler.postDelayed(this, delay);
                } else {
                    this.run();
                }
            }
        }

        protected abstract boolean runAgain();
    };

   private void startSleepingAnimation() 
    {

        zzzAnimation = new MyAnimationRunnable(50, 1, "z_animation%04d", zzzImageView) {
            @Override
            protected boolean runAgain() {
                if (current == 510) {
                    current = 470; // this seems to fit into the infinite loop.
                }
                return true;
            }
        };
    }

    private void startConnectingAnimation() {
        handler.removeCallbacks(connectingCompleteAnimation);
        handler.removeCallbacks(zzzAnimation);
        handler.removeCallbacks(transitionToSleepingAnimation);
        connectingResourceCount = 1;
        zzzImageView.setImageBitmap(null);
        connectingAnimation = new MyAnimationRunnable(50, connectingResourceCount, "logo_animation%04d", babyImageView) {
            @Override
            protected boolean runAgain() {
                if (current == 30) {
                    current = 1;
                }
                connectingResourceCount = current;
                return true;
            }
        };
    }

    // The remaining two animations have been left out since this oughta be sufficient for understanding the code.
</code></pre>

<p>This works alright on my Samsung Galaxy S3. However on older phones this causes a crash after 20-30 seconds. Any ideas on how I can make this code lighter and more efficient?</p>
