vxh.viet
8/4/2016 - 8:41 AM

Simple shaking animation

Simple shaking animation

Source: StackOverflow

Answer: Simple shaking animation

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/overshoot_interpolator"
    android:fillAfter="true">

    <translate
        android:startOffset="40"
        android:fromXDelta="0%p"
        android:toXDelta="10%p"
        android:duration="50" />

    <translate
        android:startOffset="90"
        android:fromXDelta="0%p"
        android:toXDelta="-25%p"
        android:duration="50" />

    <translate
        android:startOffset="140"
        android:fromXDelta="0%p"
        android:toXDelta="25%p"
        android:duration="50" />

    <translate
        android:startOffset="190"
        android:fromXDelta="0%p"
        android:toXDelta="-20%p"
        android:duration="50" />

    <translate
        android:startOffset="240"
        android:fromXDelta="0%p"
        android:toXDelta="10%p"
        android:duration="50" />
</set>
Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
// View element to be shaken
TextView s = (TextView) findViewById(R.id.testView);
// Perform animation
s.startAnimation(shake);