2013-01-03 16 views
14

Non riesco a mettere l'animazione della vista per i layout gonfiati. Ho usato il seguente frammento di codiceVisualizza animazione da destra a sinistra android

pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml)); 

e xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shareInterpolator="false"> 
    <translate android:fromXDelta="0%" android:toXDelta="100%" 
      android:fromYDelta="0%" android:toYDelta="0%" 
     android:duration="700"/> 

</set> 

è un qualsiasi cosa che manca?

Grazie.

+0

Cosa ottieni quando provi a eseguire l'animazione? – WarrenFaith

+0

Scorre semplicemente da sinistra a destra e da destra a sinistra durante l'animazione di una vista. – sd33din90

+0

e vuoi invece che succedere? – WarrenFaith

risposta

55

ecco il codice per l'animazione di scorrimento per la vista.

1)inFromRightAnimation 

    private Animation inFromRightAnimation() { 

     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_PARENT, +1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f); 
     inFromRight.setDuration(500); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
     } 

2)outToLeftAnimation 
    private Animation outToLeftAnimation() { 
    Animation outtoLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, -1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoLeft.setDuration(500); 
    outtoLeft.setInterpolator(new AccelerateInterpolator()); 
    return outtoLeft; 
    } 

3)inFromLeftAnimation 

    private Animation inFromLeftAnimation() { 
    Animation inFromLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, -1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f); 
    inFromLeft.setDuration(500); 
    inFromLeft.setInterpolator(new AccelerateInterpolator()); 
    return inFromLeft; 
    } 

4)outToRightAnimation 

    private Animation outToRightAnimation() { 
    Animation outtoRight = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, +1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoRight.setDuration(500); 
    outtoRight.setInterpolator(new AccelerateInterpolator()); 
    return outtoRight; 
    } 

e ora iniziamo animazione a vista

pageView.startAnimation(inFromRightAnimation()); 

Grazie,

+1

ma come avviare l'animazione per una vista. –

+0

yourview.startAnimation (inFromRightAnimation()); –

+0

risposta molto bella – Lokesh

1

Se si sta tentando di animare la vista quando viene creata per la prima volta, è necessario impostare la proprietà XML layoutAnimation o chiamare setLayoutAnimation().

Se vuoi rendere la tua vista come in movimento, è necessario un TranslateAnimation; vedere questa risposta: https://stackoverflow.com/a/4214490/832776 Inoltre, se si desidera ripetere l'animazione, quindi chiamare setAnimationListener() e in onAnimationEnd() è sufficiente riavviare l'animazione.

Se stai cercando di spostare la vista in modo permanente, vedere questo: http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400

1

So che avete già accettato la risposta. Ma penso che questa risposta sarà utile per qualcuno che legge questo. Puoi provare a rimuovere .xml da, pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));

Problemi correlati