首页 技术 正文
技术 2022年11月20日
0 收藏 497 点赞 4,449 浏览 11574 个字

1、概述

之前写了一个Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 ,恰逢QQ5.2又加了一个右侧菜单,刚好看了下DrawerLayout,一方面官方的东西,我都比较感兴趣;另一方面,这玩意用起来的确方便,于是简单写了个demo,高仿QQ5.2双向侧滑,分享给大家。

首先看看效果图:

Android DrawerLayout 高仿QQ5.2双向侧滑菜单

DrawerLayout用起来真的很方便,下面一起看看用法~

2、DrawerLayout的使用

直接将DrawerLayout作为根布局,然后其内部第一个View为内容区域,第二个View为左侧菜单,第三个View为右侧侧滑菜单,当前第三个是可选的。

第一个View的宽高应当设置为match_parent,当然了,这也理所当然。

第二、三个View需要设置android:layout_gravity=”left”,和android:layout_gravity=”right”且一搬高度设置为match_parent,宽度为固定值,即侧滑菜单的宽度。

按照上面的描述写个布局文件,然后设置给Activity就添加好了左右侧滑了,是不是很简单~~~

比如我们的布局文件:

  1. <android.support.v4.widget.DrawerLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  2. xmlns:tools=”http://schemas.android.com/tools”
  3. android:id=”@+id/id_drawerLayout”
  4. android:layout_width=”match_parent”
  5. android:layout_height=”match_parent”
  6. android:background=”@drawable/img_frame_background” >
  7. <RelativeLayout
  8. android:layout_width=”match_parent”
  9. android:layout_height=”match_parent”
  10. android:background=”@drawable/qq” >
  11. <Button
  12. android:layout_width=”40dp”
  13. android:layout_height=”30dp”
  14. android:layout_marginTop=”10dp”
  15. android:layout_alignParentRight=”true”
  16. android:background=”@drawable/youce”
  17. android:onClick=”OpenRightMenu” />
  18. </RelativeLayout>
  19. <fragment
  20. android:id=”@+id/id_left_menu”
  21. android:name=”com.zhy.demo_zhy_17_drawerlayout.MenuLeftFragment”
  22. android:layout_width=”200dp”
  23. android:layout_height=”match_parent”
  24. android:layout_gravity=”left”
  25. android:tag=”LEFT” />
  26. <fragment
  27. android:id=”@+id/id_right_menu”
  28. android:name=”com.zhy.demo_zhy_17_drawerlayout.MenuRightFragment”
  29. android:layout_width=”100dp”
  30. android:layout_height=”match_parent”
  31. android:layout_gravity=”right”
  32. android:tag=”RIGHT” />
  33. </android.support.v4.widget.DrawerLayout>

这里我们的主内容区域为RelativeLayout

菜单用的两个Fragment,左侧为200dp,右侧为100dp;

好了,看了我们的布局文件,接下来看下我们的详细代码。

3、代码是最好的老师

 

1、MenuLeftFragment

  1. package com.zhy.demo_zhy_17_drawerlayout;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class MenuLeftFragment extends Fragment
  8. {
  9. @Override
  10. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  11. Bundle savedInstanceState)
  12. {
  13. return inflater.inflate(R.layout.layout_menu, container, false);
  14. }
  15. }

