自定义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下载
 
 

你可能感兴趣的:(android,安卓,博客,自定义控件,自定义ViewGroup)