• 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

  • 相关阅读:
    JSP&JavaBean
    Cookie&&Session
    多个请求使用同一个 Servlet
    C3P0--数据库连接池
    navicat设置唯一键——unique
    J2EE常用组件简介
    JSP基础知识_3
    JSP基础知识_2
    JSP基础知识_1
    Android
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7224386.html
Copyright © 2020-2023  润新知