android之类似卫星菜单,来自定义ViewGroup。。。。。

这是一个继承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





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值