start/stop animation on View - Android User Interface

Android examples for User Interface:View Animation

Description

start/stop animation on View

Demo Code


//package com.java2s;

import android.graphics.drawable.AnimationDrawable;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

public class Main {
    /**//from w  w  w .  j a va2s  .  c  o  m
     * start/stop animation
     *
     * @param view
     * @param start
     */
    static public void startViewAnimation(View view, boolean start) {
        if (view != null) {
            // background drawable
            startDrawableAnimation(view.getBackground(), start);
            if (view instanceof ImageView) {
                // image drawable
                startDrawableAnimation(((ImageView) view).getDrawable(),
                        start);
            }
        }
    }

    /**
     * start/stop animation
     *
     * @param d
     * @param start
     */
    static public void startDrawableAnimation(Drawable d, boolean start) {
        if (d instanceof AnimationDrawable) {
            if (start) {
                ((AnimationDrawable) d).start();
            } else {
                ((AnimationDrawable) d).stop();
            }
        }
    }
}

Related Tutorials