Shake the view from left to right - Android Animation

Android examples for Animation:Shake Animation

Description

Shake the view from left to right

Demo Code


//package com.java2s;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;

import android.view.View;

public class Main {
    /**/*from   w w w .  ja v  a2  s . c  o  m*/
     * Shake the view from left to right
     *
     * @param view
     * @return
     */
    public static ObjectAnimator leftRightShake(View view) {
        // int delta = view.getResources().getDimensionPixelOffset(R.dimen.spacing_medium);
        int delta = 40;
        PropertyValuesHolder pvhTranslateX = PropertyValuesHolder
                .ofKeyframe(View.TRANSLATION_X, Keyframe.ofFloat(0f, 0),
                        Keyframe.ofFloat(.10f, -delta),
                        Keyframe.ofFloat(.26f, delta),
                        Keyframe.ofFloat(.42f, -delta),
                        Keyframe.ofFloat(.58f, delta),
                        Keyframe.ofFloat(.74f, -delta),
                        Keyframe.ofFloat(.90f, delta),
                        Keyframe.ofFloat(1f, 0f));

        return ObjectAnimator.ofPropertyValuesHolder(view, pvhTranslateX)
                .setDuration(500);
    }
}

Related Tutorials