1、效果图,网上找的,主要动画细节是一样的
2、集成appcompat_v7 包就成比如:
compile 'com.android.support:appcompat-v7:25.0.1'
3、布局上面一个toolbar,下面一个drawlayout布局
4、drawlayout布局,主界面一个图片,抽屉是一个随便的布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/tool001" android:layout_width="match_parent" android:layout_height="44dp" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="测试001"/> </RelativeLayout> </android.support.v7.widget.Toolbar> <include layout="@layout/activity_main" ></include> </LinearLayout>
5.抽屉的布局<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawerLayout001" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="hnq.sanhenxian.MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher"/> <include layout="@layout/left" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" > </include> </android.support.v4.widget.DrawerLayout>
6、然后得让主题编程noactionbar的如下面<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorAccent"> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center_horizontal" android:text="目录1"/> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center_horizontal" android:text="目录2"/> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center_horizontal" android:text="目录3"/> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center_horizontal" android:text="目录4"/> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center_horizontal" android:text="目录5"/> </LinearLayout>
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
7、下面就是主要代码了
主要是package hnq.sanhenxian; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; public class MainActivity extends AppCompatActivity { private ActionBarDrawerToggle mDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toolbar toolbar= (Toolbar) findViewById(R.id.tool001); setSupportActionBar(toolbar); getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用 getSupportActionBar().setDisplayHomeAsUpEnabled(true); DrawerLayout drawerLayout= (DrawerLayout) findViewById(R.id.drawerLayout001); mDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.ss,R.string.ss1); mDrawerToggle.syncState(); drawerLayout.addDrawerListener(mDrawerToggle); } }
这个东西再起作用,构造函数后面的2个是字符串资源,这里随便写2个就行了,比如我是这样的ActionBarDrawerToggle
<resources> <string name="app_name">SanHenXian</string> <string name="ss">打开</string> <string name="ss1">关闭</string> </resources>
感觉这2个参数没用,当然可能其他地方有用,可是标示什么的。按源码的解释,是标示打开关闭的描述。
实现原理:
根据path画出来的。具体怎么画的看不懂,没得算法。