10

I have an imagebutton that I would like to make shake/wiggle when it is touched. I would like it to wiggle in a manner similar to iPhone app icons do when they are pressed for a very long time.

Thanks!

student2015
  • 101
  • 1
  • 1
  • 3
  • Google gave me this: http://stackoverflow.com/questions/9448732/shaking-wobble-view-animation-in-android. – Warpzit May 04 '12 at 20:22
  • 3
    And google gave me this [wiggle wiggle wiggle wiggle yeah](http://www.youtube.com/watch?v=xRKcHK1PiuU) – goat May 14 '12 at 06:50

4 Answers4

19

try to use this one:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="70"
        android:fromDegrees="-5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:interpolator="@android:anim/linear_interpolator"
        android:toDegrees="5" />
    <translate
        android:fromXDelta="-10"
        android:toXDelta="10"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:interpolator="@android:anim/linear_interpolator"
        android:duration="70" />
</set>
Simon
  • 13,173
  • 14
  • 66
  • 90
  • and save this code as an resource file into the folder "anim" with the name "shake.xml". Then you can use the code of @Rajkumari below. Happy coding guy. :) – Nguyen Minh Hien Jul 24 '18 at 11:56
17

Try this for shaking the image on imageview click listener.

 public void onClick(View v)
  {
    Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
    findViewById(R.id.pw).startAnimation(shake);
   }

See this snippet is taking from the android API Demo here

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Rajkumari
  • 199
  • 4
6

I like this one

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

    <translate
        android:fromXDelta="-20"
        android:toXDelta="19"
        android:duration="20" />
    <translate
        android:startOffset="20"
        android:fromXDelta="19"
        android:toXDelta="-18"
        android:duration="20" />
    <translate
        android:startOffset="40"
        android:fromXDelta="-18"
        android:toXDelta="17"
        android:duration="20" />
    <translate
        android:startOffset="60"
        android:fromXDelta="17"
        android:toXDelta="-16"
        android:duration="20" />
    <translate
        android:startOffset="80"
        android:fromXDelta="-16"
        android:toXDelta="14"
        android:duration="20" />
    <translate
        android:startOffset="100"
        android:fromXDelta="14"
        android:toXDelta="-12"
        android:duration="20" />
    <translate
        android:startOffset="120"
        android:fromXDelta="-12"
        android:toXDelta="10"
        android:duration="20" />
    <translate
        android:startOffset="140"
        android:fromXDelta="10"
        android:toXDelta="-7"
        android:duration="20" />
    <translate
        android:startOffset="160"
        android:fromXDelta="-7"
        android:toXDelta="4"
        android:duration="20" />
    <translate
        android:startOffset="200"
        android:fromXDelta="4"
        android:toXDelta="0"
        android:duration="20" />
    <translate
        android:startOffset="220"
        android:fromXDelta="0"
        android:toXDelta="0"
        android:duration="20" />
</set>
Abhishek Gupta
  • 157
  • 1
  • 3
1

Upper one is incorrect try this one.

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="5" />
Remy
  • 4,843
  • 5
  • 30
  • 60
  • 2
    Please also include some explenation to your code to better help others understand it. Instead of just pasting the code saying it is better. – Remy Feb 14 '19 at 12:38