android 自定义控件----优酷菜单

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.youkumenu.MainActivity">

    <RelativeLayout
        android:id="@+id/leve3"
        android:background="@drawable/level3"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_width="280dp"
        android:layout_height="140dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel1"
            android:id="@+id/channel1"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="8dp"
            android:layout_marginBottom="8dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel2"
            android:id="@+id/channel2"
            android:layout_above="@id/channel1"
            android:layout_marginLeft="32dp"
            android:layout_marginBottom="8dp"/>

        <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/channel3"
        android:id="@+id/channel3"
        android:layout_above="@id/channel2"
        android:layout_marginLeft="60dp"
        android:layout_marginBottom="8dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel4"
            android:id="@+id/channel4"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel7"
            android:id="@+id/channel7"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel6"
            android:id="@+id/channel6"
            android:layout_above="@id/channel7"
            android:layout_marginRight="32dp"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="8dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel5"
            android:id="@+id/channel5"
            android:layout_above="@id/channel6"
            android:layout_marginRight="60dp"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="8dp"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/leve2"
        android:background="@drawable/level2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_width="180dp"
        android:layout_height="90dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_search"
            android:layout_marginBottom="8dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="8dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_menu"
            android:id="@+id/icon_menu"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_myyouku"
            android:layout_alignParentRight="true"
            android:layout_marginRight="8dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="8dp"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/leve1"
        android:background="@drawable/level1"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_width="100dp"
        android:layout_height="50dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_home"
            android:id="@+id/icon_home"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="8dp"
            />
    </RelativeLayout>
</RelativeLayout>

public class MainActivity extends Activity {

    private RelativeLayout leve1;
    private RelativeLayout leve2;
    private RelativeLayout leve3;
    private ImageView icon_home;
    private ImageView icon_menu;


    //true为显示,false为隐藏
    private boolean isShowleve1=true;
    private boolean isShowleve2=true;
    private boolean isShowleve3=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyOnClickListener myOnClickListener = new MyOnClickListener();
        leve1=(RelativeLayout)findViewById(R.id.leve1);
        leve2=(RelativeLayout)findViewById(R.id.leve2);
        leve3=(RelativeLayout)findViewById(R.id.leve3);
        icon_home=(ImageView)findViewById(R.id.icon_home);
        icon_menu=(ImageView)findViewById(R.id.icon_menu);

        icon_home.setOnClickListener(myOnClickListener);
        icon_menu.setOnClickListener(myOnClickListener);
        leve1.setOnClickListener(myOnClickListener);
        leve2.setOnClickListener(myOnClickListener);
        leve3.setOnClickListener(myOnClickListener);


    }

    class MyOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.icon_home:
                    if(isShowleve2 && isShowleve3){
                        //隐藏二级菜单,三级菜单
                        Tools.hideView(leve2);
                        Tools.hideView(leve3,200);
                        isShowleve2=false;
                        isShowleve3=false;
//                        if(isShowleve3) {
//                            isShowleve3=false;
//                            Tools.hideView(leve3);
//                        }

                    }else{
                        //显示二级菜单
                        Tools.showView(leve2);
                        isShowleve2=true;
                    }
                    break;

                case R.id.icon_menu:
                    if(isShowleve3){
                        //隐藏三级菜单
                        Tools.hideView(leve3);
                        isShowleve3=false;
                    }else{
                        //显示三级菜单
                        Tools.showView(leve3);
                        isShowleve3=true;
                    }
                    break;

                case R.id.leve1:
                    Toast.makeText(MainActivity.this,"leve1",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.leve2:
                    Toast.makeText(MainActivity.this,"leve2",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.leve3:
                    Toast.makeText(MainActivity.this,"leve3",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
}

 

class Tools {
    public static void hideView(View view) {
        ObjectAnimator animator =ObjectAnimator.ofFloat(view,"rotation",0f,180f);
        animator.setDuration(500);
        animator.start();
        view.setPivotX(view.getWidth()/2);//不能写成setRotationX();
        view.setPivotY(view.getHeight());
//        RotateAnimation ra = new RotateAnimation(0,180,view.getWidth()/2,view.getHeight());
//        ra.setDuration(500);//设置动画播放持续的时间
//        ra.setFillAfter(true);//动画停留在播放完成的状态
//        view.startAnimation(ra);//启动动画
    }

    public static void showView(View view) {
        ObjectAnimator animator =ObjectAnimator.ofFloat(view,"rotation",180f,360f);
        animator.setDuration(500);
        animator.start();
        view.setPivotX(view.getWidth()/2);
        view.setPivotY(view.getHeight());
//        RotateAnimation ra = new RotateAnimation(180,360,view.getWidth()/2,view.getHeight());
//        ra.setDuration(500);//设置动画播放持续的时间
//        ra.setFillAfter(true);//动画停留在播放完成的状态
//        view.startAnimation(ra);//启动动画
    }

    public static void hideView(View view, int i) {
        ObjectAnimator animator =ObjectAnimator.ofFloat(view,"rotation",0f,180f);
        animator.setDuration(500);
        animator.setStartDelay(i);
        animator.start();
        view.setPivotX(view.getWidth()/2);
        view.setPivotY(view.getHeight());
//        RotateAnimation ra = new RotateAnimation(0,180,view.getWidth()/2,view.getHeight());
//        ra.setDuration(500);//设置动画播放持续的时间
//        ra.setFillAfter(true);//动画停留在播放完成的状态
//        ra.setStartOffset(i);//延迟动画
//        view.startAnimation(ra);//启动动画
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值