2012-04-27 66 views
2

我得到了這個問題,我有一個類似於活動的手風琴,其按鈕是根據其狀態展開或摺疊佈局的,然後問題出現在動畫中,展開動畫只是完美的,但是合攏一個合攏它通過父按鈕上方的佈局時,我想摺疊它相對於佈局。在android佈局上合攏動畫

對不起FO我的英語不好,下面的代碼:

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; 

} 

}

回答

0

你爲什麼不只是使用ReverseInterpolator扭轉擴大其動畫作品已經如此完美?

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

在您_set變量:

_set.setInterpolator(new ReverseInterpolator()); 

OR

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 
+0

我得到了同樣的結果 – Edgar 2012-04-27 16:41:06

0

只需將一個屬性添加到最頂端的佈局即可。它有一個像「淡出」的默認動畫。

android:animateLayoutChanges="true" 

然後爲可摺疊佈局設置可見性GONE/VISIBLE。