2012-10-04 10 views
5

Ich implementiere eine App, wo ich die Symbole wie im iPhone schütteln möchte.Shake Icons in Android als iPhone auf Long Press

Wie kann ich das gleiche implementieren?

Ich habe hier den Code meiner Animation beigefügt. Bitte schlagen Sie mir eine Lösung vor. Ich habe versucht, die Soln, die bereits auf dem Stack-Überlauf angegeben ist, aber es funktioniert nicht in meinem Fall. Und diese Klasse ist eine Nicht-Aktivitätsklasse, in die dieser Animationssatz geschrieben wird.

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); 
} 

Dank

Antwort

7

Mai werden Sie shake.xml in anim Ordner vornehmen können.

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

es jetzt in Ihrer Methode verwenden animateDragged() als

Animation animation=AnimationUtils.loadAnimation(this,R.anim.shake); 
v.startAnimation(animation); 
+0

seine nicht funktioniert bro. Die Methode loadAnimation (Context, int) im Typ AnimationUtils ist nicht anwendbar für die Argumente (DraggableGridView, int) –

+0

Danke, ich habe mich aus dem Weg gemacht. Danke –

+0

@GauravArora Könnten Sie bitte erklären, wie Sie Ihr Problem gelöst haben. Ich bin neugierig zu lernen und zu verstehen. –

0

Sie auch programmatisch für jede Ansicht machen können verschiedene zufällige Animationen. Verwenden Sie einfach Code-Schnipsel unten in der Schleife für jede Ihrer 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);