仿QQ侧滑菜单

public class SlideMenu extends HorizontalScrollView {

    private LinearLayout mWapper;
    private ViewGroup mMenu;
    private ViewGroup mContent;
    private int mWindowWidth;
    //dp
    private int mMenuRightMargin = 50;
    private int mMenuWidth;
    private boolean onMeasureOnce;
    private boolean isMenuOpened;

    public SlideMenu(Context context) {
        this(context, null);
    }

    /**
     *  未自定义属性性使用该方法
     * @param context
     * @param attrs
     */
    public SlideMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }


    /**
     * 使用自定义属性时调用该方法
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public SlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        mWindowWidth = dm.widthPixels;
        //转为dp
        mMenuRightMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
        //获取属性
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlideMenu,defStyleAttr,0);
        int n = ta.getIndexCount();
        for(int i=0;i<n;i++){
            int att = ta.getIndex(i);
            switch (att){
                case R.styleable.SlideMenu_rightMargin:
                    //第二个参数是默认值
                    mMenuRightMargin = ta.getDimensionPixelSize(att,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics()));
                break;
                default:
            }

        }
        ta.recycle();

    }

    /**先决定子view的宽高,再决定自己的宽高
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(!onMeasureOnce) {
            mWapper = (LinearLayout) getChildAt(0);
            mMenu = (ViewGroup) mWapper.getChildAt(0);
            mContent = (ViewGroup) mWapper.getChildAt(1);

            //设置菜间的宽度,为屏幕宽度-它距离右边距长度
            mMenuWidth = mMenu.getLayoutParams().width = mWindowWidth - mMenuRightMargin;
            mContent.getLayoutParams().width = mWindowWidth;
            onMeasureOnce = true;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     *  设置偏移量将menu隐藏
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(changed) {
            //界面向左移动
            this.scrollTo(mMenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action){
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                //若大于屏幕1/2,则将菜单隐藏
                if (scrollX > mWindowWidth/2) {
                    this.smoothScrollTo(mMenuWidth,0);
                    isMenuOpened = false;
                } else {
                    this.smoothScrollTo(0,0);
                    isMenuOpened = true;
                }
                return true;

        }
        return super.onTouchEvent(ev);
    }
    public void toggleMenu(){
        if(isMenuOpened){
            hideMenu();
        }else{
            showMenu();
        }
    }
    public void showMenu(){
        if(isMenuOpened) return;
        this.smoothScrollTo(0,0);
        isMenuOpened = true;
    }
    public void hideMenu(){
        if(!isMenuOpened) return;
        this.smoothScrollTo(mMenuWidth,0);
        isMenuOpened = false;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);


        mMenu.setTranslationX(l*0.7f);
        float sc = ((float)l)/mMenuWidth;
        mContent.setScaleX(0.7f+0.3f*sc);
        mContent.setScaleY(0.7f+0.3f*sc);
        mContent.setPivotX(0);
        mContent.setPivotY(mContent.getHeight()/2);
        mContent.setAlpha(0.6f+0.4f*sc);

        mMenu.setScaleX(1-0.3f*sc);
        mMenu.setScaleY(1-0.3f*sc);
        mMenu.setAlpha(0.6f+0.4f*(1-sc));

    }
}

主类

public class MainActivity extends ActionBarActivity {

    private SlideMenu slideMenu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        slideMenu = (SlideMenu) findViewById(R.id.slide_menu);
    }

    //按钮点击事件
    public void toggleMenu(View v){
        slideMenu.toggleMenu();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

菜单布局

<?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"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="10dp">

        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/img_1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"

            android:text="Item1"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center_vertical" />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="10dp">

            <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/img_2"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"

                android:text="Item2"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="10dp">

            <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/img_3"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"

                android:text="Item3"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical" />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/img_4"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"

                android:text="Item4"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical" />

        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

主布局

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

    <com.qiaqia.slidemenuview.SlideMenu
        android:id="@+id/slide_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        myns:rightMargin="70dp"
        android:background="@drawable/img_frame_background">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <include layout="@layout/layout_menu" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/background">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#0066FF">
                    <Button
                        android:onClick="toggleMenu"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="show"/>

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>

    </com.qiaqia.slidemenuview.SlideMenu>

</RelativeLayout>

自定义属性

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


    <declare-styleable name="SlideMenu">
        <attr name="rightMargin"
            format="dimension"></attr>
    </declare-styleable>
</resources>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值