側(cè)滑效果[第十五篇]:側(cè)滑框架SmartSwipe之全局返回

前面捉貌,我為大家展示了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;

[本章完...]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末餐蔬,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子佑附,更是在濱河造成了極大的恐慌樊诺,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,816評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件音同,死亡現(xiàn)場離奇詭異词爬,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)权均,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評論 3 385
  • 文/潘曉璐 我一進(jìn)店門顿膨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人叽赊,你說我怎么就攤上這事恋沃。” “怎么了蛇尚?”我有些...
    開封第一講書人閱讀 158,300評論 0 348
  • 文/不壞的土叔 我叫張陵芽唇,是天一觀的道長。 經(jīng)常有香客問我取劫,道長,這世上最難降的妖魔是什么研侣? 我笑而不...
    開封第一講書人閱讀 56,780評論 1 285
  • 正文 為了忘掉前任谱邪,我火速辦了婚禮,結(jié)果婚禮上庶诡,老公的妹妹穿的比我還像新娘惦银。我一直安慰自己,他們只是感情好末誓,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,890評論 6 385
  • 文/花漫 我一把揭開白布扯俱。 她就那樣靜靜地躺著,像睡著了一般喇澡。 火紅的嫁衣襯著肌膚如雪迅栅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,084評論 1 291
  • 那天晴玖,我揣著相機(jī)與錄音读存,去河邊找鬼为流。 笑死,一個胖子當(dāng)著我的面吹牛让簿,可吹牛的內(nèi)容都是我干的敬察。 我是一名探鬼主播,決...
    沈念sama閱讀 39,151評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼尔当,長吁一口氣:“原來是場噩夢啊……” “哼莲祸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起椭迎,我...
    開封第一講書人閱讀 37,912評論 0 268
  • 序言:老撾萬榮一對情侶失蹤锐帜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后侠碧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抹估,經(jīng)...
    沈念sama閱讀 44,355評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,666評論 2 327
  • 正文 我和宋清朗相戀三年弄兜,在試婚紗的時候發(fā)現(xiàn)自己被綠了药蜻。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,809評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡替饿,死狀恐怖语泽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情视卢,我是刑警寧澤踱卵,帶...
    沈念sama閱讀 34,504評論 4 334
  • 正文 年R本政府宣布,位于F島的核電站据过,受9級特大地震影響惋砂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜绳锅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,150評論 3 317
  • 文/蒙蒙 一西饵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鳞芙,春花似錦眷柔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至喳坠,卻和暖如春鞠评,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背丙笋。 一陣腳步聲響...
    開封第一講書人閱讀 32,121評論 1 267
  • 我被黑心中介騙來泰國打工谢澈, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留煌贴,地道東北人。 一個月前我還...
    沈念sama閱讀 46,628評論 2 362
  • 正文 我出身青樓锥忿,卻偏偏與公主長得像牛郑,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子敬鬓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,724評論 2 351

推薦閱讀更多精彩內(nèi)容

  • [文章內(nèi)容來自Developers]應(yīng)用通常包含多個 Activity淹朋。每個 Activity 均應(yīng)圍繞用戶可以執(zhí)...
    岳小川閱讀 690評論 0 4
  • 張洛琳 我,焦仲卿的父親钉答,兩千多年被遺忘在黑暗角落的焦父础芍,要撣去歷史的灰塵,訴說遲來的愧疚数尿。 親愛的兒子仑性,我有愧于...
    雅八書攤閱讀 791評論 1 2
  • 現(xiàn)在已經(jīng)是接近凌晨,其實(shí)身體不太能撐右蹦,內(nèi)心卻特難受诊杆。差不多也可以宣告我假期的結(jié)束了 我總想回想下自己假期都在干什么...
    冬瓜的巴拉巴拉閱讀 195評論 0 0
  • “失敗的丈夫”、“缺席的父親”何陆、“再普通不過的小職員”晨汹、生活中的“l(fā)oser”。這就是一部小說《一個人的朝圣》主人...
    為荷一生閱讀 271評論 0 3
  • 這些年贷盲,原來會喜歡的東西就那么些淘这。 去逛超市,以前必買的是酸奶巩剖,現(xiàn)在稍微少喝一些铝穷,隔三差五才買。每次逛超市佳魔,必拿的...
    桔梗麗娜閱讀 233評論 5 11