Sometimes we have to apply Shake or Vibrate effect to a view like text view or any other view for indicating user that something went wrong or wrong input.To achieve this we have some steps to follow:-
Step 2: Create the Interpolator cycle_inter2.xml in anim folder under res
For Vibrate
Step 1: Create the object for the Vibrator class
private Vibrator vibrateObject;
Step 2 : Block of code for vibrate the device for 300 millisecond
Step 3: Add permission in Manifest.xml
vibrateObject = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 300 milliseconds
vibrateObject.vibrate(300);
<uses-permission android:name="android.permission.VIBRATE" />
Shake The View Or Text
Step 1: Create shake_effect.xml in anim folder
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:interpolator="@anim/cycle_inter2"
android:toXDelta="20" />
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="2" />
Step 3: Apply on Views or text which you want to Shake findViewById(R.id.textView).startAnimation(AnimationUtils.loadAnimation(YourActivity.this, R.anim.shake_effect));
findViewById(R.id.simpleView).startAnimation(AnimationUtils.loadAnimation(YourActivity.this, R.anim.shake_effect));
That's it now you can apply shake or Vibrate Effect on your application.Thank you for reading this blog.
Comments
Post a Comment