Android high imitation QQ5.2 two-way Slide menu DrawerLayout implementation source code
Android high imitation QQ5.2 two-way Slide menu DrawerLayout implementation source code
Slide left and right
1. Implementation of the Home Page
Directly use DrawerLayout as the root layout, and the first View inside is the content area, the second View is the left menu, the third View is the right slide menu, and the third is optional.
Layout:
Code
package com.pcachy.drawerlayout;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.widget.DrawerLayout;import android.view.Gravity;import android.view.View;import android.view.Window;import com.nineoldandroids.view.ViewHelper;public class MainActivity extends FragmentActivity {private DrawerLayout mDrawerLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();initEvents();}private void initView(){mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.END);}private void initEvents(){mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {@Overridepublic void onDrawerSlide(View drawerView, float slideOffset) {// TODO Auto-generated method stubView 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);}}@Overridepublic void onDrawerOpened(View drawerView) {// TODO Auto-generated method stub}@Overridepublic void onDrawerClosed(View drawerView) {// TODO Auto-generated method stubmDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);}@Overridepublic void onDrawerStateChanged(int newState) {// TODO Auto-generated method stub}});}public void OpenRightMenu(View view){mDrawerLayout.openDrawer(Gravity.RIGHT);mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);}}
2. Left menu and right menu layout (the layout and code of left menu and right menu are whatever you want to write. Here is an example of left menu)
1. In order to simulate the right menu of QQ, You need to click to appear. Therefore, mDrawerLayout is used during the initialization of DrawerLayout. setDrawerLockMode (DrawerLayout. LOCK_MODE_LOCKED_CLOSED, Gravity. RIGHT. After the pop-up, you need to let the gesture slide back, so in OpenRightMenu, write: mDrawerLayout. setDrawerLockMode (DrawerLayout. LOCK_MODE_UNLOCKED, Gravity. RIGHT); UNLOCK again. Finally, in the onDrawerClosed callback, continue to set mDrawerLayout. setDrawerLockMode (DrawerLayout. LOCK_MODE_LOCKED_CLOSED, Gravity. RIGHT );
2. nineoldandroids is used for animation effects,
3. setDrawerListener
You can also see through the code that setDrawerListener can be used to listen to the opening and closing of menus. Here, the menu for the current operation is determined by TAG. I think gravity can be used to determine which menu is used.