2017-05-16 16 views
0

Ich habe ein Fragment mit 3 LinearLayout innerhalb. standardmäßig die erste haben das Gewicht Attribut 1. Jedes Linearlayout eine OnClickListener hat, die 2-Animationen auslösen:Android ObjectAnimator wird vor dem Ende abgebrochen

  • Animation mit einem Gewicht von 0 bis 1 für die eine, die

  • geklickt wird

    Animation mit einem Gewicht von 1 auf 0 für die eine geöffnete derzeit

Mein Problem das ist irgendwann die Animationen nicht vollständig sind, beenden.

ist der durch den OnClickListener ausgelöst Code:

private void launchAnimation(final LinearLayout llToExpand) { 
    ViewWeightAnimationWrapper animationWrapper = new ViewWeightAnimationWrapper(llToExpand); 
    ObjectAnimator anim = ObjectAnimator.ofFloat(animationWrapper, 
      "weight", 
      animationWrapper.getWeight(), 
      1f); 
    anim.setDuration(1000); 

    anim.addListener(new AnimatorListenerAdapter() { 
     @Override 
     public void onAnimationEnd(Animator animation) { 
      Log.d("Anim1", "onAnimationEnd: Anim1 have ended"); 
     } 

     @Override 
     public void onAnimationCancel(Animator animation) { 
      Log.d("Anim1", "onAnimationCancel: Anim1 have been canceled."); 
     } 
    }); 

    ViewWeightAnimationWrapper animationWrapper2 = new ViewWeightAnimationWrapper(mLlCurrentlyOpened); 
    ObjectAnimator anim2 = ObjectAnimator.ofFloat(animationWrapper2, 
      "weight", 
      animationWrapper2.getWeight(), 
      0f); 
    anim2.setDuration(1000); 

    anim2.addListener(new AnimatorListenerAdapter() { 
     @Override 
     public void onAnimationEnd(Animator animation) { 
      Log.d("Anim2", "onAnimationEnd: Anim2 have ended"); 
      mLlCurrentlyOpened = llToExpand; 
     } 

     @Override 
     public void onAnimationCancel(Animator animation) { 
      Log.d("Anim2", "onAnimationCancel: Anim2 have been canceled."); 
     } 
    }); 
    anim.start(); 
    anim2.start(); 
} 

Hier ist der Code meiner ViewWeightAnimationWrapper:

public class ViewWeightAnimationWrapper { 
private View view; 

public ViewWeightAnimationWrapper(View view) { 
    if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) { 
     this.view = view; 
    } else { 
     throw new IllegalArgumentException("The view should be a LinearLayout"); 
    } 
} 

public void setWeight(float weight) { 
    Log.i(String.valueOf(view.getId()), "setWeight: " + weight); 
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
    params.weight = weight; 
    view.requestLayout(); 
} 

public float getWeight() { 
    return ((LinearLayout.LayoutParams) view.getLayoutParams()).weight; 
} 

ist hier das Protokoll, das ich habe, wenn die Animation geht nicht auf das Ende:

I/2131755212: setWeight: 0.5 
I/2131755207: setWeight: 0.5 
I/2131755212: setWeight: 0.5251222 
I/2131755207: setWeight: 0.47487777 
I/2131755212: setWeight: 0.55174345 
I/2131755207: setWeight: 0.44825655 
D/Anim1: onAnimationCancel: Anim1 have been canceled. 
D/Anim1: onAnimationEnd: Anim1 have ended 
D/Anim2: onAnimationCancel: Anim2 have been canceled. 
D/Anim2: onAnimationEnd: Anim2 have ended 

Ich habe keine Ahnung, warum meine Animation nicht abgebrochen werden jedes Mal.

Wie kann ich wissen, was meine Animationen abbricht? Ist es möglich, die Animationen zu beenden, wenn sie abgebrochen werden?

Antwort

0

Ich habe nicht den Grund gefunden, warum die Animation abgebrochen wird. In der Dokumentation heißt es, dass die Animation abgebrochen wird, wenn dort dieselbe Animation für dasselbe Objekt ausgelöst wird, was nicht mein Fall war.

Allerdings habe ich eine Problemumgehung here gefunden, die mir das erwartete Ergebnis gibt.

Ich habe den ObjectAnimator durch eine benutzerdefinierte Animation ersetzt:

 /** 
    * Launch the expand animation on the LinearLayout in parameter and launch the folding animation 
    * on the currently opened LinearLayout. 
    * @param llToExpand LinearLayout that we want to expand. 
    */ 
    private void launchAnimation(final LinearLayout llToExpand) { 

     // Create the and assign the animations 
     ExpandAnimation expand = new ExpandAnimation(llToExpand, 0, 1); 
     expand.setDuration(500); 
     ExpandAnimation fold = new ExpandAnimation(mLlCurrentlyOpened, 1, 0); 
     fold.setDuration(500); 

     // Start the animations 
     llToExpand.startAnimation(expand); 
     mLlCurrentlyOpened.startAnimation(fold); 

     // Switch the currently opened LinearLayout for the next time 
     mLlCurrentlyOpened = llToExpand; 
    } 

Hier meine ExpandAnimation ist:

public class ExpandAnimation extends Animation { 

private float mStartWeight; 
private final float mEndWeight; 
private final LinearLayout mContent; 

public ExpandAnimation(LinearLayout content, float startWeight, 
       float endWeight) { 
    mStartWeight = startWeight; 
    mEndWeight = endWeight; 
    mContent = content; 
} 

@Override 
protected void applyTransformation(float interpolatedTime, 
            Transformation t) { 
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContent 
      .getLayoutParams(); 
    mStartWeight = lp.weight; 
    lp.weight = (mStartWeight + ((mEndWeight - mStartWeight) * interpolatedTime)); 
    mContent.setLayoutParams(lp); 
} 

@Override 
public boolean willChangeBounds() { 
    return true; 
} 
} 
Verwandte Themen