Android笔记之自定义微信菜单栏实现

第一步:

建一个资源文件,实现可配置的卫星菜单栏的半径

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <attr name="radius" format="dimension" />
    <declare-styleable name="ArcMenu">
        <attr name="radius" />
    </declare-styleable>

</resources>

第二步:

在xml文件中添加

  <com.example.smartwhiteboard.UI.SelfDefinedUI.SelfDefinedWXMenu
                android:layout_width="600px"
                android:layout_marginLeft="50px"
                android:layout_height="300px"
                selfWX:radius="300px">
                <ImageView
                    android:background="@drawable/addgreen"
                    android:layout_width="80px"
                    android:layout_height="80px" />
                <ImageView
                    android:background="@drawable/light"
                    android:layout_width="80px"
                    android:layout_height="80px"/>
                <ImageView
                    android:background="@drawable/curtain"
                    android:layout_width="80px"
                    android:layout_height="80px"/>
                <ImageView
                    android:background="@drawable/air_condition"
                    android:layout_width="80px"
                    android:layout_height="80px"/>
                <ImageView
                    android:background="@drawable/environment"
                    android:layout_width="80px"
                    android:layout_height="80px"/>

            </com.example.smartwhiteboard.UI.SelfDefinedUI.SelfDefinedWXMenu>

其中 在整个布局文件开头添加 xmlns:selfWX="http://schemas.android.com/apk/res-auto"

第三步:

自定义控件的实现

public class SelfDefinedWXMenu extends ViewGroup implements View.OnClickListener {
    private int mRadius;
    private Context context;
    private View mCButton;
    public SelfDefinedWXMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.ArcMenu,0 , 0);
        mRadius = (int) a.getDimension(R.styleable.ArcMenu_radius, 0);//第二个参数是如果第一个参数为null,则用第二个参数值
        Log.i("mradius=",mRadius+"");




    }


    @Override
    protected void onFinishInflate() {//加载布局的时候调用
        super.onFinishInflate();
        init();
    }


    private void init() {
        for(int i=1;i<getChildCount();i++)
        {
            getChildAt(i).setVisibility(View.GONE);
        }
    }




    public SelfDefinedWXMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);




    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        for (int i = 0; i < count; i++)
        {
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
        }


        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        LayoutMainIcon();
        Double a=0.00;
        for(int j=0;j<getChildCount()-1;j++)
        {
            View view=getChildAt(j+1);
            int left=50+Math.abs((int)(mRadius*Math.cos(a)))+mCButton.getMeasuredWidth()/2-view.getMeasuredWidth()/2;
            int top=getHeight()-Math.abs((int)(mRadius*Math.sin(a)))-view.getMeasuredHeight();
            view.layout(left,top,left+view.getMeasuredWidth(),top+view.getMeasuredHeight());
            a+=22.5;
        }
    }


    private void LayoutMainIcon() {
        mCButton = getChildAt(0);
        int width = mCButton.getMeasuredWidth();
        int height = mCButton.getMeasuredHeight();
        mCButton.layout(50, getMeasuredHeight()-height, 50+width, getMeasuredHeight());
        mCButton.setOnClickListener(this);
    }


    @Override
    public void onClick(final View view) {


        RotateAnimation animation=new RotateAnimation(0.0f,360.0f,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(1000);
        mCButton.startAnimation(animation);
        if(getChildAt(1).getVisibility()==View.VISIBLE) {
            Double a = 0.00;
            for (int i = 1; i < getChildCount(); i++) {
                AnimationSet animset = new AnimationSet(true);
                final View weixing = getChildAt(i);
                int left = Math.abs((int) (mRadius * Math.cos(a))) + mCButton.getMeasuredWidth() / 2 - weixing.getMeasuredWidth() / 2;
                int top = getHeight() - Math.abs((int) (mRadius * Math.sin(a))) - weixing.getMeasuredHeight();
                TranslateAnimation translateAnimation = new TranslateAnimation(0, -left, 0, getHeight() - top - weixing.getMeasuredHeight());
                translateAnimation.setDuration(200);
                translateAnimation.setStartOffset(500-100*(i-1));//动画开始后延迟
                translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {


                    }


                    @Override
                    public void onAnimationEnd(Animation animation) {
                        weixing.setVisibility(View.GONE);
                    }


                    @Override
                    public void onAnimationRepeat(Animation animation) {


                    }
                });


                a += 22.5;


                RotateAnimation weixinanimation=new RotateAnimation(0.0f,720.0f,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
                weixinanimation.setDuration(700);
                //weixinanimation.setStartOffset(500-100*(i-1));
                animset.addAnimation(weixinanimation);
                animset.addAnimation(translateAnimation);


                weixing.startAnimation(animset);


            }
        }
        else
        {
            Double a = 0.00;
            for (int i = 1; i < getChildCount(); i++) {
                AnimationSet animset = new AnimationSet(true);
                final View weixing = getChildAt(i);
                int left = Math.abs((int) (mRadius * Math.cos(a))) + mCButton.getMeasuredWidth() / 2 - weixing.getMeasuredWidth() / 2;
                int top = getHeight() - Math.abs((int) (mRadius * Math.sin(a))) - weixing.getMeasuredHeight();
                TranslateAnimation translateAnimation = new TranslateAnimation(-left,0,getHeight() - top - weixing.getMeasuredHeight(),0);
                translateAnimation.setDuration(200);
                translateAnimation.setStartOffset(500-100*(i-1));


                translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        weixing.setVisibility(View.VISIBLE);
                    }


                    @Override
                    public void onAnimationEnd(Animation animation) {
                        weixing.setVisibility(View.VISIBLE);
                    }


                    @Override
                    public void onAnimationRepeat(Animation animation) {


                    }
                });
                RotateAnimation weixinanimation=new RotateAnimation(0.0f,720.0f,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
                weixinanimation.setDuration(700);
                //weixinanimation.setStartOffset(500-100*(i-1));
                animset.addAnimation(weixinanimation);
                animset.addAnimation(translateAnimation);


                weixing.startAnimation(animset);
                a += 22.5;
                final int finalI = i;
                weixing.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ClickAnimation(view, finalI);
                    }
                });
            }
        }
    }


    private void ClickAnimation(View view,int pos) {
        for(int i=1;i<getChildCount();i++)
        {
            if(i==pos)
            {
                AnimationSet animationSet=new AnimationSet(true);
                ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                        0.5f);
                AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.0f);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(alphaAnim);
                animationSet.setDuration(500);
                view.startAnimation(animationSet);
                view.setVisibility(View.GONE);
            }
            else
            {
                View other=getChildAt(i);
                AnimationSet animationSet=new AnimationSet(true);
                ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                        0.5f);
                AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.0f);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(alphaAnim);
                animationSet.setDuration(500);
                other.startAnimation(animationSet);
                other.setVisibility(View.GONE);
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值