雙向滑動(...)

測試可用:http://blog.csdn.net/lmj623565791/article/details/41531475
優化參照:http://stackoverflow.com/questions/17699869/how-to-show-the-drawerlayout-when-sliding-from-left-to-right-no-matter-where

亮點是使用外部文件修改了觸摸屏的觸摸位置,導入動畫效果nineoldandroids的jar包

其中需要注意的是佈局文件中的android:name

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/id_drawerLayout"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="@drawable/img_frame_background" >  

    <RelativeLayout  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:background="@drawable/qq" >  

        <Button  
            android:layout_width="40dp"  
            android:layout_height="30dp"  
             android:layout_marginTop="10dp"  
            android:layout_alignParentRight="true"  
            android:background="@drawable/youce"  
            android:onClick="OpenRightMenu" />  
    </RelativeLayout>  
      <!--android:name爲包名,加繼承過fragment的類的名字 -->
    <fragment  
        android:id="@+id/id_left_menu"  
        android:name="com.zhy.demo_zhy_17_drawerlayout.MenuLeftFragment"  
        android:layout_width="200dp"  
        android:layout_height="match_parent"  
        android:layout_gravity="left"  
        android:tag="LEFT" />  

    <fragment  
        android:id="@+id/id_right_menu"  
        android:name="com.zhy.demo_zhy_17_drawerlayout.MenuRightFragment"  
        android:layout_width="100dp"  
        android:layout_height="match_parent"  
        android:layout_gravity="right"  
        android:tag="RIGHT" />  

</android.support.v4.widget.DrawerLayout>  

效果實現

主程序

package com.test.mysliderqq;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;  
import android.support.v4.app.FragmentActivity;  
import android.support.v4.widget.DrawerLayout;  
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.support.v4.widget.ViewDragHelper;
import android.view.Gravity;  
import android.view.View;  
import android.view.Window;

import java.lang.reflect.Field;

import com.nineoldandroids.view.ViewHelper;  

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public class MainActivity extends FragmentActivity  
{  

    private DrawerLayout mDrawerLayout;  

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

        initView();  
        initEvents();  

    }  

    public void OpenRightMenu(View view)  
    {  
        mDrawerLayout.openDrawer(Gravity.RIGHT);  
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,  
                Gravity.RIGHT);  
    }  

    private void initEvents()  
    {  
        mDrawerLayout.setDrawerListener(new DrawerListener()  
        {  
            @Override  
            public void onDrawerStateChanged(int newState)  
            {  
            }  

            @Override  
            public void onDrawerSlide(View drawerView, float slideOffset)  
            {  
                View mContent = mDrawerLayout.getChildAt(0);  
                View mMenu = drawerView;  
                float scale = 1 - slideOffset;  
                float rightScale = 0.8f + scale * 0.2f;  

                if (drawerView.getTag().equals("LEFT"))  
                {  

                    float leftScale = 1 - 0.3f * scale;  

                    ViewHelper.setScaleX(mMenu, leftScale);  
                    ViewHelper.setScaleY(mMenu, leftScale);  
                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));  
                    ViewHelper.setTranslationX(mContent,  
                            mMenu.getMeasuredWidth() * (1 - scale));  
                    ViewHelper.setPivotX(mContent, 0);  
                    ViewHelper.setPivotY(mContent,  
                            mContent.getMeasuredHeight() / 2);  
                    mContent.invalidate();  
                    ViewHelper.setScaleX(mContent, rightScale);  
                    ViewHelper.setScaleY(mContent, rightScale);  
                } else  
                {  
                    ViewHelper.setTranslationX(mContent,  
                            -mMenu.getMeasuredWidth() * slideOffset);  
                    ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());  
                    ViewHelper.setPivotY(mContent,  
                            mContent.getMeasuredHeight() / 2);  
                    mContent.invalidate();  
                    ViewHelper.setScaleX(mContent, rightScale);  
                    ViewHelper.setScaleY(mContent, rightScale);  
                }  

            }  

            @Override  
            public void onDrawerOpened(View drawerView)  
            {  
            }  

            @Override  
            public void onDrawerClosed(View drawerView)  
            {  
                mDrawerLayout.setDrawerLockMode(  
                        DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);  
            }  
        });  
    }  

    private void initView()  
    {  
        mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);  
        //這個調用來修改觸摸範圍
        setDrawerLeftEdgeSize(this, mDrawerLayout, 0.3f);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,  
                Gravity.RIGHT);  
    }  

    //這個方法是修改屏幕側邊的觸摸範圍
    public static void setDrawerLeftEdgeSize(Activity activity, DrawerLayout drawerLayout, float displayWidthPercentage) {
        if (activity == null || drawerLayout == null)
            return;

        try {
            // find ViewDragHelper and set it accessible
            Field leftDraggerField = drawerLayout.getClass().getDeclaredField("mLeftDragger");
            leftDraggerField.setAccessible(true);
            ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(drawerLayout);
            // find edgesize and set is accessible
            Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize");
            edgeSizeField.setAccessible(true);
            int edgeSize = edgeSizeField.getInt(leftDragger);
            // set new edgesize
            Point displaySize = new Point();
            activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
            edgeSizeField.setInt(leftDragger, Math.max(edgeSize, (int) (displaySize.x * displayWidthPercentage)));
        } catch (NoSuchFieldException e) {
            // ignore
        } catch (IllegalArgumentException e) {
            // ignore
        } catch (IllegalAccessException e) {
            // ignore
        }
    }

}  

左邊顯示程序

package com.test.mysliderqq;

import android.os.Bundle;  
import android.support.v4.app.Fragment;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  

