亮点是使用外部文件修改了触摸屏的触摸位置,导入动画效果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>
效果实现
主程序
<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); //这个调用来修改触摸范围 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 } }}</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个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个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个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个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个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的右侧菜单需要点击才能出现,所以在初始化DrawerLayout的时候,使用了mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.RIGHT);意思是只有编程才能将其弹出,
双向滑动(...)
,电脑资料
《双向滑动(...)》(http://meiwen.anslib.com)。然后在弹出以后,需要让手势可以滑动回去,所以在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应该也能判断出来~~