这是一个继承ViewGroup的自定义控件,对于高手来说比较简单,但对于我们这样的小白,确实是一个不错的demo
定义继承ViewGroup的自定义View ,需要实现它的两个基本方法, onMeasure和onLayout,我们在onMeasure中测量控件的大小,在onLayout中设置控件所在的位置。>我们知道在onMeasure中有3中模式,在本例子中由于设置的是match_parent,所以就根据系统测量的size来设定,本例子的onMeasure比较简单,就是测量子控件
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
}
}
主要就是布局,不过本例子,布局也比较简单,先将主menubtn的位置放好,
menuBtn = getChildAt(0);
menuBtn.setOnClickListener(this);
int width = menuBtn.getMeasuredWidth();
int height = menuBtn.getMeasuredHeight();
ml = getMeasuredWidth() - width - OFFSET;
mt = getMeasuredHeight() - height - OFFSET * 3;
menuBtn.layout(ml, mt, getMeasuredWidth() - OFFSET, getMeasuredHeight() - OFFSET * 3);
在循环遍历子view放置子控件, 这个比较随意,可以根据自己的喜好,随意的放置子控件,在这里我用的是一条直线,
for (int i = 0; i < getChildCount() - 1; i++) {
View childView = getChildAt(i + 1);
childView.setVisibility(View.GONE);
int cWidth = childView.getMeasuredWidth();
int cHeight = childView.getMeasuredHeight();
int average = getMeasuredWidth() / getChildCount();
int ct = mt - average * (i + 1);
childView.layout(ml + OFFSET / 2, ct, ml + cWidth + OFFSET / 2, ct + cHeight);
}
最后就是设置 主btn的点击事件,以及动画的设置,这也是一个难点,不过,看你像要实现什么样的动画了,简单的动画,还是比较简单的。
private void toggleMenu(int duration) {
for (int i = 0; i < getChildCount() - 1; i++) {
final View childView = getChildAt(i + 1);
childView.setVisibility(View.VISIBLE);
int average = getMeasuredWidth() / getChildCount();
int ct = average * (i + 1);
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = null;
if (mStatus == Status.CLOSE) {// to open
translateAnimation = new TranslateAnimation(0,
0, ct, 0);
childView.setClickable(true);
childView.setFocusable(true);
} else {
translateAnimation = new TranslateAnimation(0,
0, 0, ct);
childView.setClickable(false);
childView.setFocusable(false);
}
translateAnimation.setDuration(duration);
translateAnimation.setFillAfter(true);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (mStatus == Status.CLOSE) {
childView.setVisibility(View.GONE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
RotateAnimation rotateAnimation=new RotateAnimation(0,720, Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(duration);
rotateAnimation.setFillAfter(true);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
childView.startAnimation(animationSet);
final int pos=i+1;
childView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null)
itemClickListener.onClick(childView, pos);
menuItemAnimation(pos - 1);
changeStatus();
}
});
}
//切换菜单状态
changeStatus();
}
最后不要忘了,切换按钮的状态。
本例子还有个就是监听事件的回调,我想这个大家都会把。还是比较简答的。
由于不会制作gif动画,就用下面的两张静态的凑合着吧,有会制作gif的简便方法吗?
求告诉
具体源码下载地址 : http://download.csdn.net/detail/qq_21840193/8977311 点击打开链接
Github下载地址 : https://github.com/sweethearting/ArcMenu