2012-04-27 5 views
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; 

} 

}

ответ

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

У меня такой же результат – Edgar

0

просто добавьте атрибут в верхнюю часть макета. У него есть анимация по умолчанию, например «угасание».

android:animateLayoutChanges="true" 

Затем установите видимость GONE/VISIBLE на складной макет.

Смежные вопросы