如何写一个点击view带动画的下滑展开显示隐藏内容的控件

来源:互联网 发布:知善恶树英文 编辑:程序博客网 时间:2024/05/17 22:30

原博文地址:http://blog.csdn.net/baidu_nod/article/details/38815269


原理是在onMeasure中得到隐藏内容的高度,点击这个view的时候对隐藏的view startAnimation,让它的高度从0增长到onMeasure得到的这个View的measureHeight


具体这样写:

[java] view plain copy
  1. public class ExpandableLayout extends LinearLayout {  
  2.   
  3.     private Context mContext;  
  4.     private LinearLayout mHandleView;  
  5.     private RelativeLayout mContentView;  
  6.     private ImageView mIconExpand;  
  7.     int mContentHeight = 0;  
  8.     int mTitleHeight = 0;  
  9.     private boolean isExpand;  
  10.     private Animation animationDown;  
  11.     private Animation animationUp;  
  12.   
  13.     public ExpandableLayout(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         this.mContext = context;  
  16.     }  
  17.   
  18.     @Override  
  19.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  20.         if (this.mContentHeight == 0) {  
  21.             this.mContentView.measure(widthMeasureSpec, 0);  
  22.             this.mContentHeight = this.mContentView.getMeasuredHeight();  
  23.         }  
  24.         if (this.mTitleHeight == 0) {  
  25.             this.mHandleView.measure(widthMeasureSpec, 0);  
  26.             this.mTitleHeight = this.mHandleView.getMeasuredHeight();  
  27.         }  
  28.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  29.     }  
  30.   
  31.     @Override  
  32.     protected void onFinishInflate() {  
  33.         super.onFinishInflate();  
  34.         this.mHandleView = (LinearLayout) this  
  35.                 .findViewById(R.id.collapse_value);  
  36.         this.mContentView = (RelativeLayout) this.findViewById(R.id.expand_value);  
  37.         this.mIconExpand = (ImageView) this.findViewById(R.id.icon_value);  
  38.   
  39.         this.mHandleView.setOnClickListener(new ExpandListener());  
  40.         this.mContentView.setOnClickListener(new ExpandListener());  
  41.         mContentView.setVisibility(View.GONE);  
  42.     }  
  43.   
  44.     private class ExpandListener implements View.OnClickListener {  
  45.         @Override  
  46.         public final void onClick(View paramView) {  
  47.             //clearAnimation是view的方法  
  48.             clearAnimation();  
  49.             if (!isExpand) {  
  50.                 if (animationDown == null) {  
  51.                     animationDown = new DropDownAnim(mContentView,  
  52.                             mContentHeight, true);  
  53.                     animationDown.setDuration(200); // SUPPRESS CHECKSTYLE  
  54.                 }  
  55.                 startAnimation(animationDown);  
  56.                 mContentView.startAnimation(AnimationUtils.loadAnimation(  
  57.                         mContext, R.anim.animalpha));  
  58.                 mIconExpand.setImageResource(R.drawable.update_detail_up);  
  59.                 isExpand = true;  
  60.             } else {  
  61.                 isExpand = false;  
  62.                 if (animationUp == null) {  
  63.                     animationUp = new DropDownAnim(mContentView,  
  64.                             mContentHeight, false);  
  65.                     animationUp.setDuration(200); // SUPPRESS CHECKSTYLE  
  66.                 }  
  67.                 startAnimation(animationUp);  
  68.                 mIconExpand.setImageResource(R.drawable.update_detail_down);  
  69.             }  
  70.         }  
  71.     }  
  72.   
  73.     class DropDownAnim extends Animation {  
  74.         /** 目标的高度 */  
  75.         private int targetHeight;  
  76.         /** 目标view */  
  77.         private View view;  
  78.         /** 是否向下展开 */  
  79.         private boolean down;  
  80.   
  81.         /** 
  82.          * 构造方法 
  83.          *  
  84.          * @param targetview 
  85.          *            需要被展现的view 
  86.          * @param vieweight 
  87.          *            目的高 
  88.          * @param isdown 
  89.          *            true:向下展开,false:收起 
  90.          */  
  91.         public DropDownAnim(View targetview, int vieweight, boolean isdown) {  
  92.             this.view = targetview;  
  93.             this.targetHeight = vieweight;  
  94.             this.down = isdown;  
  95.         }  
  96.         //down的时候,interpolatedTime从0增长到1,这样newHeight也从0增长到targetHeight  
  97.         @Override  
  98.         protected void applyTransformation(float interpolatedTime,  
  99.                 Transformation t) {  
  100.             int newHeight;  
  101.             if (down) {  
  102.                 newHeight = (int) (targetHeight * interpolatedTime);  
  103.             } else {  
  104.                 newHeight = (int) (targetHeight * (1 - interpolatedTime));  
  105.             }  
  106.             view.getLayoutParams().height = newHeight;  
  107.             view.requestLayout();  
  108.             if (view.getVisibility() == View.GONE) {  
  109.                 view.setVisibility(View.VISIBLE);  
  110.             }  
  111.         }  
  112.   
  113.         @Override  
  114.         public void initialize(int width, int height, int parentWidth,  
  115.                 int parentHeight) {  
  116.             super.initialize(width, height, parentWidth, parentHeight);  
  117.         }  
  118.   
  119.         @Override  
  120.         public boolean willChangeBounds() {  
  121.             return true;  
  122.         }  
  123.     }  
  124. }  

