2013-01-19 14 views
6

Ich versuche, ein Alpha zu machen und in einem RelativeLayout zu übersetzen. Ich sehe beides:Starten Sie zwei Animationen im selben Layout

AlphaAnimation alpha; 
alpha = new AlphaAnimation(0.0f, 1.0f); 
alpha.setDuration(1500); 
alpha.setFillAfter(true); 

TranslateAnimation translate; 
translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,                  
       Animation.RELATIVE_TO_SELF, 0, 
       Animation.RELATIVE_TO_SELF, 1, 
       Animation.RELATIVE_TO_SELF, 0); 
translate.setDuration(1000); 

So beginne ich die Animation in meinem RelativeLayout

RelativeLayout.startAnimation(translate); 
RelativeLayout.startAnimation(alpha); 

Das Problem ist, dass in diesem Fall nur die Alpha-Animation starten und nicht die Übersetzung. Kann mir jemand helfen? Die Frage ist, wie kann ich zwei verschiedene Animationen zur gleichen Zeit im gleichen Objekt (Relative-Layout in meinem Fall) beginne


ich die Frage lösen. Ich habe es:

AnimationSet animationSet = new AnimationSet(true); 
animationSet.addAnimation(alpha); 
animationSet.addAnimation(translate); 

RelativeLayout.startAnimation(animationSet); 

Antwort

5

Ihre aktuellen Code wird nicht funktionieren, denn sobald die erste Animation beginnt, der zweite endet und beginnt sich. Also musst du warten bis es fertig ist.

Try this:

translate.setAnimationListener(new AnimationListener() { 

    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    public void onAnimationEnd(Animation animation) { 
     // TODO Auto-generated method stub 
     RelativeLayout.startAnimation(alpha); 
    } 
}); 

Wenn Sie wollen, dass sie gleichzeitig auszuführen, würde ich vorschlagen, dass Sie eine animation.xml Datei in Ihrem res/Anim/Ordner erstellen.

Beispiel:

<?xml version="1.0" encoding="utf-8"?> 
<set 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<scale 
android:fromXScale="1.0" 
android:fromYScale="1.0" 
android:toXScale=".75" 
android:toYScale=".75" 
android:duration="1500"/> 
<rotate 
android:fromDegrees="0" 
android:toDegrees="360" 
android:duration="1500" 
android:pivotX="50%" 
android:pivotY="50%" /> 
<scale 
android:fromXScale=".75" 
android:fromYScale=".75" 
android:toXScale="1" 
android:toYScale="1" 
android:duration="1500"/> 
</set> 

Java-Code:

Animation multiAnim = AnimationUtils.loadAnimation(this, R.anim.animation); 
    RelativeLayout.startAnimation(multiAnim); 
7

Sie Animationssatz verwenden können, wenn Sie zwei Animation in der gleichen Zeit ausgeführt werden soll:

http://developer.android.com/reference/android/view/animation/AnimationSet.html

Für Beispiel;

as = new AnimationSet(true); 
as.setFillEnabled(true); 
as.setInterpolator(new BounceInterpolator()); 

TranslateAnimation ta = new TranslateAnimation(-300, 100, 0, 0); 
ta.setDuration(2000); 
as.addAnimation(ta); 

TranslateAnimation ta2 = new TranslateAnimation(100, 0, 0, 0); 
ta2.setDuration(2000); 
ta2.setStartOffset(2000); // allowing 2000 milliseconds for ta to finish 
as.addAnimation(ta2); 
Verwandte Themen