Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码
Android 高仿QQ5.2雙向側滑菜單DrawerLayout實現源代碼
左右側滑效果圖
1.主頁的實現
直接將DrawerLayout作為根布局,然后其內部第一個View為內容區域,第二個View為左側菜單,第三個View為右側側滑菜單,當前第三個是可選的。
布局:
代碼
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.左菜單和右菜單布局(左菜單和右菜單的布局和代碼隨便你怎么寫,這里是左菜單的樣例) <?
xml version="1.0" encoding="utf-8"?
> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/one" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img_1" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/one" android:text="第1個Item" android:textColor="#f0f0f0" android:textSize="20sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/two" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img_2" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/two" android:text="第2個Item" android:textColor="#f0f0f0" android:textSize="20sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/three" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img_3" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/three" android:text="第3個Item" android:textColor="#f0f0f0" android:textSize="20sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/four" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img_4" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/four" android:text="第4個Item" android:textColor="#f0f0f0" android:textSize="20sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/five" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:src="@drawable/img_5" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/five" android:text="第5個Item" android:textColor="#f0f0f0" android:textSize="20sp" /> </RelativeLayout> </LinearLayout> </RelativeLayout>
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。
3、setDrawerListener
通過代碼也能看出來,能夠使用setDrawerListener監聽菜單的打開與關閉等等。這里對于當前操作是哪個菜單的推斷是通過TAG推斷的。我認為使用gravity應該也能推斷出來
源代碼下載地址:http://download.csdn.net/detail/pcaxb/9042309
總結
以上是生活随笔為你收集整理的Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android开发进阶1 思维改变 如何
- 下一篇: 使用CountDownLatch模拟高并