shake the view with Animation - Android Animation

Android examples for Animation:Shake Animation

Description

shake the view with Animation

Demo Code


//package com.java2s;
import android.view.View;

import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;

public class Main {
    public static void shake(View view, float fromXDelta, float toXDelta,
            float cycles, long durationMillis, final boolean isBanClick) {
        translate(view, fromXDelta, toXDelta, 0.0f, 0.0f, cycles,
                durationMillis, isBanClick);
    }// w  w  w  .  j av a2  s . com
    public static void shake(View view, float fromXDelta, float toXDelta,
            float cycles, long durationMillis) {
        translate(view, fromXDelta, toXDelta, 0.0f, 0.0f, cycles,
                durationMillis, false);
    }
    public static void shake(View view, float cycles, long durationMillis,
            final boolean isBanClick) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, durationMillis,
                isBanClick);
    }
    public static void shake(View view, float cycles,
            final boolean isBanClick) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, 700, isBanClick);
    }
    public static void shake(View view, float cycles, long durationMillis) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, durationMillis,
                false);
    }
    public static void shake(View view, long durationMillis,
            final boolean isBanClick) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, durationMillis,
                isBanClick);
    }
    public static void shake(View view, float cycles) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, 700, false);
    }
    public static void shake(View view, long durationMillis) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, durationMillis, false);
    }

    /**
     * shake the view.
     * default x delta is from 0.0f to 10.0f.
     * duration is 700ms.
     * repeat 7 times.
     *
     * @param view       view to be shake
     * @param isBanClick whether view can click in animation
     */
    public static void shake(View view, final boolean isBanClick) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, 700, isBanClick);
    }

    /**
     * shake the view.
     * default x delta is from 0.0f to 10.0f.
     * duration is 700ms.
     * repeat 7 times.
     *
     * @param view view to be shake
     */
    public static void shake(View view) {
        translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, 700, false);
    }

    /**
     * translate the view.
     *
     * @param view           view to be translated
     * @param fromXDelta     Change in X coordinate to apply at the start of the
     *                       animation
     * @param toXDelta       Change in X coordinate to apply at the end of the
     *                       animation
     * @param fromYDelta     Change in Y coordinate to apply at the start of the
     *                       animation
     * @param toYDelta       Change in Y coordinate to apply at the end of the
     *                       animation
     * @param cycles         Repeats the animation for a specified number of cycles {@link CycleInterpolator}.
     * @param durationMillis Duration in milliseconds
     * @param isBanClick     whether view can click in animation
     */
    public static void translate(final View view, float fromXDelta,
            float toXDelta, float fromYDelta, float toYDelta, float cycles,
            long durationMillis, final boolean isBanClick) {
        TranslateAnimation translateAnimation = new TranslateAnimation(
                fromXDelta, toXDelta, fromYDelta, toYDelta);
        translateAnimation.setDuration(durationMillis);
        if (cycles > 0.0) {
            translateAnimation
                    .setInterpolator(new CycleInterpolator(cycles));
        }
        translateAnimation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                if (isBanClick) {
                    view.setClickable(false);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (isBanClick) {
                    view.setClickable(true);
                }
            }
        });
        view.startAnimation(translateAnimation);
    }

    /**
     * translate the view.
     *
     * @param view           view to be translated
     * @param fromXDelta     Change in X coordinate to apply at the start of the
     *                       animation
     * @param toXDelta       Change in X coordinate to apply at the end of the
     *                       animation
     * @param fromYDelta     Change in Y coordinate to apply at the start of the
     *                       animation
     * @param toYDelta       Change in Y coordinate to apply at the end of the
     *                       animation
     * @param cycles         Repeats the animation for a specified number of cycles {@link CycleInterpolator}.
     * @param durationMillis Duration in milliseconds
     */
    public static void translate(final View view, float fromXDelta,
            float toXDelta, float fromYDelta, float toYDelta, float cycles,
            long durationMillis) {
        translate(view, fromXDelta, toXDelta, fromYDelta, toYDelta, cycles,
                durationMillis, false);
    }
}

Related Tutorials