androidanimationvibration

How can I make vibrate animation for ImageView


I have no idea for this animation.

How can I do it via XML like that? Or another solution?

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

Thanks for your help


Solution

  • This code shakes a view in horizontal direction

    shake.xml

    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXDelta="0"
        android:interpolator="@anim/cycle_5"
        android:toXDelta="10" />
    

    cycle_5.xml

    <?xml version="1.0" encoding="utf-8"?>
    <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
        android:cycles="5" />
    

    Method to shake ImageView

    public void onShakeImage() {    
       Animation shake;
       shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
    
       ImageView image;
       image = (ImageView) findViewById(R.id.image_view);
    
       image.startAnimation(shake); // starts animation
    }