亮點(diǎn)是使用外部文件修改了觸摸屏的觸摸位置,導(dǎo)入動(dòng)畫效果nineoldandroids的jar包
其中需要注意的是布局文件中的android:name
<code class="language-html" hljs=""><relativelayout android:background="@drawable/qq" android:layout_height="match_parent" android:layout_width="match_parent"></relativelayout></code><b><code class="language-html" hljs=""><fragment android:id="@+id/id_left_menu" android:layout_gravity="left" android:layout_height="match_parent" android:layout_width="200dp" android:name="com.zhy.demo_zhy_17_drawerlayout.MenuLeftFragment" android:tag="LEFT"><fragment android:id="@+id/id_right_menu" android:layout_gravity="right" android:layout_height="match_parent" android:layout_width="100dp" android:name="com.zhy.demo_zhy_17_drawerlayout.MenuRightFragment" android:tag="RIGHT"></fragment></fragment></code></button>
效果實(shí)現(xiàn)
主程序
<code class="language-java" hljs="">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); //這個(gè)調(diào)用來修改觸摸范圍 setDrawerLeftEdgeSize(this, mDrawerLayout, 0.3f); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT); } //這個(gè)方法是修改屏幕側(cè)邊的觸摸范圍 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 } }}</code>
左邊顯示程序
<code class="language-java" hljs="">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); } }</code>
右邊顯示程序
<code class="language-java" hljs="">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); } }</code>
布局文件
<code class="language-html" hljs=""><relativelayout android:background="@drawable/qq" android:layout_height="match_parent" android:layout_width="match_parent"></relativelayout></code><b><code class="language-html" hljs=""><fragment android:id="@+id/id_left_menu" android:layout_gravity="left" android:layout_height="match_parent" android:layout_width="200dp" android:name="com.test.mysliderqq.MenuLeftFragment" android:tag="LEFT"><fragment android:id="@+id/id_right_menu" android:layout_gravity="right" android:layout_height="match_parent" android:layout_width="100dp" android:name="com.test.mysliderqq.MenuRightFragment" android:tag="RIGHT"></fragment></fragment></code></button>
左邊布局文件layout_menu.xml
<code class="language-html" hljs=""><relativelayout android:background="#00000000" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"><li><relativelayout android:layout_height="wrap_content" android:layout_width="match_parent"><imageview android:id="@+id/one" android:layout_centervertical="true" android:layout_height="50dp" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_width="50dp" android:src="@drawable/img_1"><textview android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_torightof="@id/one" android:layout_width="fill_parent" android:text="第1個(gè)Item" android:textcolor="#f0f0f0" android:textsize="20sp"></textview></imageview></relativelayout><relativelayout android:layout_height="wrap_content" android:layout_width="match_parent"><imageview android:id="@+id/two" android:layout_centervertical="true" android:layout_height="50dp" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_width="50dp" android:src="@drawable/img_2"><textview android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_torightof="@id/two" android:layout_width="fill_parent" android:text="第2個(gè)Item" android:textcolor="#f0f0f0" android:textsize="20sp"></textview></imageview></relativelayout><relativelayout android:layout_height="wrap_content" android:layout_width="match_parent"><imageview android:id="@+id/three" android:layout_centervertical="true" android:layout_height="50dp" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_width="50dp" android:src="@drawable/img_3"><textview android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_torightof="@id/three" android:layout_width="fill_parent" android:text="第3個(gè)Item" android:textcolor="#f0f0f0" android:textsize="20sp"></textview></imageview></relativelayout><relativelayout android:layout_height="wrap_content" android:layout_width="match_parent"><imageview android:id="@+id/four" android:layout_centervertical="true" android:layout_height="50dp" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_width="50dp" android:src="@drawable/img_4"><textview android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_torightof="@id/four" android:layout_width="fill_parent" android:text="第4個(gè)Item" android:textcolor="#f0f0f0" android:textsize="20sp"></textview></imageview></relativelayout><relativelayout android:layout_height="wrap_content" android:layout_width="match_parent"><imageview android:id="@+id/five" android:layout_centervertical="true" android:layout_height="50dp" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_width="50dp" android:src="@drawable/img_5"><textview android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_torightof="@id/five" android:layout_width="fill_parent" android:text="第5個(gè)Item" android:textcolor="#f0f0f0" android:textsize="20sp"></textview></imageview></relativelayout></linearlayout></relativelayout></code>
右邊布局文件menu_layout_right.xml
<code class="language-html" hljs=""><li><li><imageview android:layout_gravity="center" android:layout_height="60dp" android:layout_width="60dp" android:src="@drawable/wode"><textview android:gravity="center" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="掃一掃" android:textcolor="#ffffff"></textview></imageview></linearlayout><li><imageview android:layout_gravity="center" android:layout_height="60dp" android:layout_width="60dp" android:src="@drawable/saoma"><textview android:gravity="center" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="討論組" android:textcolor="#ffffff"></textview></imageview></linearlayout><li><imageview android:layout_gravity="center" android:layout_height="60dp" android:layout_width="60dp" android:src="@drawable/wode"><textview android:gravity="center" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="掃一掃" android:textcolor="#ffffff"></textview></imageview></linearlayout><li><imageview android:layout_gravity="center" android:layout_height="60dp" android:layout_width="60dp" android:src="@drawable/saoma"><textview android:gravity="center" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="討論組" android:textcolor="#ffffff"></textview></imageview></linearlayout></linearlayout></code>
效果圖
簡單講解
1、為了模擬QQ的右側(cè)菜單需要點(diǎn)擊才能出現(xiàn),所以在初始化DrawerLayout的時(shí)候,使用了mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.RIGHT);意思是只有編程才能將其彈出,
雙向滑動(dòng)(...)
,電腦資料
《雙向滑動(dòng)(...)》(http://www.msguai.com)。然后在彈出以后,需要讓手勢可以滑動(dòng)回去,所以在OpenRightMenu中又編寫了:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.RIGHT); UNLOCK了一下。
最后在onDrawerClosed回調(diào)中,繼續(xù)設(shè)置mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.RIGHT);
2、動(dòng)畫效果
動(dòng)畫用了nineoldandroids,關(guān)于動(dòng)畫各種偏移量、縮放比例的計(jì)算請參考Android 高仿 QQ5.0 側(cè)滑菜單效果 自定義控件來襲 基本是一致的,唯一的不同的地方,給Content設(shè)置了ViewHelper.setTranslationX(mContent, mMenu.getMeasuredWidth() * (1 - scale));讓Content在菜單的右側(cè),默認(rèn)情況下Menu在菜單之上,所以我們根據(jù)菜單劃出的距離給Content設(shè)置X方向的偏移量。
好了,其實(shí)看到可以這么做,基本上任何的側(cè)滑菜單效果都能寫出來了。有興趣的話,可以拿DrawerLayout實(shí)現(xiàn)這篇博客的所有效果:Android 實(shí)現(xiàn)形態(tài)各異的雙向側(cè)滑菜單 自定義控件來襲 。
3、setDrawerListener
通過代碼也能看出來,可以使用setDrawerListener監(jiān)聽菜單的打開與關(guān)閉等等。這里對(duì)于當(dāng)前操作是哪個(gè)菜單的判斷是通過TAG判斷的,我覺得使用gravity應(yīng)該也能判斷出來~~