星型导航菜单

实现效果:

主要代码:
//布局
if (changed) {
            View cButton = getChildAt(getChildCount()-1);
            int ll = 0;
            int lt = 0;
            int width = cButton.getMeasuredWidth();
            int height = cButton.getMeasuredHeight();
            ll = getMeasuredWidth() / 2 - width;
            lt = getMeasuredHeight() / 2 - height;
            cButton.layout(ll, lt, ll + width, lt + height);
            cButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    two();
                }
            });
            //
            int rrr = 200;
            int count = getChildCount();
            for (int i = 0; i < count - 1; i++) {
                final View child = getChildAt(i);
                child.setVisibility(View.GONE);
                int cl = (int) (ll + rrr
                        * (Math.cos((i) * (2 * Math.PI) / (count - 1))));
                int ct = (int) (lt + rrr
                        * (Math.sin((i) * (2 * Math.PI) / (count - 1))));
                int cWidth = child.getMeasuredWidth();
                int cHeight = child.getMeasuredHeight();
                // ct = getMeasuredHeight() - height * (count - i);
                // getMeasuredHeight()获取整个View的高度
                child.layout(cl, ct, cl + cWidth, ct + cHeight);
                final int abc = i;
                child.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getContext(), "item=" + abc,
                                Toast.LENGTH_LONG).show();
                    }
                });
            }


//切换动画
int count = getChildCount();
        for (int i = 0; i < count - 1; i++) {
            View child = getChildAt(i + 0);
            int rrr = 200;
            int cl = (int) (rrr * (Math.cos((i) * (2 * Math.PI) / (count - 1))));
            int ct = (int) (rrr * (Math.sin((i) * (2 * Math.PI) / (count - 1))));
            if (isopen) {
                TranslateAnimation ta = new TranslateAnimation(0, -cl, 0, -ct);
                ta.setDuration(1000 + i * 100);
                child.startAnimation(ta);
                child.setVisibility(View.GONE);
            } else {
                TranslateAnimation ta = new TranslateAnimation(-cl, 0, -ct, 0);
                ta.setDuration(1000 + i * 100);
                child.startAnimation(ta);
                child.setVisibility(View.VISIBLE);
            }
        }
        isopen = isopen == false ? true : false;

备注:自定义控件继承ViewGroup,事件响应+属性动画

你可能感兴趣的:(星型导航菜单)