Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an image shake/wiggle when clicked?

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!

like image 928
student2015 Avatar asked May 04 '12 20:05

student2015


4 Answers

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>
like image 79
Simon Avatar answered Nov 18 '22 00:11

Simon


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

like image 22
Rajkumari Avatar answered Nov 18 '22 02:11

Rajkumari


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>
like image 6
Abhishek Gupta Avatar answered Nov 18 '22 01:11

Abhishek Gupta


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" />
like image 1
Vishal Khanna Avatar answered Nov 18 '22 02:11

Vishal Khanna