View展开隐藏动画
2016-07-02 14:11
246 查看
引用的布局
import com.slidingmenu.R; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.Transformation; import android.widget.ImageView; import android.widget.LinearLayout; public class ExpandableLayout extends LinearLayout { private Context mContext; private LinearLayout mHandleView; private LinearLayout mContentView; private ImageView mIconExpand; int mContentHeight = 0; int mTitleHeight = 0; private boolean isExpand; private Animation animationDown; private Animation animationUp; public ExpandableLayout(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (this.mContentHeight == 0) { this.mContentView.measure(widthMeasureSpec, 0); this.mContentHeight = this.mContentView.getMeasuredHeight(); } if (this.mTitleHeight == 0) { this.mHandleView.measure(widthMeasureSpec, 0); this.mTitleHeight = this.mHandleView.getMeasuredHeight(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onFinishInflate() { super.onFinishInflate(); this.mHandleView = (LinearLayout) this .findViewById(R.id.ll);//点击展开的父类布局 this.mContentView = (LinearLayout) this.findViewById(R.id.ll_menu);//要展开的布局 this.mIconExpand = (ImageView) this.findViewById(R.id.image);//图片 this.mHandleView.setOnClickListener(new ExpandListener());//展开、隐藏监听 //this.mContentView.setOnClickListener(new ExpandListener()); mContentView.setVisibility(View.GONE); } private class ExpandListener implements View.OnClickListener { @Override public final void onClick(View paramView) { //clearAnimation是view的方法 clearAnimation(); if (!isExpand) { if (animationDown == null) { animationDown = new DropDownAnim(mContentView, mContentHeight, true); animationDown.setDuration(200); // SUPPRESS CHECKSTYLE } startAnimation(animationDown); mContentView.startAnimation(AnimationUtils.loadAnimation( mContext, R.anim.animalpha)); mIconExpand.setImageResource(R.drawable.update_detail_up); isExpand = true; } else { isExpand = false; if (animationUp == null) { animationUp = new DropDownAnim(mContentView, mContentHeight, false); animationUp.setDuration(200); // SUPPRESS CHECKSTYLE } startAnimation(animationUp); mIconExpand.setImageResource(R.drawable.update_detail_down); } } } class DropDownAnim extends Animation { /** 目标的高度 */ private int targetHeight; /** 目标view */ private View view; /** 是否向下展开 */ private boolean down; /** * 构造方法 * * @param targetview * 需要被展现的view * @param vieweight * 目的高 * @param isdown * true:向下展开,false:收起 */ public DropDownAnim(View targetview, int vieweight, boolean isdown) { this.view = targetview; this.targetHeight = vieweight; this.down = isdown; } //down的时候,interpolatedTime从0增长到1,这样newHeight也从0增长到targetHeight @Override protected void applyTransformation(float interpolatedTime, Transformation t) { int newHeight; if (down) { newHeight = (int) (targetHeight * interpolatedTime); } else { newHeight = (int) (targetHeight * (1 - interpolatedTime)); } view.getLayoutParams().height = newHeight; view.requestLayout(); if (view.getVisibility() == View.GONE) { view.setVisibility(View.VISIBLE); } } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); } @Override public boolean willChangeBounds() { return true; } } }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:orientation="vertical" > <view.ExpandableLayout android:id="@+id/app_detail_safety_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/ll" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="19sp" android:text="展开隐藏布局" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" android:src="@drawable/update_detail_down" /> </LinearLayout> <LinearLayout android:id="@+id/ll_menu" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" android:visibility="gone" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="19sp" android:text="菜单1" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="19sp" android:text="菜单2" /> </LinearLayout> </view.ExpandableLayout> <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="19sp" android:text="布局1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="19sp" android:text="布局2" /> </LinearLayout>展开、隐藏动画文件R.anim.animalpha
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500"/>
最后显示隐藏布局的id不可重复监听,否则显示监听会无效
相关文章推荐
- android 布局
- Codevs 1029(遍历问题 )
- ubuntu 16.04 设置位wifi热点 方法(手机可链接)亲测可用
- java基础/Scanner类/String类
- 数据库中的分页问题
- HDU-2263 过山车(二分图最大匹配)
- redis配置详解
- Python3之PrettyTable模块
- codeforces_677D. Vanya and Treasure(BFS+DP)
- Java实现几种常见排序方法
- c#中的委托和c++中的bind/function对比
- 关闭eclipse或MyEclipse的单词检查
- Linux下安装nginx
- java.sql.SQLException: Field 'id' doesn't have a default value解决方案
- 提高项目18-二分法解方程
- leetcode_c++:哈希:Group Anagrams (049)
- js输出数据精确到小数点后n位代码
- VirtualBox虚拟机与主机互通,并且虚拟机又能上网配置
- 求逆元
- leetcode: Binary Tree Maximum Path Sum