Example usage for android.graphics.drawable Animatable start

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

Introduction

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

Prototype

void start();

Source Link

Document

Starts the drawable's animation.

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 ava2 s .  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:cn.qingchengfit.widgets.AnimatedButton.java

/**
 * Change the checked state of the view to the inverse of its current state
 *///from w  w  w. jav  a 2 s  . 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();
    }
}

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.  jav a  2s  . 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();
        }
    }
}