2

私はこの問題を抱えています。状態に応じてレイアウトを展開または折りたたむボタンを備えたアコーディオンのようなアクティビティがあります。問題はアニメーションにあります。展開アニメーションは完璧ですが、上に渡されたレイアウトを折りたたむときの折りたたみアニメーションです親ボタンのレイアウトに合わせて折りたたみたい。

下手な英語で申し訳ありませんが、コードは次のとおりです。

public class Animations extends Animation {

/**
 * Initializes expand collapse animation, has two types, collapse (1) and expand (0).
 * @param view The view to animate
 * @param duration
 * @param type The type of animation: 0 will expand from gone and 0 size to visible and layout size defined in xml. 
 * 1 will collapse view and set to gone
 */
public AnimationSet AnimationSet;

public Animations(String type) {
    if (type == "Expand")
        AnimationSet = ExpandAnimation();
    if (type == "Collapse")
        AnimationSet =  CollapseAnimation();    
}
public AnimationSet ExpandAnimation() {

    AnimationSet _set = new AnimationSet(true);

      Animation animation = new AlphaAnimation(0.0f, 1.0f);
      animation.setDuration(250);
      _set.addAnimation(animation);

      animation = new TranslateAnimation(
          Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
          Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
      );
      animation.setDuration(150);
      _set.addAnimation(animation);

      return _set;

}
public AnimationSet CollapseAnimation() {

    AnimationSet set = new AnimationSet(true);

      Animation animation = new AlphaAnimation(1.0f, 1.0f);
      animation.setDuration(250);
      set.addAnimation(animation);

      animation = new TranslateAnimation(
          Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
          Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f
      );
      animation.setDuration(250);
      set.addAnimation(animation);

      return set;

}

}

4

2 に答える 2

0

ReverseInterpolator を使用して、すでに完全に機能している展開されたアニメーションを反転させてみませんか?

public class ReverseInterpolator implements Interpolator {
    public float getInterpolation(float paramFloat) {
        return Math.abs(paramFloat -1f);
    }
}

あなたの_set変数で:

_set.setInterpolator(new ReverseInterpolator());

また

public AnimationSet CollapseAnimation() {
    AnimationSet set = someObj.ExpandAnimation();
    set.setInterpolator(new ReverseInterpolator());
    return set;
}

また、あなたが間違っていることをいくつか強調しましょう。

.equalsの代わりに文字列を比較するために使用します==。これは間違っています-> if (type == "Expand"). また、メソッドと変数に適切な名前を付けてください。

public AnimationSet AnimationSet; //poor
public AnimationSet expandAnim; //better
public AnimationSet ExpandAnimation() { //poor
public AnimationSet expand() { //better
于 2012-04-27T15:00:37.537 に答える