Monday, May 7, 2012

Shake effect in Android

Shake effect in Android

We can give shaking effect for any layout component in android.
For that first we have to create a folder anim under res folder. In anim first create an xml 'cycle_7.xml'
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />

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

Then in class file,import these two,
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

Then add this in code,(when and where that you can decide),
Animation shake = AnimationUtils.loadAnimation(CurrentActivity.this, R.anim.shake);
findViewById(R.id.textViewShake).startAnimation(shake);

Now run.............!