2012-04-11 40 views
6

Tôi đã thực hiện bố cục mở rộng hoạt ảnh với đoạn mã sau. Nó hoạt động tốt khi chúng ta gọi các hình ảnh động bằng cách nhấp vào một chế độ xem văn bản, nhưng điều tương tự cũng không xảy ra khi tôi cố gắng thực hiện điều đó bằng cách nhấp vào bố cục. Bất cứ ai có thể giúp tôi giải quyết điều này?Bố cục mở rộng hoạt ảnh

LinearLayout hello= (LinearLayout)findViewById(R.id.lin_hello); 
final RelativeLayout rel=(RelativeLayout)findViewById(R.id.rel_nah_hide); 
hello.setOnClickListener(new OnClickListener() {   
    @Override 
    public void onClick(View v) { 
     expand=!expand; 
     Animation a=expand(rel, expand); 
     rel.setAnimation(a); 
     a.start(); 
    } 
}); 

public static Animation expand(final View v, final boolean expand) { 
    try { 
     Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class); 
     m.setAccessible(true); 
     m.invoke(v, 
       MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
       MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST) 
     ); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    final int initialHeight = v.getMeasuredHeight(); 

    if (expand) { 
     v.getLayoutParams().height = 0; 
    } else { 
     v.getLayoutParams().height = initialHeight; 
    } 
    v.setVisibility(View.VISIBLE); 

    Animation a = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      int newHeight = 0; 
      if (expand) { 
       newHeight = (int) (initialHeight * interpolatedTime); 
      } else { 
       newHeight = (int) (initialHeight * (1 - interpolatedTime)); 
      } 
      v.getLayoutParams().height = newHeight; 
      v.requestLayout(); 

      if (interpolatedTime == 1 && !expand) 
       v.setVisibility(View.GONE); 
     } 

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

    a.setDuration(SPEED_ANIMATION_TRANSITION); 
    a.setAnimationListener(new AnimationListener(){ 
     @Override 
     public void onAnimationEnd(Animation arg0) { 
      animWorkingFlag=false; 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void onAnimationStart(Animation animation) { 
      animWorkingFlag=true; 
     } 
    }); 

    return a; 
} 

và sau đây là cách bố trí xml,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <LinearLayout 
     android:id="@+id/lin_hello" 
     android:layout_height="50dp" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 
     <TextView 
      android:id="@+id/hello" 
      android:layout_height="50dp" 
      android:layout_width="fill_parent" 
      android:text="Nah" 
      android:gravity="center" 
      android:background="#F03535"/> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/rel_nah_hide" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
     <TextView 
      android:id="@+id/txt_nah_hide1" 
      android:layout_height="50dp" 
      android:layout_width="fill_parent" 
      android:text="Incorrect Name/Address" 
      android:gravity="center" 
      android:textColor="@color/white"/> 
     <TextView 
      android:id="@+id/txt_nah_hide2" 
      android:layout_height="50dp" 
      android:layout_width="fill_parent" 
      android:text="Venue is closed" 
      android:gravity="center" 
      android:textColor="@color/white" 
      android:layout_below="@id/txt_nah_hide1"/> 
     <TextView 
      android:id="@+id/txt_nah_hide3" 
      android:layout_height="50dp" 
      android:layout_width="fill_parent" 
      android:text="Venue is duplicate" 
      android:gravity="center" 
      android:textColor="@color/white" 
      android:layout_below="@id/txt_nah_hide2"/> 
    </RelativeLayout> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="28dp" 
     android:text="heelo2222"/> 
</LinearLayout> 
+0

bạn cũng có thể thử với giải pháp của tôi. Một chút sạch hơn và với một chức năng đơn giản. http://stackoverflow.com/questions/19863409/android-views-expand-animation – iGio90

Trả lời

2

Sử dụng tiếp theo đang phần: ->

View hello= findViewById(R.id.lin_hello); 
final RelativeLayout rel=(RelativeLayout)findViewById(R.id.rel_nah_hide); 
hello.setOnClickListener(new OnClickListener() {   
    @Override 
    public void onClick(View v) { 
     expand=!expand; 
     Animation a=expand(rel, expand); 
     rel.setAnimation(a); 
     a.start(); 
    } 
}); 
Các vấn đề liên quan