简单展开收起某个隐藏布局
2016-02-16 15:15
459 查看
点击某个按钮,实现另一个布局隐藏和显示之间的切换。
布局文件代码:
这里说明一下,LinearLayout的marginBottom是必须的,它应该是可以避免的,但是我没有想到好的方法,为什么必须在下面代码就知道了。这个marginBottom值只在第一次时影响布局,后面就不会再产生影响了,如果初始是隐藏状态,这个几乎不会有任何影响;如果初始是显示,那么就给他个负数的值。
很简单的activity代码:
imgClick和llShow是声明好的两个空间,在这里初始化,关键的内容是这个ExpandAnimation的内容,也就是一个自定义的动画类;
关键的ExpandAnimation类代码:
/**
* This animation class is animating the expanding and reducing the size of a view.
* The animation toggles between the Expand and Reduce, depending on the current state of the view
* @author Udinic
*
*/
public class ExpandAnimation extends Animation{
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;
private ImageView arrow;
/**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
public ExpandAnimation(View open, View view, int duration) {
setDuration(duration);
arrow = (ImageView) open;
mAnimatedView = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();
// if the bottom margin is 0,
// then after the animation will end it'll be negative, and invisible.
mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);
//根据状态获取动画起始参数
mMarginStart = mIsVisibleAfter ? 0:(0- view.getHeight());
mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);
arrow.setImageResource(R.mipmap.ic_close);
mAnimatedView.setVisibility(View.VISIBLE);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.bottomMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();
if (mIsVisibleAfter) {
mAnimatedView.setVisibility(View.GONE);
arrow.setImageResource(R.mipmap.ic_open);
}
mWasEndedAlready = true;
}
}
}
基本就是这样了,用一个动画类来实现隐藏显示,特别简单呐,不用理解也能够用。
这个使用时数据已有的情况,如果是展开后加载数据,这个不适用了,也许改改就行了,也许不行。具体看需求,完事收工!
布局文件代码:
<ImageView android:id="@+id/img_click" android:layout_gravity="center_horizontal" android:layout_width="80dp" android:layout_height="wrap_content" android:src="@mipmap/ic_open"/> <LinearLayout android:id="@+id/ll_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:visibility="gone" android:layout_marginBottom="-1dp" android:background="#cccccc"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我就是隐藏的内容" android:layout_marginTop="10dp"/> </LinearLayout>
这里说明一下,LinearLayout的marginBottom是必须的,它应该是可以避免的,但是我没有想到好的方法,为什么必须在下面代码就知道了。这个marginBottom值只在第一次时影响布局,后面就不会再产生影响了,如果初始是隐藏状态,这个几乎不会有任何影响;如果初始是显示,那么就给他个负数的值。
很简单的activity代码:
imgClick = (ImageView) findViewById(R.id.img_click); llShow = (LinearLayout) findViewById(R.id.ll_show); imgClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //第一个view参数是点击后修改的对象控件,第二个view参数是需要隐藏显示的布局控件,第三个参数是动画持续时间,单位毫秒 ExpandAnimation animation = new ExpandAnimation(imgClick,llShow,500); llShow.startAnimation(animation); } });
imgClick和llShow是声明好的两个空间,在这里初始化,关键的内容是这个ExpandAnimation的内容,也就是一个自定义的动画类;
关键的ExpandAnimation类代码:
/**
* This animation class is animating the expanding and reducing the size of a view.
* The animation toggles between the Expand and Reduce, depending on the current state of the view
* @author Udinic
*
*/
public class ExpandAnimation extends Animation{
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;
private ImageView arrow;
/**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
public ExpandAnimation(View open, View view, int duration) {
setDuration(duration);
arrow = (ImageView) open;
mAnimatedView = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();
// if the bottom margin is 0,
// then after the animation will end it'll be negative, and invisible.
mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);
//根据状态获取动画起始参数
mMarginStart = mIsVisibleAfter ? 0:(0- view.getHeight());
mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);
arrow.setImageResource(R.mipmap.ic_close);
mAnimatedView.setVisibility(View.VISIBLE);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.bottomMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();
if (mIsVisibleAfter) {
mAnimatedView.setVisibility(View.GONE);
arrow.setImageResource(R.mipmap.ic_open);
}
mWasEndedAlready = true;
}
}
}
基本就是这样了,用一个动画类来实现隐藏显示,特别简单呐,不用理解也能够用。
这个使用时数据已有的情况,如果是展开后加载数据,这个不适用了,也许改改就行了,也许不行。具体看需求,完事收工!
相关文章推荐
- 动态下载系统提供的多种中文字体
- POJ-3259
- win7禁止安装
- 设计模式-享元
- MyBatis日期用法技巧
- MyCat - 使用篇(4)
- 修改Ubuntu侧边栏图标提示文字
- 2016蓝桥杯算法训练——区间k大数查询
- Android_自定义HorizontalScrollView实现滑动子控件大小伸缩
- iOS 编码规范
- 能被2、3、4、5、6、7、8、9 等数整除的数的特征
- LeetCode "Increasing Triplet Subsequence" !!
- 基于 Python 和 Scikit-Learn 的机器学习介绍
- https生成证书及TOMCAT配置
- Android Studio 常用快捷键
- Java-强引用、软引用、弱引用、虚引用详解
- java web点击链接用js判断用户是否已经登录 (js获取session及判断是否为空)
- 深入了解MyBatis返回值
- UVA 10816 - Travel in Desert(最小生成树+最短路)
- iOS: 使用故事板和xib设置按钮圆角方法