Android-通過SlidingMenu高仿微信6.2最新版手勢滑動返回(二)
轉載請標明出處:
http://blog.csdn.net/hanhailong726188/article/details/46453627
本文出自:【海龍的博客】
一、概述
在上一篇博文中,博文地址Android-通過SlidingPaneLayout高仿微信6.2最新版手勢滑動返回(一),我們是通過官方自帶的SlidingPaneLayout來實現的手勢滑動返回。在這篇博文中,我們將採用SlidingMenu來高仿。
事實上實現的原理都一樣。僅僅只是是把SlidingPaneLayout換成SlidingMenu而已,只是SlidingMenu本身的功能比官方自帶的強大,不僅能夠實現像微信一樣的邊緣滑動返回,還支持全屏幕滑動返回。
你還能夠加入動畫效果。只是這裏筆者業務時間不多。這裏就沒有實現!
首先來看一下效果圖,一個是邊緣滑動的,一個是全屏幕滑動,效果很贊。比我們上一篇博文中實現的還要給力些!
- 邊緣滑動效果圖
- 全屏幕滑動效果圖
以下我們從頭開始一步一步來實現這個手勢滑動返回效果,從新建project到引入SlidingMenu,然後通過改動Activity主題樣式、抽取出實現SwipeBack的父類BaseSwipeBackActivity等等
二、以下開始進入主題
首先肯定是通過AndroidStudio新建一個SlidingMenuSwipeBackDemoproject,然後把下好的SlidingMenu庫移到項目中,在settings.gradle文件加入這個庫,並在app內的build.gradle加入這個庫依賴
settings.gradle
include ‘:app’
include ‘:SlidingLibrary’
app內的build.gradle
compile project(":SlidingLibrary")
這裏我把SlidingMenu的庫名稱改成SlidingLibrary了。項目就差點兒相同建好了,見效果圖:
下一步就是把我們上個博文中的一些用到的資源文件拷過來,如須要用到的動畫資源slide_in_right.xml、slide_out_right.xml、styles.xml,這裏初略的展示下styles.xml文件內容,代碼例如以下:
<style name="JK.SwipeBack.Transparent.Theme" parent="AppTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@style/JK.Animation.SlidingBack</item>
<item name="android:actionBarStyle">@style/JKActionBar.Custom</item>
</style>
<style name="JKActionBar.Custom" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="displayOptions">showCustom</item>
<item name="android:background">@android:color/transparent</item>
<item name="background">@android:color/transparent</item>
<item name="android:displayOptions" tools:ignore="NewApi">showCustom</item>
<item name="android:height">?actionBarSize</item>
</style>
<style name="JK.Animation.SlidingBack" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
<item name="android:activityOpenExitAnimation">@anim/slide_out_right</item>
<item name="android:activityCloseEnterAnimation">@anim/slide_in_right</item>
<item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
<item name="android:wallpaperOpenEnterAnimation">@anim/slide_in_right</item>
<item name="android:wallpaperOpenExitAnimation">@anim/slide_out_right</item>
<item name="android:wallpaperCloseEnterAnimation">@anim/slide_in_right</item>
<item name="android:wallpaperCloseExitAnimation">@anim/slide_out_right</item>
<item name="android:wallpaperIntraOpenEnterAnimation">@anim/slide_in_right</item>
<item name="android:wallpaperIntraOpenExitAnimation">@anim/slide_out_right</item>
<item name="android:wallpaperIntraCloseEnterAnimation">@anim/slide_in_right</item>
<item name="android:wallpaperIntraCloseExitAnimation">@anim/slide_out_right</item>
</style>
接下來我們創建BaseSwipeBackActivity這個基類。全部實現手勢滑動返回的類繼承自它就OK了,不須要手勢滑動的不須要繼承它。大招來了,看BaseSwipeBackActivity源代碼文件。由於代碼凝視的比較具體,這裏我就直接貼代碼了,代碼很的簡單
package com.hhl.slidingmenuswipebackdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityHelper;
/**
* Created by hailonghan on 15/6/11.
*/
public abstract class BaseSwipeBackActivity extends AppCompatActivity implements SlidingMenu.OnOpenedListener {
private SlidingActivityHelper mHelper;
//SlidingMenu
private SlidingMenu mSlidingMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
mHelper = new SlidingActivityHelper(this);
mHelper.onCreate(savedInstanceState);
//這裏借用了SlidingMenu的setBehindContentView方法來設置一個透明菜單
View behindView = new View(this);
behindView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
behindView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
setBehindContentView(behindView);
mSlidingMenu = getSlidingMenu();
//設置陰影寬度為10個px
mSlidingMenu.setShadowWidth(10);
//設置陰影
mSlidingMenu.setShadowDrawable(R.drawable.slide_shadow);
//設置以下的布局,也就是我們上面定義的透明菜單離右邊屏幕邊緣的距離為0,也就是滑動開以後菜單會全屏幕顯示
mSlidingMenu.setBehindOffset(0);
mSlidingMenu.setFadeDegree(0.35f);
//菜單打開監聽。由於菜單打開後我們要finish掉當前的Activity
mSlidingMenu.setOnOpenedListener(this);
//設置手勢滑動方向。由於我們要實現微信那種右滑動的效果,這裏設置成SlidingMenu.LEFT模式
mSlidingMenu.setMode(SlidingMenu.LEFT);
//由於微信是僅僅有邊緣滑動。我們設置成TOUCHMODE_MARGIN模式,假設你想要全屏幕滑動,僅僅須要把這個改成TOUCHMODE_FULLSCREEN就OK了
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
super.onCreate(savedInstanceState);
}
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mHelper.onPostCreate(savedInstanceState);
}
@Override
public boolean onSupportNavigateUp() {
return true;
}
@Override
public View findViewById(int id) {
View v = super.findViewById(id);
if (v != null)
return v;
return mHelper.findViewById(id);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mHelper.onSaveInstanceState(outState);
}
@Override
public void setContentView(int id) {
setContentView(getLayoutInflater().inflate(id, null));
}
@Override
public void setContentView(View v) {
setContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
@Override
public void setContentView(View v, ViewGroup.LayoutParams params) {
super.setContentView(v, params);
mHelper.registerAboveContentView(v, params);
}
public void setBehindContentView(int id) {
setBehindContentView(getLayoutInflater().inflate(id, null));
}
public void setBehindContentView(View v) {
setBehindContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
public void setBehindContentView(View v, ViewGroup.LayoutParams params) {
mHelper.setBehindContentView(v, params);
}
public SlidingMenu getSlidingMenu() {
return mHelper.getSlidingMenu();
}
public void toggle() {
mHelper.toggle();
}
public void showContent() {
mHelper.showContent();
}
public void showMenu() {
mHelper.showMenu();
}
public void showSecondaryMenu() {
mHelper.showSecondaryMenu();
}
public void setSlidingActionBarEnabled(boolean b) {
mHelper.setSlidingActionBarEnabled(b);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
boolean b = mHelper.onKeyUp(keyCode, event);
if (b) return b;
return super.onKeyUp(keyCode, event);
}
//滑動全然打開菜單後結束掉當前的Activity
@Override
public void onOpened() {
this.finish();
}
@Override
public void finish() {
super.finish();
this.overridePendingTransition(0, R.anim.slide_out_right);
}
}
這裏須要註意的是。你假設想要邊緣滑動返回,僅僅須要將SlidingMenu的滑動模式改成TOUCHMODE_MARGIN就能夠了,全屏幕滑動的話改成TOUCHMODE_FULLSCREEN就OK了
寫到這基本上就差點兒相同了,我感覺沒啥難度的,就是把Activity的樣式改成透明,抽取出一個父類BaseSwipeBackActivity。在父類裏面加入SlidingMenu。將左菜單設置成透明,然後菜單全然打開後finish掉當前的Activity
最後附上github源代碼下載地址github
Android-通過SlidingMenu高仿微信6.2最新版手勢滑動返回(二)