Example usage for android.graphics.drawable Animatable isRunning

List of usage examples for android.graphics.drawable Animatable isRunning

Introduction

In this page you can find the example usage for android.graphics.drawable Animatable isRunning.

Prototype

boolean isRunning();

Source Link

Document

Indicates whether the animation is running.

Usage

From source file:Main.java

public static void startVDAnimation(ImageView imageView, @DrawableRes int inactiveResId,
        @DrawableRes int activeResId, int duration) {
    int drawableResId = imageView.isSelected() ? activeResId : inactiveResId;
    Drawable drawable = ContextCompat.getDrawable(imageView.getContext(), drawableResId);
    imageView.setImageDrawable(drawable);

    if (drawable instanceof Animatable) {
        Animatable animatable = (Animatable) drawable;
        if (animatable.isRunning()) {
            animatable.stop();//from w w  w. j  a  v  a 2s .c o  m
        }

        animatable.start();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            imageView.setSelected(!imageView.isSelected());
        } else {
            imageView.postDelayed(() -> {
                imageView.setSelected(!imageView.isSelected());
                int nextDrawableResId = imageView.isSelected() ? activeResId : inactiveResId;
                Drawable nextDrawable = ContextCompat.getDrawable(imageView.getContext(), nextDrawableResId);
                imageView.setImageDrawable(nextDrawable);
            }, duration);
        }
    }
}

From source file:io.github.importre.animatedicons.AnimatedButton.java

/**
 * Change the checked state of the view to the inverse of its current state
 *///from  w  ww  . ja v a 2  s. co  m
public void toggle() {
    checked = !checked;
    setImageDrawable(checked ? onDrawable : offDrawable);

    if (isLollipop()) {
        Drawable drawable = getDrawable();
        if (drawable instanceof Animatable) {
            Animatable animatable = (Animatable) drawable;
            if (animatable.isRunning()) {
                animatable.stop();
            }
            animatable.start();
        }
    }
}

From source file:cn.qingchengfit.widgets.AnimatedButton.java

/**
 * Change the checked state of the view to the inverse of its current state
 *///from ww  w . j  ava  2s .  c  o m
public void toggle() {
    checked = !checked;
    setImageDrawable(checked ? onDrawable : offDrawable);

    Drawable drawable = getDrawable();
    if (drawable instanceof Animatable) {
        Animatable animatable = (Animatable) drawable;
        if (animatable.isRunning()) {
            animatable.stop();
        }
        animatable.start();
    }
}