2012-10-26 11 views
11

Odtworzyłem mój problem w bardzo prosty sposób. Mam 3 widok tekstowy. 2 z nich są w osobnym LinearLayout, trzeci jest na tym samym poziomie co LinearLayout. Przełączam widoczność test1 i test2 i chciałbym, aby zniknęły (co działa). Ponadto chcę, aby test3 wsunął się w swoje nowe miejsce (miejsce: test1 i test2). Nie mogę tego zrobić. test3 po prostu przyciąga do nowego punktu.LayoutTransition: Animate Zobacz obok rozwijającego się widoku

Jak mogę to osiągnąć?

Mój kod:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parent" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:animateLayoutChanges="true" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Click" /> 

    <LinearLayout android:animateLayoutChanges="true" 
     android:id="@+id/child1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" android:visibility="gone" 
      android:layout_height="wrap_content" 
      android:text="TEST1" /> 

     <TextView 
      android:id="@+id/test2" 
      android:layout_width="match_parent" android:visibility="gone" 
      android:layout_height="wrap_content" 
      android:text="TEST2" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/test3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="TEST3" /> 

</LinearLayout> 

A w mojej działalności:

public class LayoutAnimations extends Activity { 
    private boolean toggle = true; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_animations); 

     Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (toggle) { 
        findViewById(R.id.test1).setVisibility(View.VISIBLE); 
        findViewById(R.id.test2).setVisibility(View.VISIBLE); 
       } else { 
        findViewById(R.id.test1).setVisibility(View.GONE); 
        findViewById(R.id.test2).setVisibility(View.GONE); 
       } 
       toggle = !toggle; 
      } 
     }); 

    } 

} 

Edit: I rzeczywiście mają inny TextView obok test1 i test2 który powinien być widoczny przez cały czas, więc ja nie można po prostu ukryć samego siebie.

Odpowiedz

6

mam rozwiązać go za pomocą innego pytanie. . See this answer

Cytując:

Wierzę, najprostszym rozwiązaniem jest rozszerzenie Animation klasę i zastąpić applyTransformation() zmienić wysokość widoku w następujący sposób:

import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.view.animation.Animation; 
import android.view.animation.Transformation; 
import android.widget.LinearLayout; 

public class MyCustomAnimation extends Animation { 

    public final static int COLLAPSE = 1; 
    public final static int EXPAND = 0; 

    private View mView; 
    private int mEndHeight; 
    private int mType; 
    private LinearLayout.LayoutParams mLayoutParams; 

    public MyCustomAnimation(View view, int duration, int type) { 

     setDuration(duration); 
     mView = view; 
     mEndHeight = mView.getHeight(); 
     mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams()); 
     mType = type; 
     if(mType == EXPAND) { 
      mLayoutParams.height = 0; 
     } else { 
      mLayoutParams.height = LayoutParams.WRAP_CONTENT; 
     } 
     view.setVisibility(View.VISIBLE); 
    } 

    public int getHeight(){ 
     return mView.getHeight(); 
    } 

    public void setHeight(int height){ 
     mEndHeight = height; 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 

     super.applyTransformation(interpolatedTime, t); 
     if (interpolatedTime < 1.0f) { 
      if(mType == EXPAND) { 
       mLayoutParams.height = (int)(mEndHeight * interpolatedTime); 
      } else { 
       mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime)); 
      } 
      mView.requestLayout(); 
     } else { 
      if(mType == EXPAND) { 
       mLayoutParams.height = LayoutParams.WRAP_CONTENT; 
       mView.requestLayout(); 
      }else{ 
       mView.setVisibility(View.GONE); 
      } 
     } 
    } 
} 

Aby go użyć, ustaw onclick() następująco:

int height; 

@Override 
public void onClick(View v) { 
    if(view2.getVisibility() == View.VISIBLE){ 
     MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyAnimation.COLLAPSE); 
     height = a.getHeight(); 
     view2.startAnimation(a); 
    }else{ 
     MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyAnimation.EXPAND); 
     a.setHeight(height); 
     view2.startAnimation(a); 
    } 
} 

Pozdrawiam.

1

stosowanie tej animacji XML slide_in_left.XML

<translate 
    android:duration="500" 
    android:fromXDelta="-100%" 
    android:fromYDelta="0%" 
    android:toXDelta="0%" 
    android:toYDelta="0%" /> 