public class MenuLeftFragment extends Fragment  
{  

    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState)  
    {  
        return inflater.inflate(R.layout.layout_menu, container, false);  
    }  
}  

右邊顯示程序

package com.test.mysliderqq;

import android.os.Bundle;  
import android.support.v4.app.Fragment;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  

public class MenuRightFragment extends Fragment  
{  

    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState)  
    {  
        return inflater.inflate(R.layout.menu_layout_right, container, false);  
    }  
}  

佈局文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/id_drawerLayout"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="@drawable/img_frame_background" >  

    <RelativeLayout  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:background="@drawable/qq" >  

        <Button  
            android:layout_width="40dp"  
            android:layout_height="30dp"  
             android:layout_marginTop="10dp"  
            android:layout_alignParentRight="true"  
            android:background="@drawable/youce"  
            android:onClick="OpenRightMenu" />  
    </RelativeLayout>  
    <!--  -->
    <fragment  
        android:id="@+id/id_left_menu"  
        android:name="com.test.mysliderqq.MenuLeftFragment"  
        android:layout_width="200dp"  
        android:layout_height="match_parent"  
        android:layout_gravity="left"  
        android:tag="LEFT" />  

    <fragment  
        android:id="@+id/id_right_menu"  
        android:name="com.test.mysliderqq.MenuRightFragment"  
        android:layout_width="100dp"  
        android:layout_height="match_parent"  
        android:layout_gravity="right"  
        android:tag="RIGHT" />  

</android.support.v4.widget.DrawerLayout>  

左邊佈局文件layout_menu.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"  
    android:background="#00000000" >  

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

        <RelativeLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content" >  

            <ImageView  
                android:id="@+id/one"  
                android:layout_width="50dp"  
                android:layout_height="50dp"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_marginTop="20dp"  
                android:src="@drawable/img_1" />  

            <TextView  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_toRightOf="@id/one"  
                android:text="第1個Item"  
                android:textColor="#f0f0f0"  
                android:textSize="20sp" />  
        </RelativeLayout>  

        <RelativeLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content" >  

            <ImageView  
                android:id="@+id/two"  
                android:layout_width="50dp"  
                android:layout_height="50dp"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_marginTop="20dp"  
                android:src="@drawable/img_2" />  

            <TextView  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_toRightOf="@id/two"  
                android:text="第2個Item"  
                android:textColor="#f0f0f0"  
                android:textSize="20sp" />  
        </RelativeLayout>  

        <RelativeLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content" >  

            <ImageView  
                android:id="@+id/three"  
                android:layout_width="50dp"  
                android:layout_height="50dp"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_marginTop="20dp"  
                android:src="@drawable/img_3" />  

            <TextView  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_toRightOf="@id/three"  
                android:text="第3個Item"  
                android:textColor="#f0f0f0"  
                android:textSize="20sp" />  
        </RelativeLayout>  

        <RelativeLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content" >  

            <ImageView  
                android:id="@+id/four"  
                android:layout_width="50dp"  
                android:layout_height="50dp"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_marginTop="20dp"  
                android:src="@drawable/img_4" />  

            <TextView  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_toRightOf="@id/four"  
                android:text="第4個Item"  
                android:textColor="#f0f0f0"  
                android:textSize="20sp" />  
        </RelativeLayout>  

        <RelativeLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content" >  

            <ImageView  
                android:id="@+id/five"  
                android:layout_width="50dp"  
                android:layout_height="50dp"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_marginTop="20dp"  
                android:src="@drawable/img_5" />  

            <TextView  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:layout_marginLeft="20dp"  
                android:layout_toRightOf="@id/five"  
                android:text="第5個Item"  
                android:textColor="#f0f0f0"  
                android:textSize="20sp" />  
        </RelativeLayout>  
    </LinearLayout>  

</RelativeLayout>  

右邊佈局文件menu_layout_right.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="20dp"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:src="@drawable/wode" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="掃一掃"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="20dp"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:src="@drawable/saoma" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="討論組"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="20dp"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:src="@drawable/wode" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="掃一掃"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="20dp"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:src="@drawable/saoma" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="討論組"
            android:textColor="#ffffff" />
    </LinearLayout>

</LinearLayout>

效果圖

這裏寫圖片描述

簡單講解

1、爲了模擬QQ的右側菜單需要點擊才能出現,所以在初始化DrawerLayout的時候,使用了mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.RIGHT);意思是隻有編程才能將其彈出。

然後在彈出以後,需要讓手勢可以滑動回去,所以在OpenRightMenu中又編寫了:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.RIGHT); UNLOCK了一下。

最後在onDrawerClosed回調中,繼續設置mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.RIGHT);

2、動畫效果

動畫用了nineoldandroids,關於動畫各種偏移量、縮放比例的計算請參考Android 高仿 QQ5.0 側滑菜單效果 自定義控件來襲 基本是一致的,唯一的不同的地方,給Content設置了ViewHelper.setTranslationX(mContent, mMenu.getMeasuredWidth() * (1 - scale));讓Content在菜單的右側,默認情況下Menu在菜單之上,所以我們根據菜單劃出的距離給Content設置X方向的偏移量。

好了,其實看到可以這麼做,基本上任何的側滑菜單效果都能寫出來了。有興趣的話,可以拿DrawerLayout實現這篇博客的所有效果:Android 實現形態各異的雙向側滑菜單 自定義控件來襲 。

3、setDrawerListener

通過代碼也能看出來,可以使用setDrawerListener監聽菜單的打開與關閉等等。這裏對於當前操作是哪個菜單的判斷是通過TAG判斷的,我覺得使用gravity應該也能判斷出來~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章