shake View with ObjectAnimator - Android android.view.animation

Android examples for android.view.animation:ObjectAnimator

Description

shake View with ObjectAnimator

Demo Code

import android.animation.ObjectAnimator;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class Main{

    public static void shake(View v) {
        final float distance = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4.0f, v.getResources()
                        .getDisplayMetrics());
        final ObjectAnimator animator = ObjectAnimator.ofFloat(v,
                "translationX", 0, distance, 0, -distance, 0);
        animator.setRepeatMode(ObjectAnimator.RESTART);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(4);/*from   w  ww .j a  va2s  .c  o  m*/
        animator.setDuration(150);
        animator.start();
    }

}

Related Tutorials