有时候需要点击一个view可以动画展开和收缩折叠一个View这样的效果,这样就可以直接自定义View来实现。
本例中,采用继承FrameLayout来实现自定义的ExpandView。下面将详细介绍各个部分来实现该类以及如何使用该自定义视图。
效果图如下:
未展开效果:
正在向上折叠收缩中的效果:
已经展开效果:
自定义展开类:ExpandView的实现:
packagecom.czm.customview;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.animation.Animation;importandroid.view.animation.AnimationUtils;importandroid.widget.FrameLayout;importandroid.widget.LinearLayout;public class ExpandView extendsFrameLayout{privateAnimation mExpandAnimation;privateAnimation mCollapseAnimation;private booleanmIsExpand;publicExpandView(Context context) {this(context,null);//TODO Auto-generated constructor stub
}publicExpandView(Context context, AttributeSet attrs) {this(context, attrs,0);//TODO Auto-generated constructor stub
}public ExpandView(Context context, AttributeSet attrs, intdefStyle) {super(context, attrs, defStyle);//TODO Auto-generated constructor stub
initExpandView();
}private voidinitExpandView() {
LayoutInflater.from(getContext()).inflate(R.layout.layout_expand,this, true);
mExpandAnimation=AnimationUtils.loadAnimation(getContext(), R.anim.expand);
mExpandAnimation.setAnimationListener(newAnimation.AnimationListener() {
@Overridepublic voidonAnimationStart(Animation animation) {//TODO Auto