对应的布局文件:

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:layout_width=”match_parent”
  4. android:layout_height=”match_parent”
  5. android:background=”#00000000″ >
  6. <LinearLayout
  7. android:layout_width=”match_parent”
  8. android:layout_height=”wrap_content”
  9. android:layout_centerVertical=”true”
  10. android:orientation=”vertical” >
  11. <RelativeLayout
  12. android:layout_width=”match_parent”
  13. android:layout_height=”wrap_content” >
  14. <ImageView
  15. android:id=”@+id/one”
  16. android:layout_width=”50dp”
  17. android:layout_height=”50dp”
  18. android:layout_centerVertical=”true”
  19. android:layout_marginLeft=”20dp”
  20. android:layout_marginTop=”20dp”
  21. android:src=”@drawable/img_1″ />
  22. <TextView
  23. android:layout_width=”fill_parent”
  24. android:layout_height=”wrap_content”
  25. android:layout_centerVertical=”true”
  26. android:layout_marginLeft=”20dp”
  27. android:layout_toRightOf=”@id/one”
  28. android:text=”第1个Item”
  29. android:textColor=”#f0f0f0″
  30. android:textSize=”20sp” />
  31. </RelativeLayout>
  32. <RelativeLayout
  33. android:layout_width=”match_parent”
  34. android:layout_height=”wrap_content” >
  35. <ImageView
  36. android:id=”@+id/two”
  37. android:layout_width=”50dp”
  38. android:layout_height=”50dp”
  39. android:layout_centerVertical=”true”
  40. android:layout_marginLeft=”20dp”
  41. android:layout_marginTop=”20dp”
  42. android:src=”@drawable/img_2″ />
  43. <TextView
  44. android:layout_width=”fill_parent”
  45. android:layout_height=”wrap_content”
  46. android:layout_centerVertical=”true”
  47. android:layout_marginLeft=”20dp”
  48. android:layout_toRightOf=”@id/two”
  49. android:text=”第2个Item”
  50. android:textColor=”#f0f0f0″
  51. android:textSize=”20sp” />
  52. </RelativeLayout>
  53. <RelativeLayout
  54. android:layout_width=”match_parent”
  55. android:layout_height=”wrap_content” >
  56. <ImageView
  57. android:id=”@+id/three”
  58. android:layout_width=”50dp”
  59. android:layout_height=”50dp”
  60. android:layout_centerVertical=”true”
  61. android:layout_marginLeft=”20dp”
  62. android:layout_marginTop=”20dp”
  63. android:src=”@drawable/img_3″ />
  64. <TextView
  65. android:layout_width=”fill_parent”
  66. android:layout_height=”wrap_content”
  67. android:layout_centerVertical=”true”
  68. android:layout_marginLeft=”20dp”
  69. android:layout_toRightOf=”@id/three”
  70. android:text=”第3个Item”
  71. android:textColor=”#f0f0f0″
  72. android:textSize=”20sp” />
  73. </RelativeLayout>
  74. <RelativeLayout
  75. android:layout_width=”match_parent”
  76. android:layout_height=”wrap_content” >
  77. <ImageView
  78. android:id=”@+id/four”
  79. android:layout_width=”50dp”
  80. android:layout_height=”50dp”
  81. android:layout_centerVertical=”true”
  82. android:layout_marginLeft=”20dp”
  83. android:layout_marginTop=”20dp”
  84. android:src=”@drawable/img_4″ />
  85. <TextView
  86. android:layout_width=”fill_parent”
  87. android:layout_height=”wrap_content”
  88. android:layout_centerVertical=”true”
  89. android:layout_marginLeft=”20dp”
  90. android:layout_toRightOf=”@id/four”
  91. android:text=”第4个Item”
  92. android:textColor=”#f0f0f0″
  93. android:textSize=”20sp” />
  94. </RelativeLayout>
  95. <RelativeLayout
  96. android:layout_width=”match_parent”
  97. android:layout_height=”wrap_content” >
  98. <ImageView
  99. android:id=”@+id/five”
  100. android:layout_width=”50dp”
  101. android:layout_height=”50dp”
  102. android:layout_centerVertical=”true”
  103. android:layout_marginLeft=”20dp”
  104. android:layout_marginTop=”20dp”
  105. android:src=”@drawable/img_5″ />
  106. <TextView
  107. android:layout_width=”fill_parent”
  108. android:layout_height=”wrap_content”
  109. android:layout_centerVertical=”true”
  110. android:layout_marginLeft=”20dp”
  111. android:layout_toRightOf=”@id/five”
  112. android:text=”第5个Item”
  113. android:textColor=”#f0f0f0″
  114. android:textSize=”20sp” />
  115. </RelativeLayout>
  116. </LinearLayout>
  117. </RelativeLayout>

其实就是堆出来的布局~~没撒意思~

2、MenuRightFragment

  1. package com.zhy.demo_zhy_17_drawerlayout;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class MenuRightFragment extends Fragment
  8. {
  9. @Override
  10. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  11. Bundle savedInstanceState)
  12. {
  13. return inflater.inflate(R.layout.menu_layout_right, container, false);
  14. }
  15. }

对应布局文件:

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:layout_width=”match_parent”
  4. android:layout_height=”match_parent”
  5. android:gravity=”center_vertical”
  6. android:orientation=”vertical” >
  7. <LinearLayout
  8. android:layout_width=”match_parent”
  9. android:layout_height=”wrap_content”
  10. android:layout_centerVertical=”true”
  11. android:layout_gravity=”center_vertical”
  12. android:layout_marginBottom=”20dp”
  13. android:orientation=”vertical” >
  14. <ImageView
  15. android:layout_width=”60dp”
  16. android:layout_height=”60dp”
  17. android:layout_gravity=”center”
  18. android:src=”@drawable/wode” />
  19. <TextView
  20. android:layout_width=”fill_parent”
  21. android:layout_height=”wrap_content”
  22. android:gravity=”center”
  23. android:text=”扫一扫”
  24. android:textColor=”#ffffff” />
  25. </LinearLayout>
  26. <LinearLayout
  27. android:layout_width=”match_parent”
  28. android:layout_height=”wrap_content”
  29. android:layout_centerVertical=”true”
  30. android:layout_gravity=”center_vertical”
  31. android:layout_marginBottom=”20dp”
  32. android:orientation=”vertical” >
  33. <ImageView
  34. android:layout_width=”60dp”
  35. android:layout_height=”60dp”
  36. android:layout_gravity=”center”
  37. android:src=”@drawable/saoma” />
  38. <TextView
  39. android:layout_width=”fill_parent”
  40. android:layout_height=”wrap_content”
  41. android:gravity=”center”
  42. android:text=”讨论组”
  43. android:textColor=”#ffffff” />
  44. </LinearLayout>
  45. <LinearLayout
  46. android:layout_width=”match_parent”
  47. android:layout_height=”wrap_content”
  48. android:layout_centerVertical=”true”
  49. android:layout_gravity=”center_vertical”
  50. android:layout_marginBottom=”20dp”
  51. android:orientation=”vertical” >
  52. <ImageView
  53. android:layout_width=”60dp”
  54. android:layout_height=”60dp”
  55. android:layout_gravity=”center”
  56. android:src=”@drawable/wode” />
  57. <TextView
  58. android:layout_width=”fill_parent”
  59. android:layout_height=”wrap_content”
  60. android:gravity=”center”
  61. android:text=”扫一扫”
  62. android:textColor=”#ffffff” />
  63. </LinearLayout>
  64. <LinearLayout
  65. android:layout_width=”match_parent”
  66. android:layout_height=”wrap_content”
  67. android:layout_centerVertical=”true”
  68. android:layout_gravity=”center_vertical”
  69. android:layout_marginBottom=”20dp”
  70. android:orientation=”vertical” >
  71. <ImageView
  72. android:layout_width=”60dp”
  73. android:layout_height=”60dp”
  74. android:layout_gravity=”center”
  75. android:src=”@drawable/saoma” />
  76. <TextView
  77. android:layout_width=”fill_parent”
  78. android:layout_height=”wrap_content”
  79. android:gravity=”center”
  80. android:text=”讨论组”
  81. android:textColor=”#ffffff” />
  82. </LinearLayout>
  83. </LinearLayout>