Layout:

[java] view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#FFFFFF" >  
  6.   
  7.     <com.example.view.ExpandableLayout  
  8.         android:id="@+id/app_detail_safety_info"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="vertical" >  
  12.   
  13.         <LinearLayout  
  14.             android:id="@+id/collapse_value"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="34dip"  
  17.             android:layout_marginLeft="14dip"  
  18.             android:gravity="center_vertical"  
  19.             android:orientation="horizontal" >  
  20.               
  21.             <TextView  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:layout_marginLeft="30dp"  
  25.                 android:text="点击我会显示隐藏的内容"  
  26.                 android:textColor="#000000"  
  27.                 android:textSize="18sp" >  
  28.             </TextView>  
  29.   
  30.             <LinearLayout  
  31.                 android:layout_width="fill_parent"  
  32.                 android:layout_height="fill_parent"  
  33.                 android:gravity="right|center_vertical"  
  34.                 android:orientation="horizontal" >  
  35.   
  36.                 <ImageView  
  37.                     android:id="@+id/icon_value"  
  38.                     android:layout_width="wrap_content"  
  39.                     android:layout_height="wrap_content"  
  40.                     android:layout_marginRight="22dip"  
  41.                     android:src="@drawable/update_detail_down" />  
  42.             </LinearLayout>  
  43.         </LinearLayout>  
  44.   
  45.         <RelativeLayout  
  46.             android:id="@+id/expand_value"  
  47.             android:layout_width="fill_parent"  
  48.             android:layout_height="wrap_content"  
  49.             android:background="@drawable/app_detail_safety_info_bg"  
  50.             android:orientation="horizontal"  
  51.             android:paddingBottom="8dip"  
  52.             android:paddingTop="8dip" >  
  53.   
  54.             <ImageButton  
  55.                 android:id="@+id/btn"  
  56.                 android:layout_width="wrap_content"  
  57.                 android:layout_height="wrap_content"  
  58.                 android:layout_centerVertical="true"  
  59.                 android:layout_marginLeft="30dp"  
  60.                 android:background="@null"  
  61.                 android:src="@drawable/btn_icon"  
  62.                 android:textColor="#0A84BD"  
  63.                 android:textSize="16sp" />  
  64.   
  65.             <TextView  
  66.                 android:layout_width="wrap_content"  
  67.                 android:layout_height="wrap_content"  
  68.                 android:layout_marginLeft="30dp"  
  69.                 android:layout_toRightOf="@id/btn"  
  70.                 android:text="隐藏的内容"  
  71.                 android:textColor="#000000"  
  72.                 android:textSize="18sp" >  
  73.             </TextView>  
  74.         </RelativeLayout>  
  75.     </com.example.view.ExpandableLayout>  
  76.   
  77. </RelativeLayout>  

只要在这个activity中setContentView这个layout,点击view就可以执行展开动画

代码:http://download.csdn.net/detail/baidu_nod/7812705

效果图:


0 0