2013-01-03 13 views
14

Ich bin nicht in der Lage, Ansichtsanimation für überhöhte Layouts zu setzen. habe ich den folgenden Code-SchnipselView Animation von rechts nach links android

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

und 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> 

Ist jede Sache, die ich fehlt?

Danke.

+0

Was erhalten Sie, wenn Sie versuchen, die Animation auszuführen? – WarrenFaith

+0

Es wird nur von links nach rechts verschoben, dann von rechts nach links, wenn eine Ansicht animiert wird. – sd33din90

+0

und Sie wollen, was stattdessen passiert? – WarrenFaith

Antwort

55

Hier ist der Code für die gleitende Animation für Ansicht.

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

und jetzt Animation auf Sicht starten

pageView.startAnimation(inFromRightAnimation()); 

Danke,

+1

, aber wie Sie die Animation für eine Ansicht starten. –

+0

yourview.startAnimation (inFromRightAnimation()); –

+0

sehr nette Antwort – Lokesh

1

Wenn Sie versuchen, die Ansicht beim Erstellen zu animieren, müssen Sie entweder die layoutAnimation XML-Eigenschaft festlegen oder setLayoutAnimation() aufrufen.

Wenn Sie nur Ihre Ansicht so aussehen lassen möchten, wie sie sich bewegt, benötigen Sie eine TranslateAnimation; Siehe diese Antwort: https://stackoverflow.com/a/4214490/832776 Auch wenn Sie die Animation wiederholen möchten, dann rufen Sie setAnimationListener() und in onAnimationEnd() starten Sie einfach die Animation erneut.

Wenn Sie versuchen, die Sicht permanent zu bewegen, sehen: http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400

1

Ich weiß, dass Sie die Antwort bereits akzeptiert haben. Aber ich denke, diese Antwort wird hilfreich für jemanden sein, der das liest. Sie können versuchen Entfernen .xml von, pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));

Verwandte Themen