Android high imitation QQ5.2 two-way Slide menu DrawerLayout implementation source code

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.