一个可以展开和收缩的View,通过改变LayoutParams.height实现。
代码片段:
展开
public void expand() {
measure(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
final int height = getMeasuredHeight();
getLayoutParams().height = 0;
setVisibility(View.VISIBLE);
Animation expandAnima = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
getLayoutParams().height = (interpolatedTime == 1)
? android.view.ViewGroup.LayoutParams.WRAP_CONTENT
: (int)(height * interpolatedTime);
requestLayout();
}
};
expandAnima.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
startAnimation(expandAnima);
}
收缩
public void collapse() {
final int height = getMeasuredHeight();
Animation collapseAnima = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
if(interpolatedTime == 1) {
setVisibility(View.GONE);
} else {
getLayoutParams().height = height - (int)(height * interpolatedTime);
requestLayout();
}
}
};
collapseAnima.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
startAnimation(collapseAnima);
}
代码库:git@code.youkuaiyun.com:BoySP/spdroid.git 下的ExpandPanel项目