2011-04-29 13 views
5

He creado una animación con el siguiente código.Problema de pausa y reproducción de la animación de Android

private AnimationSet rootSet = new AnimationSet(true); 
private int xstart=258; 
private int ystart=146; 
for(; k<points.length; k++) { 
    if(k==1) { 
    x1 = headX(xstart); 
    y1 = headY(ystart); 
    _animTime = 10; 
    } else { 

    x1 = headX(points[k-1][0]); 
    y1 = headY(points[k-1][1]); 
    } 
    translate = new TranslateAnimation((float)x1, (float)x2, (float)y1, (float)y2); 
    translate.setDuration(_animTime); 
    translate.setFillAfter(true); 
    translate.setInterpolator(new AccelerateDecelerateInterpolator()); 
    totalAnimTime += _animTime; 
    translate.setStartOffset(totalAnimTime); 
    rootSet.addAnimation(translate); 
    rootSet.setFillAfter(true); 
} 

imv1.startAnimation(rootSet); 

Está funcionando bien. Ahora tengo que agregar la función de pausa y reproducción para esta animación. ¿Cómo puedo hacer eso?

+0

¿Ha resuelto su problema? – Deepak

Respuesta

1

Puesto que usted ha ampliado con más información sobre explícitamente que quería utilizar AnimationSet, he encontrado otra solución que debería funcionar para usted.

Código de ejemplo:

Una clase que se extiende AnimationSet ya que se necesita con el fin de cancelar una AnimationSet:

public class CustomAnimationSet extends AnimationSet { 

    private AnimationListener mCustomAnimationSetListener; 

    public CustomAnimationSet(boolean interpolator) { 
      super(interpolator); 
    } 

    public CustomAnimationSet(Context context, AttributeSet attrs) { 
      super(context, attrs); 
    } 

    @Override 
    public void setAnimationListener(AnimationListener listener) { 
      super.setAnimationListener(listener); 
      mCustomAnimationSetListener = listener; 
    } 

    /** 
     * Your cancel method.... 
     */ 
    public void cancel() { 
      // Make sure you're cancelling an ongoing AnimationSet. 
      if(hasStarted() && !hasEnded()) { 
       if(mCustomAnimationSetListener != null) { 
        mCustomAnimationSetListener.onAnimationEnd(this); 
       } 
      } 

      // Reset the AnimationSet's start time. 
      setStartTime(Float.MIN_VALUE); 
    } 

} 

En su clase de Activity:

private CustomAnimationSet mAnimationSet; 

// Init stuff. 

@Override 
public void onClick(View v) { 
    switch(v.getId()) { 
     case R.id.onPlayButton: 
      // Might wanna add Animations before starting next time? 
      mAnimationSet.start(); 
     case R.id.onPauseButton: 
      mAnimationSet.cancel(); 
      mAnimationSet.reset(); 
    } 
} 

Esto es sólo una ejemplo. Por el momento no tengo la oportunidad de probarlo por mi cuenta, esto fue escrito solo para un propósito de ejemplo.

+0

Si la animación se cancela, ¿todavía usa setFillAfter (true)? – pumpkee

+0

@Pennypacker: nunca he intentado prácticamente, pero yo creo que la invocación de una 'cancel()', se cancelarán cualquiera que sea el 'Animation' ha definido – Wroclai

+0

tengo que animar un Animatioset, no un TranslateAnimation. El conjunto de animación no tiene el método de cancelar(). –

Cuestiones relacionadas