前面捉貌,我為大家展示了5種側(cè)滑返回效果:仿手機(jī)QQ側(cè)滑返回
贯要、仿微信側(cè)滑返回效果
暖侨、仿MIUI系統(tǒng)貝塞爾曲線側(cè)滑返回
、開門式側(cè)滑返回
崇渗、百葉窗式側(cè)滑返回
字逗。
但是,有些在兼容性上有一定的缺陷宅广。
由于采用了activity透明的方式來透視前一個activity葫掉,在5.0以下版本系統(tǒng)上存在兼容性問題,導(dǎo)致仿微信側(cè)滑返回
跟狱、開門式側(cè)滑返回
和百葉窗式側(cè)滑返回
只在5.0以上版本的設(shè)備生效俭厚。
由于5.0以下版本的設(shè)備占比已經(jīng)較少,與其采用會修改主題為透明并在onCreated中在變?yōu)椴煌该鞯容^為復(fù)雜的方案驶臊,不如分別為5.0以上及以下的采用不同的側(cè)滑返回方案挪挤。
SmartSwipe是用SmartSwipeBack
封裝了側(cè)滑返回功能藕溅。
SmartSwipeBack.java
public class SmartSwipeBack {
public static final ArrayList<Activity> ACTIVITIES = new ArrayList<>();
private static IPreviousFinder mPreviousFinder;
public interface SwipeBackConsumerFactory {
/**
* Create SwipeConsumer to do swipe back business for activity
* @param activity activity to wrap with swipe back
* @return SwipeConsumer
*/
SwipeConsumer createSwipeBackConsumer(Activity activity);
}
public interface ActivitySwipeBackFilter {
/**
* Determine whether the activity parameter should swipe back
* @param activity The activity to wrap or not
* @return true: need to wrap with swipe back, false: do not wrap
*/
boolean onFilter(Activity activity);
}
public static void activityBack(Application application, SwipeBackConsumerFactory factory) {
activityBack(application, factory, null);
}
/**
* The core function for global activity swipe back
* @param application application
* @param factory factory to create SwipeConsumer for each Activity
* @param filter filter of activity, to determine which activity should finish via swipe motion
*/
public static void activityBack(Application application, SwipeBackConsumerFactory factory, ActivitySwipeBackFilter filter) {
if (activitySwipeBackListener == null) {
activitySwipeBackListener = new ActivitySwipeBackListener(factory, filter);
} else {
application.unregisterActivityLifecycleCallbacks(activitySwipeBackListener);
activitySwipeBackListener.mFactory = factory;
activitySwipeBackListener.mFilter = filter;
}
application.registerActivityLifecycleCallbacks(activitySwipeBackListener);
}
////////////////////////////////////////////
//
// swipe back with StayConsumer
//
////////////////////////////////////////////
public static void activityStayBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter) {
final int edgeSize = SmartSwipe.dp2px(20, application);
activityStayBack(application, filter, edgeSize, 0, SwipeConsumer.DIRECTION_LEFT);
}
public static void activityStayBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter, final int edgeSize, final int minVelocity, final int direction) {
SmartSwipeBack.activityBack(application, new SwipeBackConsumerFactory() {
@Override
public SwipeConsumer createSwipeBackConsumer(final Activity activity) {
return new StayConsumer()
.setMinVelocity(minVelocity)
.setEdgeSize(edgeSize)
.enableDirection(direction)
.addListener(new SimpleSwipeListener() {
@Override
public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
if (activity != null) {
activity.finish();
}
}
});
}
}, filter);
}
////////////////////////////////////////////
//
// swipe back with ActivitySlidingBackConsumer
//
////////////////////////////////////////////
public static void activitySlidingBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter) {
final float factor = 0.5f;
// with default scrimColor: transparent
activitySlidingBack(application, filter, factor);
}
public static void activitySlidingBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter, float factor) {
//default edge size
final int edgeSize = SmartSwipe.dp2px(20, application);
final int shadowColor = 0x80000000;
final int shadowSize = SmartSwipe.dp2px(10, application);
final int direction = SwipeConsumer.DIRECTION_LEFT;
activitySlidingBack(application, filter, edgeSize, Color.TRANSPARENT, shadowColor, shadowSize, factor, direction);
}
public static void activitySlidingBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter
, final int edgeSize, final int scrimColor, final int shadowColor, final int shadowSize
, final float factor, final int direction) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//if sdk version is less than 21, the compatibility of ActivitySlidingBackConsumer is not good enough
//use StayConsumer instead for android sdk version <= 20
activityStayBack(application, filter, edgeSize, 0, direction);
} else {
activityBack(application, new SwipeBackConsumerFactory() {
@Override
public SwipeConsumer createSwipeBackConsumer(final Activity activity) {
return new ActivitySlidingBackConsumer(activity)
.setRelativeMoveFactor(factor)
.setScrimColor(scrimColor)
.setShadowColor(shadowColor)
.setShadowSize(shadowSize)
.setEdgeSize(edgeSize)
.enableDirection(direction)
.addListener(new SimpleSwipeListener(){
@Override
public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
if (activity != null) {
activity.finish();
activity.overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
}
}
});
}
}, filter);
}
}
////////////////////////////////////////
//
// swipe back with BezierBackConsumer
//
////////////////////////////////////////
public static void activityBezierBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter) {
final int edgeSize = SmartSwipe.dp2px(20, application);
activityBezierBack(application, filter, edgeSize);
}
public static void activityBezierBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter, int edgeSize) {
final int thickness = SmartSwipe.dp2px(30, application);
final int size = SmartSwipe.dp2px(200, application);
final int direction = SwipeConsumer.DIRECTION_LEFT;
activityBezierBack(application, filter, edgeSize, size, thickness, Color.BLACK, Color.WHITE, direction);
}
public static void activityBezierBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter
, final int edgeSize, final int size, final int thickness, final int color, final int arrowColor, final int direction) {
SmartSwipeBack.activityBack(application, new SwipeBackConsumerFactory() {
@Override
public SwipeConsumer createSwipeBackConsumer(final Activity activity) {
return new BezierBackConsumer()
.setColor(color)
.setArrowColor(arrowColor)
.setSize(size)
.setOpenDistance(thickness)
.addListener(new SimpleSwipeListener() {
@Override
public void onSwipeRelease(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction, float progress, float xVelocity, float yVelocity) {
if (progress >= 1) {
activity.finish();
}
}
})
.setEdgeSize(edgeSize)
.enableDirection(direction);
}
}, filter);
}
////////////////////////////////////////
//
// swipe back with ActivityDoorBackConsumer
//
////////////////////////////////////////
public static void activityDoorBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter) {
final int edgeSize = SmartSwipe.dp2px(20, application);
final int scrimColor = 0x80000000;
final boolean refreshable = true;
activityDoorBack(application, filter, SwipeConsumer.DIRECTION_LEFT, edgeSize, scrimColor, refreshable);
}
public static void activityDoorBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter
, final int direction, final int edgeSize, final int scrimColor, final boolean refreshable) {
SmartSwipeBack.activityBack(application, new SwipeBackConsumerFactory() {
@Override
public SwipeConsumer createSwipeBackConsumer(final Activity activity) {
return new ActivityDoorBackConsumer(activity)
.setRefreshable(refreshable)
.setScrimColor(scrimColor)
.enableDirection(direction)
.setEdgeSize(edgeSize)
.addListener(new SimpleSwipeListener() {
@Override
public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
activity.finish();
activity.overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
}
});
}
}, filter);
}
////////////////////////////////////////
//
// swipe back with ActivityShuttersBackConsumer
//
////////////////////////////////////////
public static void activityShuttersBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter) {
final int edgeSize = SmartSwipe.dp2px(20, application);
final int scrimColor = 0x80000000;
final boolean refreshable = true;
activityShuttersBack(application, filter, SwipeConsumer.DIRECTION_LEFT, edgeSize, scrimColor, refreshable);
}
public static void activityShuttersBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter
, final int direction, final int edgeSize, final int scrimColor, final boolean refreshable) {
SmartSwipeBack.activityBack(application, new SwipeBackConsumerFactory() {
@Override
public SwipeConsumer createSwipeBackConsumer(final Activity activity) {
return new ActivityShuttersBackConsumer(activity)
.setRefreshable(refreshable)
.setScrimColor(scrimColor)
.enableDirection(direction)
.setEdgeSize(edgeSize)
.addListener(new SimpleSwipeListener() {
@Override
public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
activity.finish();
activity.overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
}
});
}
}, filter);
}
private static ActivitySwipeBackListener activitySwipeBackListener;
public static class ActivitySwipeBackListener implements Application.ActivityLifecycleCallbacks {
private SwipeBackConsumerFactory mFactory;
private ActivitySwipeBackFilter mFilter;
ActivitySwipeBackListener(SwipeBackConsumerFactory factory, ActivitySwipeBackFilter filter) {
this.mFactory = factory;
this.mFilter = filter;
}
@Override
public void onActivityCreated(final Activity activity, Bundle savedInstanceState) {
ACTIVITIES.add(activity);
if (mFactory == null) {
return;
}
if (mFilter != null && !mFilter.onFilter(activity)) {
return;
}
SmartSwipe.wrap(activity).addConsumer(mFactory.createSwipeBackConsumer(activity));
}
@Override public void onActivityStarted(Activity activity) { }
@Override public void onActivityResumed(Activity activity) { }
@Override public void onActivityPaused(Activity activity) { }
@Override public void onActivityStopped(Activity activity) { }
@Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { }
@Override public void onActivityDestroyed(Activity activity) {
ACTIVITIES.remove(activity);
}
}
/**
* find previous activity
* @param fromActivity the given activity to find its previous
* @return the previous activity if exists
* @see ActivitySlidingBackConsumer
*/
public static Activity findPreviousActivity(Activity fromActivity) {
if (mPreviousFinder != null) {
return mPreviousFinder.findPreviousActivity(fromActivity);
}
if (fromActivity != null) {
int index = ACTIVITIES.indexOf(fromActivity);
if (index > 0) {
return ACTIVITIES.get(index - 1);
}
}
return null;
}
public static void setPreviousFinder(IPreviousFinder previousFinder) {
mPreviousFinder = previousFinder;
}
public static interface IPreviousFinder {
/**
* find the previous activity for the given activity
* @param fromActivity activity given
* @return the previous activity
*/
Activity findPreviousActivity(Activity fromActivity);
}
}
為了兼容悠菜,需要對SDK版本進(jìn)行判斷,如下:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
SmartSwipeBack.activityBezierBack(this, new SmartSwipeBack.ActivitySwipeBackFilter() {
@Override
public boolean onFilter(Activity activity) {
if(activity instanceof ImageViewerActivity){
return true;
}
return true;
}
});
}else{
SmartSwipeBack.activitySlidingBack(this, new SmartSwipeBack.ActivitySwipeBackFilter() {
@Override
public boolean onFilter(Activity activity) {
if(activity instanceof ImageViewerActivity){
return false;
}
return true;
}
});
}
以上代碼放在Application的onCreate
中即可嚷闭。
【過濾器】
以一下方法舉例
activitySlidingBack(Application application, SmartSwipeBack.ActivitySwipeBackFilter filter)
第一個參數(shù)是當(dāng)前Application纵寝,第二個參數(shù)是過濾器论寨。
SmartSwipeBack.activitySlidingBack(this, new SmartSwipeBack.ActivitySwipeBackFilter() {
@Override
public boolean onFilter(Activity activity) {
if(activity instanceof ImageViewerActivity){
return false;
}
return true;
}
});
以上代碼中的過濾器的返回值是一個布爾類型,false表示不需要側(cè)滑返回店雅,true表示需要側(cè)滑返回政基,當(dāng)然,也可以直接將過濾器置為null闹啦,如:
SmartSwipeBack.activitySlidingBack(this, null);
若為null沮明,則不過濾,對所有activity都執(zhí)行側(cè)滑返回的封裝窍奋。
【內(nèi)置生命周期監(jiān)聽】
SmartSwipeBack類中內(nèi)置了對Activity生命周期的監(jiān)聽荐健,以及管理Activity的返回棧酱畅,當(dāng)Activity被創(chuàng)建的時候,就被放入返回棧中江场。
那么纺酸,如何獲取當(dāng)前Activity的上一個Activity?方法如下:
Activity activity = SmartSwipeBack.findPreviousActivity(this);
如何獲取返回棧中所有的Activity址否?
ArrayList<Activity> list = SmartSwipeBack.ACTIVITIES;
[本章完...]