2012-10-04 11 views
5

Estoy implementando una aplicación en la que quiero agitar los iconos como ocurre en iPhone.Shake Icons en Android como iPhone en Long Press

¿Cómo puedo implementar lo mismo?

He adjuntado aquí el código de mi animación. Por favor sugiérame alguna solución. He probado la solución que ya está especificada en el desbordamiento de la pila, pero no funciona en mi caso. Y esta clase es una clase no activa en la que se escribe este conjunto de animación.

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

Gracias

Respuesta

7

puede ser usted puede hacer shake.xml en anim carpeta.

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

Ahora lo utilizan en su método animateDragged() como

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

bro no su trabajo. El método loadAnimation (Context, int) en el tipo AnimationUtils no es aplicable para los argumentos (DraggableGridView, int) –

+0

Gracias, salí. Gracias –

+0

@ GauravArora ¿Podría explicarme cómo resolvió su problema? Tengo curiosidad por aprender y entender. –

0

También puede hacer animaciones aleatorias diferentes para cada vista mediante programación. Sólo tiene que utilizar el código siguiente fragmento de código en el bucle para cada uno de sus iconos

 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); 
Cuestiones relacionadas