Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve shake animation programmatically?

Tags:

android

How can I achieve shake/wobble animation in android programmatically. There is a AndroidViewAnimations library available through which we can get the effect. But I don't want to use any library for this purpose as it tends to increase the apk size.

The code present in the AndroidViewAnimations for shake animation is as follows:

public class ShakeAnimator extends BaseViewAnimator {
    @Override
    public void prepare(View target) {
        getAnimatorAgent().playTogether(
                ObjectAnimator.ofFloat(target, "translationX", 0, 25, -25, 25, -25,15, -15, 6, -6, 0)
        );
    }
}

Can annyone tell me how can I achieve this without any libraries.

like image 919
Nikita Ameen Avatar asked Jan 14 '15 12:01

Nikita Ameen


People also ask

How to create shake animation in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken button to show shake animation for image view.


1 Answers

let's say that mView is the view you want to animate:

ObjectAnimator
  .ofFloat(mView, "translationX", 0, 25, -25, 25, -25,15, -15, 6, -6, 0)
  .setDuration(duration)
  .start();
like image 69
Budius Avatar answered Oct 14 '22 21:10

Budius