2013-01-19 12 views
6

Sto cercando di eseguire un alfa e tradurre in un RelativeLayout. Mi definisco entrambi:Avvia due animazioni nello stesso 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); 

Così ho avviare l'animazione nel mio RelativeLayout

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

Il problema è che in questo caso, solo avviare l'animazione alfa e non la traduzione. Qualcuno può aiutarmi? La domanda è: come posso iniziare due animazioni differenti allo stesso tempo nello stesso oggetto (Layout relativa nel mio caso)


risolvo la questione. Ho aggiunto:

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

RelativeLayout.startAnimation(animationSet); 

risposta

5

Il codice attuale non funziona, perché non appena la prima animazione inizia, la seconda finisce e si inizia. Quindi devi aspettare che sia fatto.

Prova questo:

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

Se si desidera eseguire simultaneamente, io suggerirei di creare un file animation.xml nella res/Anim/cartella.

Esempio:

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

del codice Java:

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

È possibile utilizzare insieme animazione, se si desidera eseguire due l'animazione nello stesso tempo:

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

Per exeample;

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); 
Problemi correlati