抽屜式選單-仿QQ5.0側滑選單
阿新 • • 發佈:2019-01-03
為擴充套件螢幕尺寸所帶來的侷限性,很多APP都有側滑選單。所謂的側滑選單就是將一些選單選項隱藏起來,而不是放置在主螢幕上,然後可以通過滑動的方式將選單顯示出來。
本文將通過android提供的HorizontalScrollView來實現一個仿QQ5.0側滑選單的案例,通過自定義控制元件和屬性動畫來實現側滑效果。
1. 最終效果圖
2. 佈局檔案
left_menu.xml
activity_main.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/call" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="160dp" android:drawableLeft="@drawable/nav_call" android:drawablePadding="20dp" android:paddingLeft="20dp" android:text="Call" android:gravity="center_vertical" android:background="@null"/> <Button android:id="@+id/friends" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/nav_friends" android:drawablePadding="20dp" android:text="Friends" android:gravity="center_vertical" android:paddingLeft="20dp" android:background="@null"/> <Button android:id="@+id/location" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/nav_location" android:drawablePadding="20dp" android:text="Location" android:gravity="center_vertical" android:paddingLeft="20dp" android:background="@null"/> <Button android:id="@+id/mail" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/nav_mail" android:drawablePadding="20dp" android:text="Mail" android:gravity="center_vertical" android:paddingLeft="20dp" android:background="@null"/> <Button android:id="@+id/tasks" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/nav_task" android:drawablePadding="20dp" android:text="Tasks" android:gravity="center_vertical" android:paddingLeft="20dp" android:background="@null"/> </LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:yxman="http://schemas.android.com/apk/res/com.yx.slidingmenu" android:layout_width="match_parent" android:layout_height="match_parent" > <com.yx.slidingmenu.view.SlidingMenu android:id="@+id/id_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/sliding_menu_background" yxman:rightPadding="80dp" > <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <include layout="@layout/left_menu" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/home_background_image" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#13b7f6" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_menu" android:onClick="toggleMenu" /> </RelativeLayout> </LinearLayout> </LinearLayout> </com.yx.slidingmenu.view.SlidingMenu> </RelativeLayout>
首先自定義一個用於顯示側滑選單的left_menu.xml佈局檔案,然後在activity_main.xml主佈局檔案中通過一個水平的LinearLayout將子佈局檔案載入進來,以及將主佈局檔案編寫在其中。
3. 新增自定義屬性
a. 首先在values下新建attr.xml,程式碼如下:
b. 在主佈局activity_main.xml中宣告名稱空間和使用自定的屬性:<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="rightPadding" format="dimension"></attr> <declare-styleable name="SlidingMenu"> <attr name="rightPadding"></attr> </declare-styleable> </resources>
<com.yx.slidingmenu.view.SlidingMenu
android:id="@+id/id_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sliding_menu_background"
yxman:rightPadding="80dp" >
......
</com.yx.slidingmenu.view.SlidingMenu>
我的名稱空間是xmlns:yxman="http://schemas.android.com/apk/res/com.yx.slidingmenu"。格式是:http://schemas.android.com/apk/res/後面再加上專案的包名。
屬性yxman:rightPadding="80dp"右側內邊距設定為80dp。
4. 自定義SlidingMenu
SlidingMenu.java
package com.yx.slidingmenu.view;
import com.nineoldandroids.view.ViewHelper;
import com.yx.slidingmenu.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
public class SlidingMenu extends HorizontalScrollView {
private LinearLayout mWapper;
private ViewGroup mMenu;
private ViewGroup mContent;
private int mScreenWidth;
private int mMenuWidth;
// 單位是dp
private int mMenuRightPadding = 50;
private boolean flag;
private boolean isOpen;
public SlidingMenu(Context context) {
this(context, null);
}
/**
* 未使用自定義屬性時呼叫
*
* @param context
* @param attrs
*/
public SlidingMenu(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 當使用了自定義屬性時會呼叫此構造方法
*
* @param context
* @param attrs
* @param defStyle
*/
public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 獲取自定義定義的屬性
TypedArray attribute = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyle, 0);
int n = attribute.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = attribute.getIndex(i);
switch (attr) {
case R.styleable.SlidingMenu_rightPadding:
int defValue = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
mMenuRightPadding = attribute.getDimensionPixelSize(attr, defValue);
break;
}
}
attribute.recycle();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mScreenWidth = outMetrics.widthPixels;
}
/**
* 設定子View的寬和高,設定自己的寬和高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!flag) {
mWapper = (LinearLayout) getChildAt(0);
mMenu = (ViewGroup) mWapper.getChildAt(0);
mContent = (ViewGroup) mWapper.getChildAt(1);
mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
mContent.getLayoutParams().width = mScreenWidth;
flag = true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* 通過設定偏移量,將menu隱藏
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
this.scrollTo(mMenuWidth, 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
// 隱藏在左邊的寬度
int scrollX = getScrollX();
if (scrollX >= mMenuWidth / 2) {
this.smoothScrollTo(mMenuWidth, 0);
isOpen = false;
} else {
this.smoothScrollTo(0, 0);
isOpen = true;
}
return true;
}
return super.onTouchEvent(ev);
}
/**
* 切換選單
*/
public void toggle() {
if (isOpen) {
closeMenu();
} else {
openMenu();
}
}
/**
* 開啟選單
*/
public void openMenu() {
if (isOpen)
return;
this.smoothScrollTo(0, 0);
isOpen = true;
}
/**
* 關閉選單
*/
public void closeMenu() {
if (!isOpen)
return;
this.smoothScrollTo(mMenuWidth, 0);
isOpen = false;
}
/**
* 滾動發生時
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
// scale取值範圍:0~1
float scale = l * 1.0f / mMenuWidth;
/**
* 1. 內容區域0.7~1.0 縮放的效果 scale: (0.7 + 0.3 * scale)
* 2. 選單的偏移量需要修改
* 3. 選單的顯示時有縮放以及透明度變化:
* 縮放: 0.7 ~1.0 -> 1.0 - scale * 0.3
* 透明度: 0.6 ~ 1.0 -> 1.0 - scale * 0.4
*/
float rightScale = 0.7f + 0.3f * scale;
float leftScale = 1.0f - scale * 0.3f;
float leftAlpha = 1.0f - scale * 0.4f;
// 呼叫屬性動畫,設定TranslationX
ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.8f);
ViewHelper.setScaleX(mMenu, leftScale);
ViewHelper.setScaleY(mMenu, leftScale);
ViewHelper.setAlpha(mMenu, leftAlpha);
// 設定content的縮放的中心點
ViewHelper.setPivotX(mContent, 0);
ViewHelper.setPivotY(mContent, mContent.getHeight() / 2);
ViewHelper.setScaleX(mContent, rightScale);
ViewHelper.setScaleY(mContent, rightScale);
}
}
以上程式碼看著很複雜,但都有詳細的註釋,主要是用於處理滑動事件,不過多解釋。
5. MainActivity.java
package com.yx.slidingmenu;
import com.yx.slidingmenu.view.SlidingMenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private SlidingMenu mLeftMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLeftMenu = (SlidingMenu) findViewById(R.id.id_menu);
}
public void toggleMenu(View view) {
mLeftMenu.toggle();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
至此,核心程式碼和主要演示過程以完畢。是不是感覺很炫酷,就是這麼簡單。如有疑問下載原始碼自行研究。