依旧很简单,除了图标比较难找以外~~

3、MainActivity

MainActivity的布局文件已经贴过了~~

  1. package com.zhy.demo_zhy_17_drawerlayout;
  2. import android.os.Bundle;
  3. import android.support.v4.app.FragmentActivity;
  4. import android.support.v4.widget.DrawerLayout;
  5. import android.support.v4.widget.DrawerLayout.DrawerListener;
  6. import android.view.Gravity;
  7. import android.view.View;
  8. import android.view.Window;
  9. import com.nineoldandroids.view.ViewHelper;
  10. public class MainActivity extends FragmentActivity
  11. {
  12. private DrawerLayout mDrawerLayout;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. requestWindowFeature(Window.FEATURE_NO_TITLE);
  18. setContentView(R.layout.activity_main);
  19. initView();
  20. initEvents();
  21. }
  22. public void OpenRightMenu(View view)
  23. {
  24. mDrawerLayout.openDrawer(Gravity.RIGHT);
  25. mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
  26. Gravity.RIGHT);
  27. }
  28. private void initEvents()
  29. {
  30. mDrawerLayout.setDrawerListener(new DrawerListener()
  31. {
  32. @Override
  33. public void onDrawerStateChanged(int newState)
  34. {
  35. }
  36. @Override
  37. public void onDrawerSlide(View drawerView, float slideOffset)
  38. {
  39. View mContent = mDrawerLayout.getChildAt(0);
  40. View mMenu = drawerView;
  41. float scale = 1 – slideOffset;
  42. float rightScale = 0.8f + scale * 0.2f;
  43. if (drawerView.getTag().equals(“LEFT”))
  44. {
  45. float leftScale = 1 – 0.3f * scale;
  46. ViewHelper.setScaleX(mMenu, leftScale);
  47. ViewHelper.setScaleY(mMenu, leftScale);
  48. ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 – scale));
  49. ViewHelper.setTranslationX(mContent,
  50. mMenu.getMeasuredWidth() * (1 – scale));
  51. ViewHelper.setPivotX(mContent, 0);
  52. ViewHelper.setPivotY(mContent,
  53. mContent.getMeasuredHeight() / 2);
  54. mContent.invalidate();
  55. ViewHelper.setScaleX(mContent, rightScale);
  56. ViewHelper.setScaleY(mContent, rightScale);
  57. } else
  58. {
  59. ViewHelper.setTranslationX(mContent,
  60. -mMenu.getMeasuredWidth() * slideOffset);
  61. ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
  62. ViewHelper.setPivotY(mContent,
  63. mContent.getMeasuredHeight() / 2);
  64. mContent.invalidate();
  65. ViewHelper.setScaleX(mContent, rightScale);
  66. ViewHelper.setScaleY(mContent, rightScale);
  67. }
  68. }
  69. @Override
  70. public void onDrawerOpened(View drawerView)
  71. {
  72. }
  73. @Override
  74. public void onDrawerClosed(View drawerView)
  75. {
  76. mDrawerLayout.setDrawerLockMode(
  77. DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
  78. }
  79. });
  80. }
  81. private void initView()
  82. {
  83. mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);
  84. mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
  85. Gravity.RIGHT);
  86. }
  87. }

嗯,代码基本没什么注释~~维撒呢?是因为的确没什么好注释的。

提几点:

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应该也能判断出来~~

好了,没撒了,由于DrawerLayout默认只能从边界划出菜单,但是QQ划出菜单的手势区域比较大,大家有兴趣,可以重写Activity的onTouchEvent,在里面判断,如果是左右滑动手势神马的,弹出菜单,应该不麻烦~~~

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889