Android Open Source - CoinFlip Custom Animation Drawable

From Project

Back to project page CoinFlip.


The source code is released under:

GNU General Public License

If you think the Android project CoinFlip listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

 **from  w ww. ja va 2 s.  c  o  m*/
 * Jul 12, 2014 4:31 PM | variable
 * Copyright (c) 2014 Richard Banasiak
 * This file is part of CoinFlip.
 *    CoinFlip is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *    CoinFlip is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    GNU General Public License for more details.
 *    You should have received a copy of the GNU General Public License
 *    along with CoinFlip.  If not, see <>.

package com.banasiak.coinflip.lib;

import android.os.Handler;
import android.util.Log;

// AnimationDrawable does not provide a callback after the animation is
// finished.  This abstract class raises an onAnimationFinish() method
// after the final frame of the animation is rendered.

public abstract class CustomAnimationDrawable extends AnimationDrawable {

    // debugging tag
    private static final String TAG = CustomAnimationDrawable.class.getSimpleName();

    Handler mAnimationHandler;
    Runnable mAnimationRunable;

    public CustomAnimationDrawable(AnimationDrawable aniDraw) {
        // Add each frame to this CustomAnimationDrawable
        for (int i = 0; i < aniDraw.getNumberOfFrames(); i++) {
            this.addFrame(aniDraw.getFrame(i), aniDraw.getDuration(i));

    public void start() {

        Log.d(TAG, "start()");

        // Call super.start() to call the base class start animation method.
        // Then add a handler to call onAnimationFinish() when the total
        // duration for the animation has passed.

        mAnimationRunable = new Runnable() {
            @Override public void run() {

        mAnimationHandler = new Handler();
        mAnimationHandler.postDelayed(mAnimationRunable, getTotalDuration());


    public int getTotalDuration() {
        Log.d(TAG, "getTotalDuration()");

        int iDuration = 0;

        for (int i = 0; i < this.getNumberOfFrames(); i++) {
            iDuration += this.getDuration(i);

        return iDuration;

    // call via onPause() to shut this thread down when the app drops to the background.
    public void removeCallbacks() {
        Log.d(TAG, "removeCallbacks()");

    // called when the animation finishes
    protected abstract void onAnimationFinish();

Java Source Code List