自定义viewgroup的个人理解和参考网络的小demo

首先,说下哈 ,这是本人第一次写博客,说实话 ,自定义的view 也不太那个啥,今天参考了 邮电三精-大精wing 的 android自定义viewgroup初步之一----抽屉菜单 ,自己的一些改进和加深,什么都不说了 ,直接上代码 !!!


抽屉效果  :

当然首先应该就是新建DrawerMenu类继承自ViewGroup:
一,写好构造方法,
二,重新onMeasure()方法,主要是获取子view的个数,和ViewGroup的大小宽,高;
三,重写onLayout()方法,layButtom()方法主要是操作最先的那个view  点击它,操作其他的子view
四,toggleMenu() 是对开合状态下要做的一些操作。
代码都给了  ,详情,看代码哈!水平有限,得继续努力啊 !!

备注:有点点小问题我的自View显示少了一个,找不到原因,希望明白的大神 ,告诉我下啊,谢了先..

    private View mButton_bottom;
    private int mWidth_button_buttom, mHeight_buttom_buttom;
    private int mButtomX;
    private int mButtonY;
    private boolean isChanged = true;
    private int count;
    private View mFir_Button_bottom;
    private View mSec_Button_bottom;
    private View mThi_Button_bottom;
    private View mFor_Buttom_bottom;

    public DrawerMenu(Context context) {
        super(context, null);
    }

    public DrawerMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

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

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (isChanged) {
            layButtom();
            for (int i = 0; i < count - 1; i++) {
                View child = getChildAt(i+1);
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();
//                child.layout(0, mButtonY - mHeight_buttom_buttom * (i + 1) * 2, childWidth, getMeasuredHeight());
                child.layout(0, mButtonY - childHeight * (i + 1), childWidth, mButtonY - childHeight * i);
                child.setVisibility(GONE);
            }
        }
    }

    private void layButtom() {
        int i = 100;
        mButton_bottom = getChildAt(0);
        mButton_bottom.setOnClickListener(this);
        mFir_Button_bottom = getChildAt(1);
        mFir_Button_bottom.setOnClickListener(this);
        mSec_Button_bottom = getChildAt(2);
        mSec_Button_bottom.setOnClickListener(this);
        mThi_Button_bottom = getChildAt(3);
        mThi_Button_bottom.setOnClickListener(this);
        mFor_Buttom_bottom = getChildAt(4);
        mFir_Button_bottom.setOnClickListener(this);

        mWidth_button_buttom = mButton_bottom.getMeasuredWidth();
        mHeight_buttom_buttom = mButton_bottom.getMeasuredHeight();
        mButtomX = 0;
        mButtonY = getMeasuredHeight() - mHeight_buttom_buttom;
        mButton_bottom.layout(mButtomX, mButtonY, mWidth_button_buttom, getMeasuredHeight());
    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.iv_01:
               toggleMenu();
           break;
           case R.id.iv_02:
               Toast.makeText(getContext(),"第一个抽屉",Toast.LENGTH_LONG).show();
           break;
           case R.id.iv_03:
               Toast.makeText(getContext(),"第二个抽屉",Toast.LENGTH_LONG).show();
           break;
           case R.id.iv_04:
               Toast.makeText(getContext(),"第三个抽屉",Toast.LENGTH_LONG).show();
           break;
           case R.id.iv_05:
               Toast.makeText(getContext(),"第四个抽屉",Toast.LENGTH_LONG).show();
           break;
           default:
       }
    }

    private void toggleMenu() {
        if (isChanged) {
            for (int i = 0; i < count-1; i++) {
                View Achild = getChildAt(i + 1);
                TranslateAnimation anim = new TranslateAnimation(-Achild.getMeasuredWidth(), 0, 0, 0);
                anim.setDuration(500 + i * 300);
                Achild.startAnimation(anim);
                Achild.setVisibility(VISIBLE);
                isChanged = false;
            }
        }else{
            for (int i = 0; i < count-1; i++) {
                View Achild = getChildAt(i + 1);
                TranslateAnimation anim = new TranslateAnimation(0, -Achild.getMeasuredWidth(), 0, 0);
                anim.setDuration(500 + i * 300);
                Achild.startAnimation(anim);
                Achild.setVisibility(GONE);
                isChanged = true;
            }
            }
        }
 
 
activity_main.xml的内容如下:
 
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.company.kl.customviewgroup.DrawerMenu
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv_01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
        <ImageView
            android:id="@+id/iv_02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aa" />
        <ImageView
            android:id="@+id/iv_03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aa" />
        <ImageView
            android:id="@+id/iv_04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aa" />
        <ImageView
            android:id="@+id/iv_05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aa" />
    </com.company.kl.customviewgroup.DrawerMenu>
</RelativeLayout>
 
 
 
 
 
 
 
 
 
 
 
 
直接上Demo
也可以到:http://download.csdn.net/detail/kunglun/9800389下载
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值