Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shake Icons in Android as iPhone on Long Press

I'm implementing an App where I want to shake the icons as in iPhone occurs.

How can I implement the same?

I have attached the code of my animation here. Please suggest me any solution. I have tried the soln that is already specified on the stack overflow but its not working in my case. And this class is a non-activity class in which this animation set is written.

protected void animateDragged(){
    View v = getChildAt(dragged);
    int x = getCoorFromIndex(dragged).x + childSize / 2, y = getCoorFromIndex(dragged).y + childSize / 2;
    int l = x - (3 * childSize / 4), t = y - (3 * childSize / 4);
    v.layout(l, t, l + (childSize * 3 / 2), t + (childSize * 3 / 2));
    AnimationSet animSet = new AnimationSet(true);
    ScaleAnimation scale = new ScaleAnimation(.667f, 1, .667f, 1, childSize * 3 / 4, childSize * 3 / 4);
    scale.setDuration(animT);
    AlphaAnimation alpha = new AlphaAnimation(1, .5f);
    alpha.setDuration(animT);

    animSet.addAnimation(scale);
    animSet.addAnimation(alpha);
    animSet.setFillEnabled(true);
    animSet.setFillAfter(true);

    v.clearAnimation();
    v.startAnimation(animSet);
}

Thanks

like image 297
Gaurav Arora Avatar asked Oct 04 '12 05:10

Gaurav Arora


2 Answers

May be you can make shake.xml in anim folder.

    <?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" />

Now use it in your method animateDragged() as

Animation animation=AnimationUtils.loadAnimation(this,R.anim.shake);
v.startAnimation(animation);
like image 195
MGDroid Avatar answered Oct 13 '22 09:10

MGDroid


You may also make different random animations for each view programatically. Just use code snippet below in the loop for each of your icons

        float px = ((float) Math.random() * .5f);
        float py = ((float) Math.random() * .5f);

        RotateAnimation anim = new RotateAnimation((float) Math.PI / 2 * -1.f, (float) Math.PI / 2,
                RotateAnimation.RELATIVE_TO_SELF, .25f + px,
                RotateAnimation.RELATIVE_TO_SELF, .25f + py);
        anim.setDuration((long) (80. + (Math.random() * 50.)));
        anim.setRepeatCount(RotateAnimation.INFINITE);
        anim.setRepeatMode(RotateAnimation.REVERSE);

        v.startAnimation(anim);
like image 31
ILLIA N. Avatar answered Oct 13 '22 09:10

ILLIA N.