</set> 

i slid_in_right.XML

<translate 
    android:duration="500" 
    android:fromXDelta="100%" 
    android:fromYDelta="0%" 
    android:toXDelta="0%" 
    android:toYDelta="0%" /> 
</set> 

stosowanie tej animacji Animacja slideinleft, slideinright;

i initlize Ta animacja

public void AnimationInitialization() { 

    slideinleft= AnimationUtils 
      .loadAnimation(this, R.anim.slide_in_left); 


    slideinright= AnimationUtils.loadAnimation(this, 
      R.anim.slide_in_right); 

} 

i wywołać tę funkcję do zmiany 2 visibilty

public void showMenu() { 

    linearlayout_first.clearAnimation(); 

    linearlayout_first.startAnimation(slideinleft); 

    linearlayout_first.setVisibility(View.VISIBLE); 

} 

public void hideMenu() { 

    linearlayout_second.clearAnimation(); 
    linearlayout_second.startAnimation(slideinright); 


    linearlayout_second.setVisibility(View.GONE); 
} 

można zmienić układ jak potrzebujesz. także zmiana w Xdelta i Ydelta animacji XML ..

+0

Animacja w widoku znikającym nie działa bez AnimationListener. W tym celu należy dołączyć AnimationListener i określić SetVisibility (View.GONE) w onAnimationEnd()! – gauravsapiens

+0

Pracuję z tym kodem ... i to dla mnie w porządku .. –

+0

Odkrywa widok, a następnie animuje. Z mojego doświadczenia wynikało to, że wyglądało to naprawdę szkicowo. – Michael

0

Dokumentacja jest nieco myląca. Dla android:animateLayoutChanges, mówi

... Gdy ta flaga jest ustawiona na wartość true, obiekt domyślny LayoutTransition zostanie ustawiony na pojemniku i domyślnych animacji ViewGroup będzie działać, gdy zmiany te następują układ. dokumentacja setLayoutTransition metody klasy ViewGroup

natomiast mówi:

Domyślnie obiekt przejściowy jest null (zmiany więc układ nie są animowane).

Powinieneś spróbować ustawić LayoutTransition na układzie.

Oto przykład, który to robi .. http://www.java2s.com/Code/Android/UI/UseLayoutTransitiontoautomatetransitionanimationsasitemsarehiddenorshowninacontainer.htm

+0

Próbowałem tego na początku, moja druga próba była w rzeczywistości przeniesieniem go do xml . – nhaarman

3

chciałem to samo wydarzy się w moim app. Aby to osiągnąć:

  1. Utwórz odpowiednią animację w Res/anim. Użyłem slajdu z lewej animacji. Możesz google inny, jeśli nie jesteś zadowolony z tego.

    slide_out_left.xml

    <translate 
        android:duration="@android:integer/config_mediumAnimTime" 
        android:fromXDelta="0" 
        android:toXDelta="-50%p" /> 
    
    <alpha 
        android:duration="@android:integer/config_mediumAnimTime" 
        android:fromAlpha="1.0" 
        android:toAlpha="0.0" /> 
    

  2. Attach animację określony powyżej z widokiem chcesz animować, (w przypadku child1 zawierającej text1 i Text2)

    Animation outAnimation; 
    LinearLayout a1 = (LinearLayout)findViewById(R.id.child1); 
    
    outAnimation = (Animation) AnimationUtils.loadAnimation (getApplicationContext(), R.anim.slide_out_left); 
    
    a1.setAnimation(outAnimation); 
    outAnimation.setAnimationListener(new AnimationListener() { 
    
        @Override 
        public void onAnimationEnd(Animation arg0) { 
        a1.setVisibility(View.GONE);        
        } 
        @Override 
        public void onAnimationRepeat(Animation animation) { 
         // TODO Auto-generated method stub       
        } 
    
        @Override 
        public void onAnimationStart(Animation animation) { 
         // TODO Auto-generated method stub 
        }     
    }); 
    a1.startAnimation(outAnimation); 
    

UWAGA: Mam załączeniu animację z child1 zamiast Text3 bo gdy będzie child1 wysunąć powoli będzie automatycznie dawać złudzenie, że text3 jest przesuwane w